2016-10-16 80 views
0

我創建對,現在的想法是,通過我的C#程序中插入數據庫裏面的數據「人信息姓名,年齡等」和我在一個簡單的程序我單擊按鈕我得到此錯誤數據類型不匹配的MS接取數據庫

System.Data.OleDb.OleDbException(0x80040E07):條件表達式中的數據類型不匹配。 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult小時) 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult在System.Data.OleDb.OleDbCommand.ExecuteCommandText(tagDBPARAMS dbParams,對象&的ExecuteReuslt) (對象&的ExecuteReuslt) 在System.Data.OleDb.OleDbCommand.ExecuteCommand(的CommandBehavior行爲,對象&的ExecuteReuslt) 在System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(的CommandBehavior行爲,字符串方法) 在System.Data.OleDb.OleDbCommand.ExecuteNonQuery() 在School_System.newRegisteration.button1_Click(對象發件人,EventArgs e)在C:\用戶\ OmarS_000 \文件\的Visual Studio 2015年\項目\學校系統\ SCHO OL SYSTEM \ newRegisteration.cs:行35

這裏是我的代碼

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using System.Data.OleDb; 
    using System.ComponentModel; 


    public partial class newRegisteration : Form 

       { 
      private OleDbConnection connection = new OleDbConnection(); 
      public newRegisteration() 
      { 
       InitializeComponent(); 
       connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\OmarS_000\Documents\Visual Studio 2015\Projects\School System\School System\School.accdb; 
    Persist Security Info=False;"; 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       try 
       { 
        connection.Open(); 
        OleDbCommand command = new OleDbCommand(); 
        command.Connection = connection; 
        command.CommandText = "INSERT into School ([Name], [Age], [Grade], [Class]) VALUES('" + nameTextBox2 + "', '" + ageTextBox2 + "', '" + gradeTextBox2 + "', '" + classTextBox2 + "') "; 

        command.ExecuteNonQuery(); 
        MessageBox.Show("Data Saved"); 
        connection.Close(); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("Error" + ex); 
       } 
      } 
       } 

線35提到的錯誤它包含

command.ExecuteNonQuery(); 

當我在點擊我得到這個錯誤按鈕,但在Visual Studio中的代碼,沒有錯誤完美調試。所以我錯過了什麼地方?

+0

當你通過文字是數字或日期字段會發生。使用SQL參數 – Plutonix

回答

2

您正在使用一些文本框的值,所以你需要使用它的Text屬性來獲取價值

command.CommandText = "INSERT into School ([Name], [Age], [Grade], [Class]) VALUES('" + nameTextBox2.Text + "', '" + ageTextBox2.Text + "', '" + gradeTextBox2.Text + "', '" + classTextBox2.Text + "') "; 
+0

謝謝你這麼多,我不知道我怎麼錯過。 0錯誤的工作:d –

+0

確定這就是偉大的,現在的工作:) – Mostafiz