我正在构建一个基于库存的应用程序,可以直接从任何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)'? –
不..他们正在单独运行 –