user2963950
user2963950

Reputation: 45

Ado.net, DateTime in SqlCommand

I have DataSet "_cinemaZone" with DataTable "SessionsCinema", this DataTable has DataColumn "sessionDateTime" with DataType - "typeof(System.DateTime)".

Help me, please, to use "sessionDateTime" field in SqlCommand.

My wrong tries:

//first
commandSql.Parameters.Add("@sessionDateTime", SqlDbType.DateTime, sizeof(DateTime), "sessionDateTime");

//second
commandSql.Parameters.Add("@sessionDateTime", SqlDbType.DateTime);
commandSql.Parameters["@sessionDateTime"].Value = sessionDateTime;

//third
commandSql.Parameters.Add("@sessionDateTime", SqlDbType.DateTime);
commandSql.Parameters["@sessionDateTime"].Value = Convert.ToDateTime(_cinemaZone.Tables["SessionsCinema"].Columns["sessionDateTime"]);

Upvotes: 0

Views: 1419

Answers (1)

Pantelis
Pantelis

Reputation: 2060

foreach (DataRow row in _cinameZone.Tables["SessionsCinema"].Rows)
{
    var sqlParameter = new SqlParameter("@paramName", SqlDbType.DateTime) 
                       { 
                           Value = Convert.ToDateTime(row["sessionDateTime"].Value);                              
                       }
    cmd.Parameters.Add(sqlParameter);
}

Upvotes: 2

Related Questions