繁体   English   中英

无法提交对SQL Server Compact Edition数据库的更改

[英]Can't commit changes to SQL Server Compact Edition database

我有个问题,

private void button_Submit_Click(object sender, EventArgs e)
{
   try
   {
      string connectionString = @"Data Source=Database_TouchPOS.sdf;Persist Security Info=False;";
      using (SqlCeConnection connection = new SqlCeConnection(connectionString))
      {
         using (SqlCeCommand command = connection.CreateCommand())
         {
            connection.Open();
            command.CommandText = "INSERT INTO Product (Title,Price,Category_Id) VALUES (@title, @price,@category_Id)";
            command.Parameters.AddWithValue("@title", textBox_Title.Text);
            command.Parameters.AddWithValue("@price", textBox_Price.Text);
            command.Parameters.AddWithValue("@category_Id", comboBox_Category.SelectedIndex);

            command.ExecuteNonQuery();

            MessageBox.Show("Product Added Successfully...");
         }
         connection.Close();
      }                
   }
   catch (SqlException ex)
   {
      MessageBox.Show(ex.Message);
   }
}

一切似乎都很好,但仍然无法将数据添加到数据库中。

  1. 我尝试使用完整的数据库路径,例如c:\\project\\database.sdf
  2. 在问之前我做了很多搜索。 我看到了类似的问题,但即便是一个也不适合我。
  3. 编译时添加数据但未提交数据库。 第二次调试后我可以看到数据。
  4. 请尽量详细解释。

谢谢

这是一个非常古老的主题,所以如果我提出答案,我不知道是否会对任何人有所帮助。

我也有这个问题。 当我在C#中执行更新或插入代码时,显然一切正常,但是当我查找数据库时,没有任何变化。

问题是,在调试时,我在Management Studio中打开了数据库。 并且不知何故,即使没有错误消息,这也阻碍了数据库的更改。 关闭Management Studio并在执行代码后打开它,完全存储在数据库中的更改。

问候。

您是否尝试明确使用交易?

IDbTransaction transaction = connection.BeginTransaction();
//add to database
transaction.Commit(); // before close connection

您可能正面临这种情况, http://erikej.blogspot.com/2010/05/faq-why-does-my-changes-not-get-saved.html建议您在连接中使用数据库文件的完整路径串。

那些寻求像我这样的人的完整例子... @"Data Source=C:\\Users\\MYPC\\Documents\\Visual Studio 2010\\Projects\\MyProjectFolder\\MyProject-1\\bin\\Debug\\MyDatabase.sdf;Persist Security Info=False;";

谢谢这个例子。 这节省了我的一天。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM