问题描述
嗨
有谁可以告诉我为什么我用以下代码得到SQL语法错误?
string strInsert =" INSERT INTO dateEntry(条目,日期)VALUES(''test3'',
''17 / 08/2004'')" ;;
OleDbCommand cmd = new OleDbCommand(strInsert,oleDbConnection1);
尝试
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch(例外情况)
{
tbxOutput。 Text = ex.Message;
}
终于
{
oleDbConnection1.Close();
}
我的猜测是日期参数是一个狡猾的格式。而不是
依赖于在SQL文本中正确使用参数。有关更多信息,请参阅
OleDbParameter或OleDbCommand.Parameters。
-
Jon Skeet - < sk *** @ pobox.com>
如果回复小组,请不要给我发邮件
KavvY,
您的异常或消息是否如下所示?
char数据类型的转换到日期时间数据类型导致
超出范围的日期时间值。
该语句已被终止。
这是因为d atetime值作为您输入的字符串形式为
dd / mm / yyyy。 SqlServer可以设置为接受这些日期,但只需使用mm / dd / yyyy格式就可以了。
希望这有助于:)
Mythran
Hi
Can anyone tell me why I get a SQL syntax error with the following code?
string strInsert = "INSERT INTO dateEntry (entry, date) VALUES (''test3'',
''17/08/2004'')";
OleDbCommand cmd = new OleDbCommand(strInsert, oleDbConnection1);
try
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
tbxOutput.Text = ex.Message;
}
finally
{
oleDbConnection1.Close();
}
My guess is that the date parameter is in a dodgy format. Rather than
rely on getting it right in your SQL text, use parameters. See
OleDbParameter or OleDbCommand.Parameters for more information.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
KavvY,
Does your exception or message look like the following?
The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value.
The statement has been terminated.
This is because the datetime value as a string you entered is in the form of
dd/mm/yyyy. SqlServer can be set up to accept those dates but it is more the
wiser to just use the format of mm/dd/yyyy.
Hope this helps :)
Mythran
这篇关于INSERT语法错误OleDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!