我正在構建一個基於庫存的應用程序,可以直接從任何pc運行而無需任何安裝,因此我使用SQL CE ..在開始時,我檢查數據庫是否存在。如果不是我正在創建新數據庫:SQL CE不更新Windows窗體中的數據庫值
try
{
string conString = "Data Source='ITM.sdf';LCID=1033;Password=ABCDEF; Encrypt = TRUE;";
SqlCeEngine engine = new SqlCeEngine(conString);
engine.CreateDatabase();
return true;
}
catch (Exception e)
{
//MessageBox.Show("Database Already Present");
}
該數據庫是正確創建的,我可以訪問數據庫中創建表以及..我面對的問題是 - 我在窗口形式插入和更新記錄按鈕點擊使用代碼:
using (SqlCeConnection connection = new SqlCeConnection(DatabaseConnection.connectionstring))
{
connection.Open();
String Name = NameTxt.Text.ToString();
String Phone = PhoneTxt.Text.ToString();
double balance = Double.Parse(BalanceTxt.Text.ToString());
String City = CityTxt.Text.ToString();
string sqlquery = "INSERT INTO Customers (Name,Phone,Balance,City)" + "Values(@name,@phone, @bal, @city)";
SqlCeCommand cmd = new SqlCeCommand(sqlquery, connection);
cmd.Parameters.AddWithValue("@name", Name);
cmd.Parameters.AddWithValue("@phone", Phone);
cmd.Parameters.AddWithValue("@bal", balance);
cmd.Parameters.AddWithValue("@city", City);
int x = cmd.ExecuteNonQuery();
if (x > 0)
{
MessageBox.Show("Data Inserted");
}
else
{
MessageBox.Show("Error Occured. Cannot Insert the data");
}
connection.Close();
}
和更新用代碼是
using (SqlCeConnection connection = new SqlCeConnection(DatabaseConnection.connectionstring))
{
connection.Open();
int idtoedit = select_id_edit;
String Name = NameEditTxt.Text.ToString();
String Phone = metroTextBox1.Text.ToString();
String City = CityEditTxt.Text.ToString();
string sqlquery = "Update Customers Set Name = @name, Phone = @phone,City = @city where Id = @id";
SqlCeCommand cmd = new SqlCeCommand(sqlquery, connection);
cmd.Parameters.AddWithValue("@name", Name);
cmd.Parameters.AddWithValue("@phone", Phone);
cmd.Parameters.AddWithValue("@id", idtoedit);
cmd.Parameters.AddWithValue("@city", City);
int x = cmd.ExecuteNonQuery();
if (x > 0)
{
MessageBox.Show("Data Updated");
}
else
{
MessageBox.Show("Error Occured. Cannot Insert the data");
}
loadIntoGrid();
connection.Close();
}
每當我執行代碼插入和更新記錄 - 記錄反映在數據網格從數據庫表中填充適配器。但是,一旦我重新啓動應用程序,值不會出現在數據庫中。一旦進入百萬,價值就會反映在數據庫中。我無法理解這個問題背後的原因。
我已經下文稱這些文章:
C# - ExecuteNonQuery() isn't working with SQL Server CE
Insert, Update, Delete are not applied on SQL Server CE database file for Windows Mobile
但由於我編程方式創建數據庫 - 它是越來越直接創建於斌/調試目錄,我看不到它在溶液中用於更改複製選項的視覺工作室中的資源管理器
只是爲了澄清是更新和插入查詢運行**在同一時間**當if(x> 0)'? –
不..他們正在單獨運行 –