gpt4 book ai didi

c# - 插入 MS Access 语句中的语法错误

转载 作者:行者123 更新时间:2023-11-30 22:00:52 25 4
gpt4 key购买 nike

我在下面的 INSERT 语句中找不到语法错误。

public partial class doRegister : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database";
using (OleDbConnection con = new OleDbConnection(str))
using (OleDbCommand cmd = con.CreateCommand())
{
cmd.CommandText = "INSERT INTO users (staffID,accessLevelIdD,username,password,email) VALUES (@staffID, '2', @username,@password,@email)";
cmd.Parameters.AddWithValue("@staffID", Request.Form["staffid"]);
cmd.Parameters.AddWithValue("@password",Request.Form["confpassword"]);
cmd.Parameters.AddWithValue("@username", Request.Form["username"]);
cmd.Parameters.AddWithValue("@email", Request.Form["email"]);
con.Open();
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Successfully registered!");
Response.Redirect("Login.aspx");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
}
}
}
}

最佳答案

似乎 Passwordreserved keyword在 OLE DB 提供程序中。将其与方括号一起使用,例如 [Password]。但作为最佳实践,请将其更改为非保留字。

OleDbCommand 不支持命名参数。

来自 documentation ;

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

在文档中它说?必须使用实际上,它不是。命名参数确实有效,但名称无关紧要;仍然是参数在 CommandText 中的位置以及它们的添加顺序很重要。

并且不要再使用 AddWithValue 了。有时它可能会产生意想不到的结果。使用 .Add() method或者是重载。

阅读:Can we stop using AddWithValue() already?

最后,您不需要在 finally block 中使用 con.Close() 手动关闭连接,因为 using 语句会自动处理它。

顺便说一下,我不得不说,accessLevelIdD 列以 ID 结尾,因此从名称上看就像是数字类型。如果是(或者应该或不应该),您需要将值作为 2 而不是 '2' 传递。

关于c# - 插入 MS Access 语句中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28210620/

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