简体   繁体   中英

SQL Server CE insert query not working fine

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();
        }
    }

I think I am right according to the above code, but it throws an exception:

An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll

Check the SqlCeParameter.ParameterName documentation, specifically the Remarks section :

The .NET Compact Framework Data Provider for SQL Server Compact uses positional parameters that are marked with a question mark (?) instead of named parameters. Although not required, it is recommended to set ParameterName to a string beginning with '@'.

The means the query string in your code should use ? instead of named values like @[Father Name] for the parameter placeholders. You can still use the named value when adding parameters to the collection, and use those names to later lookup a parameter in the collection, but when matching the parameter collection to the query the provider will go off of index order in the collection, and the names won't matter.

Additionally, I count 20 columns in the INTO and VALUES clauses, but only 19 parameters added... so something's not right there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM