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();
}
}
我认为根据上面的代码我是对的,但它会引发异常:
System.Data.SqlServerCe.dll中出现未处理的“System.Data.SqlServerCe.SqlCeException”类型异常
答案 0 :(得分:1)
检查SqlCeParameter.ParameterName文档,特别是the Remarks section:
SQL Server Compact的.NET Compact Framework数据提供程序使用标有问号(?)而不是命名参数的位置参数。虽然不是必需的,但建议将ParameterName设置为以“@”开头的字符串。
表示代码中的query
字符串应使用?
而不是@[Father Name]
等命名值作为参数占位符。在向集合添加参数时,可以仍然使用命名值,并使用这些名称稍后查找集合中的参数,但是当将参数集合与查询匹配时,提供程序将脱离索引在集合中订购,名称无关紧要。
此外,我在INTO
和VALUES
子句中计算了20列,但只添加了19个参数...所以有些东西不在那里。