0
我一直試圖讓下面的代碼寫入我的SQL Server CE數據庫幾個月沒有成功。我的嘗試捕獲不會引發錯誤,但我的數據沒有寫入。有沒有人看到我這樣做的方式有什麼問題?需要幫助寫入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();
您不需要兩次打開連接 – Jonesopolis
您是否在項目項目中列出了您的SDF文件?您是否使用「服務器管理器」窗口檢查此文件的內容?服務器管理器顯示的數據庫文件的位置在哪裏? – Steve