[英]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. 一切似乎都很好,但仍然无法将数据添加到数据库中。
c:\\project\\database.sdf
c:\\project\\database.sdf
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.