2015-04-20 69 views
0

在數據庫中插入代碼時遇到問題,試圖將所有代碼插入到相應的文本框中,並且它不起作用。插入到文本框中c#mdb數據庫

我得到沒有錯誤,但sql代碼不工作。

public partial class CreateAdmin : Form 
    { 
     OleDbConnection db = new OleDbConnection(); 
     OleDbDataAdapter da = new OleDbDataAdapter(); 
     DataTable dt = new DataTable(); 
     public CreateAdmin() 
     { 
      InitializeComponent(); 
      db.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ChattBankMDB.mdb"; 
     } 

     private void textBox4_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void label5_Click(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 


      db.Open(); 
      da = new OleDbDataAdapter("insert into Customers(CustID,CustPassword,CustFirstName,CustLastName,CustAddress,CustEmail)" + "VALUES (?,?,?,?,?,?)", db); 
      db.Parameters.AddWithValue("@CustID", iDTextt.Text); 
      db.Parameters.AddWithValue("@CustPassword", passText.Text); 
      db.Parameters.AddWithValue("@CustFirstName", fnText.Text); 
      db.Parameters.AddWithValue("@CustLastName", lnText.Text); 
      db.Parameters.AddWithValue("@CustAddress", AddText.Text); 
      db.Parameters.AddWithValue("@CustEmail", EmText.Text); 
      db.ExecuteNonQuery(); 
     } 
+0

沒有這甚至編譯?您正在爲連接添加參數。在連接上執行查詢等等。您不需要使用此代碼的oledb命令的適配器。 –

+0

''OleDb'東西不支持**命名參數**(如'@ CustID') - 您需要在SQL中使用'?'作爲佔位符,並且參數是**位置** –

+0

我回答了我的問題自己的問題,謝謝你指出我@Philli – Jimm

回答

0
public partial class CreateAdmin : Form 
    { 
     OleDbConnection db = new OleDbConnection(); 
     OleDbDataAdapter da = new OleDbDataAdapter(); 
     DataTable dt = new DataTable(); 
     public CreateAdmin() 
     { 
      InitializeComponent(); 
      db.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ChattBankMDB.mdb"; 
     } 

     private void textBox4_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void label5_Click(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 


      db.Open(); 
     OleDbCommand df = new OleDbCommand("insert into Customers(CustID,CustPassword,CustFirstName,CustLastName,CustAddress,CustEmail)" + "VALUES (?,?,?,?,?,?)", db); 
      df.Parameters.AddWithValue("@CustID", iDTextt.Text); 
      df.Parameters.AddWithValue("@CustPassword", passText.Text); 
      df.Parameters.AddWithValue("@CustFirstName", fnText.Text); 
      df.Parameters.AddWithValue("@CustLastName", lnText.Text); 
      df.Parameters.AddWithValue("@CustAddress", AddText.Text); 
      df.Parameters.AddWithValue("@CustEmail", EmText.Text); 
      df.ExecuteNonQuery(); 
      db.Close(); 
     }