INSERT INTO 语句的语法错误+sqlserver2005打包

xo_knight 2008-09-10 09:50:56
class User
{
public static int Insert() {
OleDbParameter[] o = new OleDbParameter[2];
o[0] = new OleDbParameter("@name", OleDbType.VarChar);
o[0].Value = "飞机";
o[1] = new OleDbParameter("@pwd", OleDbType.VarChar);
o[1].Value = "fjklsdj";

string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DataBase\GoodsManage.mdb;Persist Security Info=True";
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
OleDbCommand gx = new OleDbCommand("insert into User([Myname],[Pwd]) values('dd','dsfs');", conn);
//foreach (OleDbParameter add in o)
//gx.Parameters.Add(add);
int t = gx.ExecuteNonQuery();
if (t > 0)
{
return 1;
}
else
{
return 0;
}
}
}

这是我写的一个小应用程序中的一个类,由于是桌面应用程序,规模较少,所以就用Access+C#了,结果刚开始编写数据库操作的类的时候就老遇到
未处理 System.Data.OleDb.OleDbException
Message="INSERT INTO 语句的语法错误。"
Source="Microsoft JET Database Engine"
ErrorCode=-2147217900
StackTrace:
在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
在 System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
在 GoodsManage.BaseClass.User.Insert() 位置 F:\projects\GoodsManage\GoodsManage\BaseClass\User.cs:行号 24
在 GoodsManage.Form1.Form1_Load(Object sender, EventArgs e) 位置 F:\projects\GoodsManage\GoodsManage\Form1.cs:行号 37
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.set_Visible(Boolean value)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 GoodsManage.Program.Main() 位置 F:\projects\GoodsManage\GoodsManage\Program.cs:行号 17
在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()

最后我索性不加参数,也不用数据库处理类DBAcess类,直接尝试加数据,居然还是报错,我实在没办法了,到网上找了很久,说是用了Access的保留关键字,但我很明显没有啊,这到底怎么回事啊,求求那位大虾救救我吧,我实在不想用SQL SERVER2005,因为那个打包很麻烦的,如果解决不了,至少告诉如果用SQL SERVER2005应该怎么打包,


谢谢了各位
...全文
269 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xo_knight 2008-09-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 CN_SQL 的回复:

不确定是不是我记错了 ,我记得ACCESS,字符串插入是用的双引号,不是单引号。
[/Quote]
应该是用单引号,我现在重新建了一个项目,全部重新来,结果就没问题了,真郁闷,不过我到目前为止只是用select成功了,其他的还不知道呢
CN_SQL 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xo_knight 的回复:]
引用 6 楼 jacklinchen 的回复:
user是保留字啊
insert into [User]([Myname],[Pwd]) values('dd','dsfs') 试试

加上[]的确不报错了,我早就试过了,但是并没有执行插入操作,数据库表中User内容根本没有改变
[/Quote]
不确定是不是我记错了 ,我记得ACCESS,字符串插入是用的双引号,不是单引号。
xo_knight 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jacklinchen 的回复:]
user是保留字啊
insert into [User]([Myname],[Pwd]) values('dd','dsfs') 试试
[/Quote]
加上[]的确不报错了,我早就试过了,但是并没有执行插入操作,数据库表中User内容根本没有改变
jacklinchen 2008-09-10
  • 打赏
  • 举报
回复
user是保留字啊
insert into [User]([Myname],[Pwd]) values('dd','dsfs') 试试
CN_SQL 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Garnett_KG 的回复:]
把sql语句换成这个试下行不行呀??


SQL code

insert into [User]([Myname],[Pwd])
select 'dd' as Myname,'dsfs' as Pwd
[/Quote]
原来楼主是给了SQL语句的,没注意看。

USER在ACCESS里确实就是关键字,以前遇到过,你把它用“[]”括起来,应该就OK了。
Garnett_KG 2008-09-10
  • 打赏
  • 举报
回复
把sql语句换成这个试下行不行呀??



insert into [User]([Myname],[Pwd])
select 'dd' as Myname,'dsfs' as Pwd


CN_SQL 2008-09-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 xo_knight 的帖子:]
class User
{
public static int Insert() {
OleDbParameter[] o = new OleDbParameter[2];
o[0] = new OleDbParameter("@name", OleDbType.VarChar);
o[0].Value = "飞机";
o[1] = new OleDbParameter("@pwd", OleDbType.VarChar);
o[1].Value = "fjklsdj";

string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data…
[/Quote]
既然你查过说可能是关键字的问题,那么你先把所有的SQL对象,如表,列都用 “[]”括起来,然后再执行看看。
-晴天 2008-09-10
  • 打赏
  • 举报
回复
既然 conn.Open(); 没错,那错误就在
OleDbCommand gx = new OleDbCommand("insert into User([Myname],[Pwd]) values('dd','dsfs');", conn);
检查一下你的语句和你的表有没有问题,应该能够搞定吧.
dawugui 2008-09-10
  • 打赏
  • 举报
回复
真长,只能帮顶了.

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧