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

发布于 2024-12-03 15:53:31 字数 1249 浏览 0 评论 0原文

我有一个问题,

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. 请尝试详细解释。

谢谢

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
  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放我走吧 2024-12-10 15:53:31

您可能面临这个问题, http ://erikej.blogspot.com/2010/05/faq-why-does-my-changes-not-get-saved.html建议您使用数据库文件的完整路径你的连接字符串。

You are probably facing this, http://erikej.blogspot.com/2010/05/faq-why-does-my-changes-not-get-saved.html suggest you use a full path to your database file in your connection string.

久而酒知 2024-12-10 15:53:31

您是否尝试明确使用事务?

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

Did you try to use transactions explicitly?

IDbTransaction transaction = connection.BeginTransaction();
//add to database
transaction.Commit(); // before close connection
寄与心 2024-12-10 15:53:31

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

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

问题是,在调试时我在 Management Studio 中打开了数据库。即使没有错误消息,这也会以某种方式阻碍数据库更改。
关闭 Management Studio 并在执行代码后打开它,更改完美地存储在数据库中。

问候。

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.

The thing was that while debugging I had the database opened in a 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.

Regards.

混浊又暗下来 2024-12-10 15:53:31

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

感谢这个例子。这救了我的命。

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;";

thank for this example. This saved my day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文