简体   繁体   English

使用OLE DB .NET Provider插入数据库表将不起作用

[英]Insert into database table with OLE DB .NET Provider won't work

I am getting an error msg of "syntax error" for the INSERT INTO command when it gets to cmd.ExecuteNonQuery . 当我到达cmd.ExecuteNonQuery时,我得到了INSERT INTO命令的“语法错误”错误消息。

It is important that I use string.Format , and that the structure stays as close to the current structure as possible. 重要的是,我使用string.Format ,并且该结构应保持与当前结构尽可能接近。

        {
            OleDbConnection con = DAL.GetConnection();
            con.Open();
            if (con.State == ConnectionState.Open)
            {
                string s = string.Format("INSERT INTO DataTable1 (Username, Password, FName, LName, Bdate, Sex, City, Mail) VALUES ('{0}', '{1}', '{2}', '{3}', #{4}#, {5}, {6}, '{7}')", uname, pass, fname, lname, bd, sex, city, mail);
                OleDbCommand cmd = DAL.GetCommand(con, s);
                int check = cmd.ExecuteNonQuery();
                if (check == 0)
                {
                    con.Close();
                    Response.Redirect("Reg.aspx?err=-An error has occured. Please try again-");
                }

Thank you. 谢谢。

Probably you have some quotes in the passed text, try using parameters ... 可能您在传递的文本中有一些引号,请尝试使用参数...

string s = "INSERT INTO DataTable1 (Username) VALUES (@user)";
OleDbCommand cmd = DAL.GetCommand(con, s);
//Add the parameter ...
OleDbParameter nam = new OleDbParameter("@user",uname);
cmd.Parameters.Add(nam);

Check out OleDbCommand.Parameters if you haven't already. 如果尚未查看OleDbCommand.Parameters It is safer (along the lines of @Mark B's comment), and it will probably clear up the syntax error you have. 它更安全(沿@Mark B的注释行),并且可能会清除您的语法错误。

If you insist on a pure String.Format approach, simply output cmd.CommandText for debugging to see where the syntax error lies. 如果您坚持使用纯String.Format方法,只需输出cmd.CommandText进行调试即可查看语法错误所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 VFP OLE DB 提供程序关闭表 - How to close a table with the VFP OLE DB Provider 无法使用Ole Db提供程序通过C#查找postgres数据库 - Can't find postgres database through C# using Ole Db provider Microsoft Office 12.0 Access 数据库引擎 OLE DB 提供程序 - Microsoft Office 12.0 Access database engine OLE DB Provider 如果Ole.DB提供程序在系统上可用,为什么OleDbConnection.Open()会抛出无法识别的数据库格式 - Why OleDbConnection.Open() throws Unrecognized database format if Ole.DB provider is available on the system DBF文件的备注字段仅使用VFP OLE DB Provider for .NET返回一些字符 - Memo field from DBF file only returns a few characters using VFP OLE DB Provider for .NET OLE DB覆盖旧数据库 - OLE DB overwriting old database 未在ConnectionString中指定OLE DB提供程序。 'Provider = SQLOLEDB; - An OLE DB Provider was not specified in the ConnectionString. 'Provider=SQLOLEDB; 未在ConnectionString中指定OLE DB提供程序。 例如,“ Provider = SQLOLEDB;” - An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;' 未在ConnectionString中指定OLE DB提供程序。 '提供者= SQLOLEDB - An OLE DB Provider was not specified in the ConnectionString. 'Provider=SQLOLEDB 插入Access数据库不会提交 - Insert into Access Database won't commit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM