2017-10-12 65 views
-1
private void AddMenuButton_Click(object sender, EventArgs e) 
{ 
    if (IsValidated()) 
    { 
     String query = "INSERT INTO Empoyee_GI (Name,[Father Name], Birthdate, Address, City, Zone, Province, [Cell Number], Email, [Employement Status], [Hire Date], [Renewal Date], Location, Position, BPS, Department, Gender, [Maritial Status], CNIC, [Employement Number]) VALUES (@Name,@[Father Name], @Birthdate, @Address, @City, @Zone, @Province, @[Cell Number], @Email, @[Employement Status], @[Hire Date], @[Renewal Date], @Location, @Position, @BPS, @Department, @Gender, @[Maritial Status], @CNIC, @[Employement Number])"; 

     using (SqlCeConnection connection = new SqlCeConnection(@"Data Source=c:\users\the doom's day\documents\visual studio 2012\Projects\StaffFiles1.0\StaffFiles1.0\Employee_DB.sdf")) 
     using (SqlCeCommand command = new SqlCeCommand(query, connection)) 
     { 
      // a shorter syntax to adding parameters 
      command.Parameters.Add("@Name", SqlDbType.NChar).Value = name_TextBox.Text; 
      command.Parameters.Add("@[Father Name]", SqlDbType.NChar).Value = father_Name_TextBox.Text; 

      // a longer syntax for adding parameters 
      command.Parameters.Add("@Birthdate", SqlDbType.NChar).Value = birthdate_DateTimePicker.Text; 
      command.Parameters.Add("@Address", SqlDbType.NChar).Value = address_TextBox.Text; 
      command.Parameters.Add("@City", SqlDbType.NChar).Value = city_ComboBox.Text; 
      command.Parameters.Add("@Zone", SqlDbType.NChar).Value = zone_ComboBox.Text; 
      command.Parameters.Add("@Province", SqlDbType.NChar).Value = province_ComboBox.Text; 
      command.Parameters.Add("@[Cell Number]", SqlDbType.NChar).Value = cell_Number_TextBox.Text; 
      command.Parameters.Add("@[Employement Status]", SqlDbType.NChar).Value = employement_Status_ComboBox.Text; 
      command.Parameters.Add("@[Hire Date]", SqlDbType.NChar).Value = hire_Date_DateTimePicker.Text; 
      command.Parameters.Add("@[Renewal Date]", SqlDbType.NChar).Value =renewal_Date_DateTimePicker.Text; 
      command.Parameters.Add("@[Location]", SqlDbType.NChar).Value = location_ComboBox.Text; 
      command.Parameters.Add("@[Position]", SqlDbType.NChar).Value = position_ComboBox.Text; 
      command.Parameters.Add("@BPS", SqlDbType.NChar).Value = bPS_ComboBox.Text; 
      command.Parameters.Add("@Department", SqlDbType.NChar).Value = department_ComboBox.Text; 
      command.Parameters.Add("@Gender", SqlDbType.NChar).Value = gender_ComboBox.Text; 
      command.Parameters.Add("@[Maritial Status]", SqlDbType.NChar).Value = maritial_Status_ComboBox.Text; 
      command.Parameters.Add("@CNIC", SqlDbType.NChar).Value = cNICTextBox.Text; 
      command.Parameters.Add("@[Employement Number]", SqlDbType.NChar).Value = employement_Number_TextBox.Text; 

      // make sure you open and close(after executing) the connection 
      connection.Open(); 
      command.ExecuteNonQuery(); // I get the error here this line doenot execute. I am really worried about this. 
      connection.Close(); 
     } 
    } 

我想我的權利根據上面的代碼,但它拋出一個異常:的SQL Server CE插入查詢工作不細

類型的未處理的異常「System.Data.SqlServerCe.SqlCeException 「發生在System.Data.SqlServerCe.dll

+3

什麼是異常的_message_? – gunr2171

+2

請勿在參數名稱中使用空格。 –

+0

在System.Data.SqlServerCe.dll中發生類型爲「System.Data.SqlServerCe.SqlCeException」的未處理異常。 – Paramour

回答

1

檢查SqlCeParameter.ParameterName文件,具體the Remarks section

用於SQL Server Compact的.NET Compact Framework數據提供程序使用標記爲問號(?)而不是命名參數的位置參數。雖然不是必需的,但建議將ParameterName設置爲以'@'開頭的字符串。

將意味着query串在你的代碼應該像@[Father Name]的參數佔位符使用?而不是命名值。您可以在向集合中添加參數時仍然使用命名值,並使用這些名稱稍後查找集合中的參數,但是在將參數集合與查詢匹配時,提供程序將從集合中的索引順序中跳出,和名稱無關緊要。

此外,我在INTOVALUES子句中計數了20列,但只添加了19個參數......所以某些東西並不正確。

+0

我算了一次又一次,他們都是20 20 – Paramour

+0

我的錯誤...雖然仍然存在差異。 –