【发布时间】:2013-08-25 15:08:53
【问题描述】:
今天还有一个问题。这一次,我无法从 SQL Server CE 数据库中删除一行。
private void Form1_Load(object sender, EventArgs e)
{
// Create a connection to the file datafile.sdf in the program folder
string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\userDtbs.sdf";
SqlCeConnection connection = new SqlCeConnection("datasource=" + dbfile);
// Read all rows from the table test_table into a dataset (note, the adapter automatically opens the connection)
SqlCeDataAdapter adapter = new SqlCeDataAdapter("SELECT * FROM history", connection);
DataSet data = new DataSet();
adapter.Fill(data);
//Delete from the database
using (SqlCeCommand com = new SqlCeCommand("DELETE FROM accounts WHERE Id = 0", connection))
{
com.ExecuteNonQuery();
}
// Save data back to the databasefile
var cmd = new SqlCeCommandBuilder(adapter);
adapter.Update(data);
// Close
connection.Close();
}
我的程序给我一个错误,告诉我connection 处于关闭状态,我不知道为什么它会在执行DELETE 命令之前关闭。
【问题讨论】:
标签: c# database sql-server-ce delete-row