![](/img/trans.png)
[英]While I'm trying to insert data into my access database it's showing Exceptionhandler error: Syntax error INSERT INTO statement
[英]Syntax error in INSERT INTO Statement while trying to input values in the Database
嗨,我基本上是在創建注冊頁面。 我收到一條錯誤消息:“ 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名字姓氏電話號碼用戶名密碼電子郵件地址城市國家郵政編碼
有人可以幫幫我嗎? :) 謝謝 :)
您應該使用以下查詢:
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)";
該問題是由單詞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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
您在此行中缺少空格:
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.