字符串没有被识别为有效的DateTime。从索引0开始有一个未知的单词。

时间:2023-01-20 06:24:58

Here's a part of the code..

这是代码的一部分。

//HERE'S FOR INSERT

/ /这里是插入

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();

//HERE'S FOR UPDATE

/ /更新

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();

problem #1: having an error in the update part. the error says, "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0." which is on the birthday part. even if date is in correct format like 09/08/11. which works in insert part.

问题1:更新部分有错误。错误说,“字符串没有被识别为有效的DateTime。”有一个不认识的词从0开始,在生日那部分。即使日期格式正确,如09/08/11。在插入部分工作。

problem #2: when i tried to remove the birthday part to see if there's some errors other than on problem #1, here's the error "Syntax error in update statement."

问题2:当我试图删除生日数部分以查看除了问题1之外是否有其他错误时,这里有一个错误“update语句中的语法错误”。

sorry.. im just new to programming. and thanks for the help. God bless.

对不起. .我刚学编程。谢谢你的帮助。上帝保佑。

4 个解决方案

#1


1  

Date problem might be, while ADO is converting your date-time object into string using current locale. I'd rathe tried

日期问题可能是,当ADO使用当前语言环境将您的日期时间对象转换为字符串时。我早熟的尝试

DateTime.Parse(tbBirthday.Text).Date

as was specified by user leppie.

如用户leppie所指定。

For the problem #2, take a look to the SQL expression you have for instert, and for update. Inside Insert you are using @columnName, and in update you are using ? sign. Try to use full name for the parameter, as in Insert SQL: @columnName

对于问题#2,请查看instert和update的SQL表达式。在插入中使用@columnName,在更新中使用?的迹象。尝试为参数使用全名,如Insert SQL: @columnName

#2


1  

The update fails because the parameters are not in the correct order.
The OleDb ADO provider requires that you add the parameter to the collection in the exact order in which they appear on the sql update string

更新失败是因为参数的顺序不正确。OleDb ADO提供程序要求您按照在sql更新字符串中出现的顺序向集合添加参数

Try to move the UserName parameter at the end

尝试在最后移动用户名参数

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(); 

Now the order of the parameters is the same in which they appear in the update string and the date values are correctly understood.

现在,参数的顺序与它们在更新字符串中出现的顺序相同,并且可以正确地理解日期值。

#3


0  

If you are only interested in the date (which it seems so):

如果你只对日期感兴趣(看起来是这样):

DateTime.Parse(tbBirthday.Text).Date

DateTime.Parse .Date(tbBirthday.Text)

or

DateTime.Today

DateTime.Today

in the other cases.

在其他情况下。

#4


0  

try with Date property of DateTime

尝试使用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;//<---

#1


1  

Date problem might be, while ADO is converting your date-time object into string using current locale. I'd rathe tried

日期问题可能是,当ADO使用当前语言环境将您的日期时间对象转换为字符串时。我早熟的尝试

DateTime.Parse(tbBirthday.Text).Date

as was specified by user leppie.

如用户leppie所指定。

For the problem #2, take a look to the SQL expression you have for instert, and for update. Inside Insert you are using @columnName, and in update you are using ? sign. Try to use full name for the parameter, as in Insert SQL: @columnName

对于问题#2,请查看instert和update的SQL表达式。在插入中使用@columnName,在更新中使用?的迹象。尝试为参数使用全名,如Insert SQL: @columnName

#2


1  

The update fails because the parameters are not in the correct order.
The OleDb ADO provider requires that you add the parameter to the collection in the exact order in which they appear on the sql update string

更新失败是因为参数的顺序不正确。OleDb ADO提供程序要求您按照在sql更新字符串中出现的顺序向集合添加参数

Try to move the UserName parameter at the end

尝试在最后移动用户名参数

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(); 

Now the order of the parameters is the same in which they appear in the update string and the date values are correctly understood.

现在,参数的顺序与它们在更新字符串中出现的顺序相同,并且可以正确地理解日期值。

#3


0  

If you are only interested in the date (which it seems so):

如果你只对日期感兴趣(看起来是这样):

DateTime.Parse(tbBirthday.Text).Date

DateTime.Parse .Date(tbBirthday.Text)

or

DateTime.Today

DateTime.Today

in the other cases.

在其他情况下。

#4


0  

try with Date property of DateTime

尝试使用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;//<---