0

嗨,我基本上是在创建一个注册页面。我收到一条错误消息,提示“INSERT INTO 语句中的语法错误”。有时也会收到错误消息,说明连接已打开或其他内容。它以前在不同的表和不同的字段等中工作......代码如下

public partial class Registration : System.Web.UI.Page
{
    static OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\New folder\Project 1.0\WebSite1\New Microsoft Office Access 2007 Database.accdb");
    OleDbDataAdapter ada = new OleDbDataAdapter();
    OleDbCommand cmd = new OleDbCommand();
    OleDbDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    string str = "insert into User_Registeration (First_Name, Last_name, Phone_No, Username, Password, Email, Address, City, Country, Zipcode)" +
         "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    con.Open();
    cmd = new OleDbCommand(str, con);
    cmd.Parameters.AddWithValue("@p1", TextBox1.Text);
    cmd.Parameters.AddWithValue("@p2", TextBox2.Text);
    cmd.Parameters.AddWithValue("@p3", TextBox3.Text);
    cmd.Parameters.AddWithValue("@p4", TextBox4.Text);
    cmd.Parameters.AddWithValue("@p5", TextBox5.Text);
    cmd.Parameters.AddWithValue("@p6", TextBox6.Text);
    cmd.Parameters.AddWithValue("@p7", TextBox8.Text);
    cmd.Parameters.AddWithValue("@p8", TextBox12.Text);
    cmd.Parameters.AddWithValue("@p9", TextBox9.Text);
    cmd.Parameters.AddWithValue("@p10", TextBox11.Text);
    cmd.ExecuteNonQuery();
    con.Close();
}

}

我的 mc 访问表具有以下结构...

ID 名字 姓氏 电话号码 用户名 密码 电子邮件地址 城市 国家 邮政编码

有人可以帮帮我吗?:) 谢谢 :)

4

3 回答 3

2

你应该使用这个查询:

  string str = "insert into User_Registeration (First_Name, Last_name, Phone_No, [Username], [Password], [Email], [Address], City, Country, Zipcode)" +
     " values (@p1, @p2, @p3,@p4, @p5,@p6, @p7,@p8,@p9,@p10)";
于 2013-03-29T13:56:35.533 回答
1

问题是由 PASSWORD 一词引起的。这个词是JET (MS-Access)中的保留词
要在 sql 命令中使用该词,您需要用方括号将其封装起来。

当然,当您向查询添加参数时,您应该确保添加查询中占位符所期望的确切数量的参数。
您有 10 个占位符 (?),因此您需要 10 个参数并按照各个字段预期的确切顺序

所以,总结

string str = "insert into User_Registeration (First_Name, Last_name, Phone_No, " + 
             "Username, [Password], Email, Address, City, Country, Zipcode)" +
             "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
于 2013-03-29T14:08:50.640 回答
0

您在这一行中缺少一个空格:

string str = "insert into User_Registeration (First_Name, Last_name, Phone_No, Username, Password, Email, Address, City, Country, Zipcode)" +
     "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

应该 :

string str = "insert into User_Registeration (First_Name, Last_name, Phone_No, Username, Password, Email, Address, City, Country, Zipcode)" +
     " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
于 2013-03-29T13:52:36.743 回答