2014-01-23 59 views
0

下面附代碼,代碼工作正常,但它不是插入的價值觀,到我已經創建了訪問數據庫表,無論是代碼顯示任何錯誤使用C#插入數據訪問,淨

namespace WindowsFormsApplication1 
    { 
     public partial class Form1 : Form 
     { 
      public Form1() 
      { 
       InitializeComponent(); 
      } 

      private void textBox2_TextChanged(object sender, EventArgs e) 
      { 
      } 

      private void submit_Click(object sender, EventArgs e)     
      {  
       OleDbConnection cnon = new OleDbConnection(); 
       cnon.ConnectionString [email protected]"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\visual_c\Database71.accdb"; 
       OleDbCommand command = new OleDbCommand(); 
       command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number)VALUES('" + textBox1.Text + "','" + textBox2.Text + "')"; 
       cnon.Open(); 
       command.Connection = cnon; 
       command.ExecuteNonQuery(); 
       cnon.Close(); 
      } 
     } 
    } 
+0

是提交按鈕的事件hanlder連接到submit_Click? – Jade

回答

0

你在這裏缺少一個空格:

"(Asset_name,Asset_number)VALUES" 

嘗試改變你的命令字符串:

command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES('" + textBox1.Text + "','" + textBox2.Text + "')"; 

你笑ULD使用parameterized queries防止Sql Injection攻擊

command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES(@name,@number)"; 
command.Parameters.AddWithValue("@name", textBox1.Text); 
command.Parameters.AddWithValue("@number", textBox2.Text); 
+0

我嘗試使用命令parameteres,但仍然無法正常工作。它仍然沒有顯示任何錯誤,也沒有插入任何值訪問後,我點擊提交 – Rajat

0

使用parameteraized查詢 確保您的按鈕OnClick事件

command.CommandText = "INSERT INTO electricity (Asset_name,Asset_number) VALUES (@Assetname,@Assetnumber)",cnon; 

command.Parameters.AddWithValue("@Assetname", textBox1.Text); 
command.Parameters.AddWithValue("@Assetnumber", textBox2.Text); 
+0

我試了一遍參數和在新窗口中的窗體應用程序,它的工作..謝謝lot – Rajat

+0

@Rajat如果有用標記答案爲有用 –