gpt4 book ai didi

c# - 从 ASP.NET 向 MSAcess 添加记录

转载 作者:行者123 更新时间:2023-11-30 17:14:45 25 4
gpt4 key购买 nike

我使用 ASP.NET 开发 MYSQL。就在今天开始在 MSAccess 中尝试 SQL 命令。我尝试了以下代码,但我收到了未处理的异常错误。任何人请检查以下内容并让我知道错误在哪里。

string ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\project\Pages\db.mdb";
OleDbConnection MyConn = new OleDbConnection(ConnStr);
OleDbCommand cmd = new OleDbCommand();
cmd = new OleDbCommand("INSERT INTO accounts(email, password)VALUES(?, ?)", MyConn);
cmd.Parameters.Add("email", OleDbType.VarChar).Value = "val1";
cmd.Parameters.Add("password", OleDbType.VarChar).Value = "val2";
MyConn.Open();
cmd.ExecuteNonQuery();
MyConn.Close();

我创建了一个包含表帐户和字段 ID、电子邮件和密码的 Access 数据库。

我得到的确切错误是(但我相信我的语法是正确的)

Syntax error in INSERT INTO statement.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.

Source Error:


Line 22: cmd.Parameters.Add("password", OleDbType.VarChar).Value = "val2";
Line 23: MyConn.Open();
Line 24: cmd.ExecuteNonQuery();
Line 25: MyConn.Close();
Line 26: }

最佳答案

“密码”是保留字。把它放在括号内。

http://support.microsoft.com/kb/321266

cmd = new OleDbCommand("INSERT INTO accounts(email, [password])VALUES(?, ?)", MyConn);

关于c# - 从 ASP.NET 向 MSAcess 添加记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8458826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com