这是代码的一部分..
//在这里插入
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO tblUsersAccount (Username,[Password],Firstname, MiddleName, Lastname,Birthday,ContactNo,DateCreated,DateModified) values (@Username,@Password,@Firstname,@MiddleName,@Lastname,@Birthday,@ContactNo,@DateCreated,@DateModified)";
command.Parameters.Add("@Username", OleDbType.Char).Value = tbUsername.Text;
command.Parameters.Add("@Password", OleDbType.Char).Value = tbPassword.Text;
command.Parameters.Add("@Firstname", OleDbType.Char).Value = tbFirstname.Text;
command.Parameters.Add("@MiddleName", OleDbType.Char).Value = tbMiddleName.Text;
command.Parameters.Add("@Lastname", OleDbType.Char).Value = tbLastname.Text;
command.Parameters.Add("@Birthday", OleDbType.Date).Value =DateTime.Parse(tbBirthday.Text);
command.Parameters.Add("@ContactNo", OleDbType.Char).Value = tbContactNo.Text;
command.Parameters.Add("@DateCreated", OleDbType.Date).Value = DateTime.Now;
command.Parameters.Add("@DateModified", OleDbType.Date).Value = DateTime.Now;
command.ExecuteNonQuery();
//这里有更新
OleDbCommand command = new OleDbCommand();
command.CommandText = "UPDATE tblUsersAccount SET Password = ?, Firstname = ?, MiddleName = ?, Lastname = ?, Birthday = ?, ContactNo = ? WHERE Username = ?";
connect.ConnectionString = connectionString;
connect.Open();
command.Connection = connect;
command.Parameters.Add("@Username", OleDbType.Char).Value = tbUsername.Text;
command.Parameters.Add("@Password", OleDbType.Char).Value = tbPassword.Text;
command.Parameters.Add("@Firstname", OleDbType.Char).Value = tbFirstname.Text;
command.Parameters.Add("@MiddleName", OleDbType.Char).Value = tbMiddleName.Text;
command.Parameters.Add("@Lastname", OleDbType.Char).Value = tbLastname.Text;
command.Parameters.Add("@Birthday", OleDbType.Date).Value =DateTime.Parse(tbBirthday.Text);
command.Parameters.Add("@ContactNo", OleDbType.Char).Value = tbContactNo.Text;
command.ExecuteNonQuery();
问题#1:更新部分出错。错误说,“字符串未被识别为有效的DateTime。从索引0开始有一个未知的单词。”这是生日那天。即使日期的格式正确,如09/08/11。它适用于插入部分。
问题#2:当我试图删除生日部分以查看除问题#1之外是否存在某些错误时,这里出现错误“更新语句中的语法错误”。
抱歉..我刚接触编程。并感谢您的帮助。上帝保佑。答案 0 :(得分:1)
更新失败,因为参数的顺序不正确 OleDb ADO提供程序要求您按照它们在sql更新字符串中出现的确切顺序将参数添加到集合中
尝试在末尾移动UserName参数
OleDbCommand command = new OleDbCommand();
command.CommandText = "UPDATE tblUsersAccount SET [Password] = ?, Firstname = ?, " +
"MiddleName = ?, Lastname = ?, Birthday = ?, ContactNo = ? " +
"WHERE Username = ?";
connect.ConnectionString = connectionString;
connect.Open();
command.Connection = connect;
command.Parameters.Add("@Password", OleDbType.Char).Value = tbPassword.Text;
command.Parameters.Add("@Firstname", OleDbType.Char).Value = tbFirstname.Text;
command.Parameters.Add("@MiddleName", OleDbType.Char).Value = tbMiddleName.Text;
command.Parameters.Add("@Lastname", OleDbType.Char).Value = tbLastname.Text;
command.Parameters.Add("@Birthday", OleDbType.Date).Value =DateTime.Parse(tbBirthday.Text);
command.Parameters.Add("@ContactNo", OleDbType.Char).Value = tbContactNo.Text;
command.Parameters.Add("@Username", OleDbType.Char).Value = tbUsername.Text;
command.ExecuteNonQuery();
现在,参数的顺序与它们在更新字符串中的显示顺序相同,并且正确理解了日期值。
答案 1 :(得分:1)
日期问题可能是,而ADO正在使用当前区域设置将您的日期时间对象转换为字符串。我试过了
DateTime.Parse(tbBirthday.Text).Date
由用户leppie
指定。
对于问题#2,请查看您对instert和更新的SQL表达式。在插入内部,您使用的是@columnName
,而在更新中,您使用的是?
符号。尝试使用参数的全名,如插入SQL:@columnName
答案 2 :(得分:0)
如果您只对日期感兴趣(看起来如此):
DateTime.Parse(tbBirthday.Text).Date
或
DateTime.Today
在其他情况下。
答案 3 :(得分:0)
尝试使用DateTime的Date属性
command.CommandText = "INSERT INTO tblUsersAccount (Username,[Password],Firstname, MiddleName, Lastname,Birthday,ContactNo,DateCreated,DateModified) values (@Username,@Password,@Firstname,@MiddleName,@Lastname,@Birthday,@ContactNo,@DateCreated,@DateModified)";
command.Parameters.Add("@Username", OleDbType.Char).Value = tbUsername.Text;
command.Parameters.Add("@Password", OleDbType.Char).Value = tbPassword.Text;
command.Parameters.Add("@Firstname", OleDbType.Char).Value = tbFirstname.Text;
command.Parameters.Add("@MiddleName", OleDbType.Char).Value = tbMiddleName.Text;
command.Parameters.Add("@Lastname", OleDbType.Char).Value = tbLastname.Text;
command.Parameters.Add("@Birthday", OleDbType.Date).Value =DateTime.Parse(tbBirthday.Text).Date;//<---
command.Parameters.Add("@ContactNo", OleDbType.Char).Value = tbContactNo.Text;
command.Parameters.Add("@DateCreated", OleDbType.Date).Value = DateTime.Now.Date; //<---
command.Parameters.Add("@DateModified", OleDbType.Date).Value = DateTime.Now.Date;//<---