INSERT语法错误OleDB [英] INSERT syntax error OleDB
问题描述
嗨
有谁可以告诉我为什么我用以下代码得到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();
}
KavvY< k@u.r>写道:任何人都可以告诉我为什么我用以下代码得到SQL语法错误?
string strInsert =" INSERT INTO dateEntry(entry,date)VALUES(' 'test3'',
''17 / 08/2004'')" ;;
OleDbCommand cmd = new OleDbCommand(strInsert,oleDbConnection1);
尝试
{mmD.ExecuteNonQuery();
}
catch(例外情况)
{
Text = ex.Message;
}
终于
{
oleDbConnection1.Close();
}
我的猜测是日期参数是一个狡猾的格式。而不是
依赖于在SQL文本中正确使用参数。有关更多信息,请参阅
OleDbParameter或OleDbCommand.Parameters。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
首先,你得到的错误信息是什么?
您可以在查询分析器中执行相同的查询而不会出现问题吗?
查询似乎还不错。
干杯,
-
伊格纳西奥·马奇,
ignacio .machin AT dot.state.fl.us
佛罗里达州交通局
" KavvY" < k@u.r>在消息中写道
news:qv ******************* @ news-text.cableinet.net ...嗨
任何人都可以告诉我为什么我用以下代码得到SQL语法错误?
string strInsert =" INSERT INTO dateEntry(entry,date)VALUES( ''test3'',
''17 / 08/2004'')" ;;
OleDbCommand cmd = new OleDbCommand(strInsert,oleDbConnection1);
尝试
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
tbxOutput .Text = ex.Message;
}
终于
{
oleDbConnection1.Close();
}
" KavvY" < k@u.r>在消息中写道
news:qv ******************* @ news-text.cableinet.net ...嗨
任何人都可以告诉我为什么我用以下代码得到SQL语法错误?
string strInsert =" INSERT INTO dateEntry(entry,date)VALUES( ''test3'',
''17 / 08/2004'')" ;;
OleDbCommand cmd = new OleDbCommand(strInsert,oleDbConnection1);
尝试
{
oleDbConnection1.Open();
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
tbxOutput .Text = ex.Message;
}
终于
{
oleDbConnection1.Close();
}
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();
}
KavvY <k@u.r> wrote: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
Hi,
First of all, what is the error message you are getting?
Can you execute the same query in the query analyser without problem?
the query seems to be ok though.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"KavvY" <k@u.r> wrote in message
news:qv*******************@news-text.cableinet.net...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();
}
"KavvY" <k@u.r> wrote in message
news:qv*******************@news-text.cableinet.net...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();
}
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!