简体   繁体   English

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

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

I have a problem, 我有个问题,

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);
   }
}

Everything seem to be fine, but still can't add data into database. 一切似乎都很好,但仍然无法将数据添加到数据库中。

  1. I did try with complete database path, example c:\\project\\database.sdf 我尝试使用完整的数据库路径,例如c:\\project\\database.sdf
  2. I did a lot of search before asking. 在问之前我做了很多搜索。 I saw similar problems but not even a single one works for me. 我看到了类似的问题,但即便是一个也不适合我。
  3. Data are added when compiling but not committed to database. 编译时添加数据但未提交数据库。 I can see the data after second attempt of debugging. 第二次调试后我可以看到数据。
  4. Please kindly try to explain in detail. 请尽量详细解释。

Thanks 谢谢

This is a very old thread, so I don't know if it will help anyone if I put my answer. 这是一个非常古老的主题,所以如果我提出答案,我不知道是否会对任何人有所帮助。

I had this problem, too. 我也有这个问题。 When I executed an update or insert code in C#, apparently, everything was ok, but when I looked up the database, there was no change. 当我在C#中执行更新或插入代码时,显然一切正常,但是当我查找数据库时,没有任何变化。

The thing was that while debugging I had the database opened in a Management Studio. 问题是,在调试时,我在Management Studio中打开了数据库。 And somehow this obstructed the database changes even when there was no error message. 并且不知何故,即使没有错误消息,这也阻碍了数据库的更改。 Closing the Management Studio and opening it after executing the code, the changes where perfectly stored in the data base. 关闭Management Studio并在执行代码后打开它,完全存储在数据库中的更改。

Regards. 问候。

Did you try to use transactions explicitly? 您是否尝试明确使用交易?

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建议您在连接中使用数据库文件的完整路径串。

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

thank for this example. 谢谢这个例子。 This saved my day. 这节省了我的一天。

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

相关问题 无法在SQL Server Compact Edition中删除有条件的数据 - Can't delete data with condition in SQL Server Compact Edition 可以共享一个SQL Compact Edition 3.5数据库吗? - Can share a SQL Compact Edition 3.5 database? 如果我的连接字符串包含max database size关键字,为什么SQL Server Compact Edition无法打开现有的SDF文件? - Why can't SQL Server Compact Edition open an existing SDF file if my connection string contains the max database size keyword? 所做的更改不会永久保存到SQL Server Compact版本中 - Changes do not get saved permanently into SQL Server Compact edition SQL Server可以复制到SQL Server Compact Edition吗? - Can SQL Server replicate TO SQL Server Compact Edition? SQL查询在SQL Server Compact Edition中不起作用 - SQL query doesn't not work in SQL Server Compact Edition 为什么我不能用C#将数据插入本地数据库(SQL Compact Edition)? - Why can't I insert data into local database (SQL Compact Edition) with C#? SQL Server Compact Edition 4 - AccessViolationException - SQL Server Compact Edition 4 - AccessViolationException SQL Server Compact Edition无服务器吗? - Is SQL Server Compact Edition Serverless? 您正在尝试访问SQL Server Compact Edition数据库的旧版本 - You are trying to access an older version of a SQL Server Compact Edition database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM