尝试将日期插入数据库时出现错误,这是插入命令:
cmd.CommandText = "INSERT INTO Customer([date1])VALUES(Convert.ToDateTime(date1.Text))
错误是:
String was not recognized as a valid datetime.
尝试将日期插入数据库时出现错误,这是插入命令:
cmd.CommandText = "INSERT INTO Customer([date1])VALUES(Convert.ToDateTime(date1.Text))
错误是:
String was not recognized as a valid datetime.
你这里有一个字符串。
Convert.ToDateTime(date1.Text)
永远不会编译成你需要的。它将在数据库中发送。使用参数化命令并将结果传递给参数。
例如:
string sqlstring = "INSERT INTO Customer([date1])VALUES(@Date)";
SqlCommand cmd = new SqlCommand(sqlstring, conn);
SqlParameter par = new SqlParameter("@Date", Convert.ToDateTime(date1.Text));
cmd.Parameters.Add(par);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();