消息242,级别16,状态3,行1将varchar数据类型转换为日期时间数据类型导致超出范围的值

时间:2015-10-08 11:30:16

标签: c#-4.0

insert into book_issues(stu_id,stu_name,book_id,book_title,
user_type,issu_date,exect_return_date,is_return) 
values
( 5,'thenmozhi',10,'windows programming', 
'Student','10-10-2015 16:23:55','15-10-2015 16:23:55','False');

我收到此代码的错误,请告诉我我的错...

1 个答案:

答案 0 :(得分:0)

你不应该这样做。 总是尝试参数化的方式。

喜欢:cmd.Parameters.AddWithValue("@datecreation", Convert.ToDateTime(datecreation));

点击此处: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value error

你可以这样做:

using(SqlConnection conn = new SqlConnection("Connectionstring"))
using (SqlCommand cmd = new SqlCommand())
{
    string sql = "insert into Usertable ";
    sql += "values(@mVendid, @usrname, @usrpass, @datecreation, @createdby)";
    cmd.CommandText = sql;
    cmd.Parameters.AddWithValue("@mVendid", mVendid);
    cmd.Parameters.AddWithValue("@usrname", username);
    cmd.Parameters.AddWithValue("@usrpass", userpass);
    cmd.Parameters.AddWithValue("@datecreation", Convert.ToDateTime(datecreation));
    cmd.Parameters.AddWithValue("@createdby", createdby);
    cmd.Connection = conn;
    conn.Open();
    cmd.ExecuteNonQuery();
}

只需根据您的代码更改此内容。