2017-10-12 63 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

我的错误...虽然仍然存在差异。 –