asp 将 varchar 数据类型转换为日期时间错误 SQL aspx
asp conversion of a varchar data type to a datetime error SQL aspx
我不知道我需要做什么。我搜索了两天,没有找到有用的东西。
fieldValue = row.Cells[0].Text;
DateTime timeNow = DateTime.Now;
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["ITCConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(constring)) {
timeNow = Convert.ToDateTime(timeNow);
using (SqlCommand cmd = new SqlCommand("UPDATE AktywneZgloszenia SET Data_przyjecia_do_realizacji='" + timeNow + "' WHERE Nr_zgloszenia ='"+fieldValue+"';", connection)) {
using (SqlDataAdapter da = new SqlDataAdapter(cmd)) {
connection.Open();
cmd.ExecuteNonQuery(); // <--executeNon
connection.Close();
GridView1.DataBind();
}
}
}
当我尝试 save/update 变量时间现在我有一个错误:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value. The statement has been terminated.
两个数据库中的列类型= datetime
dane[] type= string 中的变量。(不在此片段中)
我清除了数据库中的所有数据。
我做错了什么?
正如 Martin Smith 所暗示的,您想使用 SQL 命令参数而不是通过串联构建命令。
类似于:
fieldValue = row.Cells[0].Text;
DateTime timeNow = DateTime.Now;
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["ITCConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(constring)) {
// timeNow = Convert.ToDateTime(timeNow); (not needed)
using (SqlCommand cmd = new SqlCommand("UPDATE AktywneZgloszenia SET Data_przyjecia_do_realizacji= @pTimeNow WHERE Nr_zgloszenia = @pFieldValue;", connection)) {
using (SqlDataAdapter da = new SqlDataAdapter(cmd)) {
connection.Open();
cmd.Parameters.AddWithValue("@pTimeNow", timeNow); // <-- 'pass' timenow
cmd.Parameters.AddWithValue("@pFieldValue", fieldValue); // <-- 'pass' fieldValue
cmd.ExecuteNonQuery(); // <--executeNon
connection.Close();
GridView1.DataBind();
}
}
}
我不知道我需要做什么。我搜索了两天,没有找到有用的东西。
fieldValue = row.Cells[0].Text;
DateTime timeNow = DateTime.Now;
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["ITCConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(constring)) {
timeNow = Convert.ToDateTime(timeNow);
using (SqlCommand cmd = new SqlCommand("UPDATE AktywneZgloszenia SET Data_przyjecia_do_realizacji='" + timeNow + "' WHERE Nr_zgloszenia ='"+fieldValue+"';", connection)) {
using (SqlDataAdapter da = new SqlDataAdapter(cmd)) {
connection.Open();
cmd.ExecuteNonQuery(); // <--executeNon
connection.Close();
GridView1.DataBind();
}
}
}
当我尝试 save/update 变量时间现在我有一个错误:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.
两个数据库中的列类型= datetime dane[] type= string 中的变量。(不在此片段中) 我清除了数据库中的所有数据。 我做错了什么?
正如 Martin Smith 所暗示的,您想使用 SQL 命令参数而不是通过串联构建命令。
类似于:
fieldValue = row.Cells[0].Text;
DateTime timeNow = DateTime.Now;
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["ITCConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(constring)) {
// timeNow = Convert.ToDateTime(timeNow); (not needed)
using (SqlCommand cmd = new SqlCommand("UPDATE AktywneZgloszenia SET Data_przyjecia_do_realizacji= @pTimeNow WHERE Nr_zgloszenia = @pFieldValue;", connection)) {
using (SqlDataAdapter da = new SqlDataAdapter(cmd)) {
connection.Open();
cmd.Parameters.AddWithValue("@pTimeNow", timeNow); // <-- 'pass' timenow
cmd.Parameters.AddWithValue("@pFieldValue", fieldValue); // <-- 'pass' fieldValue
cmd.ExecuteNonQuery(); // <--executeNon
connection.Close();
GridView1.DataBind();
}
}
}