几个月来,我一直试图将以下代码写入我的 SQL Server CE 数据库,但没有成功。我的尝试捕获没有捕获抛出的错误,但我的数据没有写入。有人认为我这样做有什么问题吗?
conn = new SqlCeConnection("Data Source = LaZSolutions.sdf");
conn.Open();
var id = Guid.NewGuid();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Customers (FirstName, LastName, Address, City, State, Zip, Phone, Mobile, Email, CustomerNum) VALUES(@FirstName, @LastName, @Address, @City, @State, @Zip, @HomePhone, @MobilePhone, @Email, @CustomerNum)";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@FirstName", txtFName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLName.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@HomePhone", txtHPhone.Text);
cmd.Parameters.AddWithValue("@MobilePhone", txtMPhone.Text);
cmd.Parameters.AddWithValue("@City", txtCity.Text);
cmd.Parameters.AddWithValue("@State", txtState.Text);
cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@CustomerNum", id);
cmd.ExecuteNonQuery();
conn1 = new SqlCeConnection("Data Source = LaZSolutions.sdf");
conn1.Open();
SqlCeCommand cmd1 = conn1.CreateCommand();
cmd1.CommandText = "INSERT INTO Orders (OrderNum, OrderDate, Cost, [Open], Comments) VALUES(@OrderNum, @OrderDate, @Cost, @Open, @Comments)";
cmd1.CommandType = CommandType.Text;
cmd1.Connection = conn1;
cmd1.Parameters.AddWithValue("@OrderNum", txtOrderNum.Text);
cmd1.Parameters.AddWithValue("@OrderDate", txtOrderdate.Text);
cmd1.Parameters.AddWithValue("@Cost", lblPrice.Text);
cmd1.Parameters.AddWithValue("@Open", open);
cmd1.Parameters.AddWithValue("@CustomerNum", id);
cmd1.Parameters.AddWithValue("@Comments", txtComments.Text);
cmd1.ExecuteNonQuery();
conn1.Close();
conn.Close();