我的Insert语句出错

时间:2014-02-16 23:31:55

标签: c# sql ms-access-2003

我的代码遇到问题,我得到Invalid expression term ')'不确定我做错了什么。这是我的代码。

protected void btnSubmit_Click(object sender, EventArgs e)
{
    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = '" + Server.MapPath("WSC_DB.mdb") + "'; Persist Security Info=False");

    using (OleDbCommand cmd = new OleDbCommand("insert into Users(UserFirstName, UserLastName, ShipAddress, ShipCity, ShipState, UserPhone, UserEmail, UserName, UserPassword, LoginType) values (@FirstName, @LastName, @Address, @City, @State, @Zip, @Phone, @Email, @Username, @Password, @Logintype)", conn))
    {
        cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
        cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
        cmd.Parameters.AddWithValue("@City", txtCity.Text);
        cmd.Parameters.AddWithValue("@State", DropDownList1.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
        cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
        cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
        cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
        cmd.Parameters.AddWithValue("@Logintype", "U");
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

2 个答案:

答案 0 :(得分:1)

列列表中有10个项目,值列表中有11个项目。值列表包括

... @City, @State, @Zip, @Phone, @Email, ...

但列列表中没有Zip

... ShipCity, ShipState, UserPhone, UserEmail, ...

答案 1 :(得分:0)

我似乎记得在使用Jet OLEDB时不能在SQL语句中使用命名参数。您需要将@FirstName,@ LastName,...替换为问号'?'。

为了更好地分析您的错误,请包含整个错误消息和堆栈跟踪。