C#:删除DataTable中的行不适用于数据库
我的任务应该很简单,但过了几个小时,我必须承认我完全被卡住了 我只想从C#:删除DataTable中的行不适用于数据库,c#,sqlite,C#,Sqlite,我的任务应该很简单,但过了几个小时,我必须承认我完全被卡住了 我只想从数据表中删除数据行。My datatable是SQLite数据库中当前数据集中的表的副本。必须使用table.row.Delete()方法。我知道delete()只是在表更新时标记要删除的行 以下是我当前使用的代码: 我通过以下方式检索数据: public DataTable GetTable(string tableName) { string connectionPath = dbVaria
数据表
中删除数据行
。My datatable是SQLite数据库中当前数据集中的表的副本。必须使用table.row.Delete()
方法。我知道delete()
只是在表更新时标记要删除的行
以下是我当前使用的代码:
我通过以下方式检索数据:
public DataTable GetTable(string tableName)
{
string connectionPath = dbVariables.ConnectionString;
try
{
SQLiteConnection myConnection = new SQLiteConnection(connectionPath);
myConnection.Open();
string cmdStr = "SELECT * FROM " + tableName;
DataTable myTable = new DataTable();
SQLiteDataAdapter myAdapter = new SQLiteDataAdapter(cmdStr, myConnection);
myAdapter.FillSchema(myTable, SchemaType.Source);
myTable.Columns[dbVariables.ClassesID].AutoIncrement = true;
myTable.Columns[dbVariables.ClassesID].AutoIncrementSeed = 1;
myTable.Columns[dbVariables.ClassesID].AutoIncrementStep = 1;
myAdapter.Fill(myTable);
myConnection.Close();
return myTable;
}
catch (SQLiteException e)
{
MessageBox.Show(e.ToString());
return null;
}
}
我在这里操作我的数据:
if (myResult == DialogResult.Yes)
{
//killTable.AcceptChanges();
DataRow[] dr = killTable.Select("" + cmVariables.ClassName + " = '" + cmbClasses.Text + "'");
//First I need to evaluate the row index of the row I want to delete
string indexName = (killTable.Rows.IndexOf(dr[0])).ToString();
int i = Int32.Parse(indexName);
// And we are done - I got my row index
DataRow modifiedRow = killTable.Rows[i];
killTable.Rows[i].Delete();
//I inserted this messagebox just to see the rowstatus - and yes, it is marked as deleted on runtime...
MessageBox.Show(killTable.Rows[i].RowState);
// I refer to this in the text below
killTable.AcceptChanges();
killClass_Execution(killTable, cmbClasses.Text, ShortClassNm);
}
以及至少将我的datatable更新回数据库的代码:
public void UpdateTable(string tableName, DataTable sourceTable, bool newOrEdit)
{
try
{
string connectionPath = dbVariables.ConnectionString;
//Connection erstellen --> der connectString gibt dabei den Pfad an.
SQLiteConnection myConnection = new SQLiteConnection(connectionPath);
myConnection.Open();
//Einen Befehls-String erstellen, der das UPDATE-Command auslöst
// UPDATE cm_ClassTest SET className = userEditInput WHERE className = 'oldClassName'
string myUpdateString = "SELECT * FROM " + tableName + "";
SQLiteDataAdapter myAdapter = new SQLiteDataAdapter(myUpdateString, myConnection);
SQLiteCommandBuilder comBuild = new SQLiteCommandBuilder(myAdapter);
myAdapter.DeleteCommand = comBuild.GetDeleteCommand(true);
myAdapter.UpdateCommand = comBuild.GetUpdateCommand(true);
myAdapter.InsertCommand = comBuild.GetInsertCommand(true);
myAdapter.Update(sourceTable);
myConnection.Close();
if (newOrEdit == true)
{
MessageBox.Show("Klasse erstellt!");
}
else
{
MessageBox.Show("Klasse aktualisiert!");
}
}
catch (SQLiteException e)
{
MessageBox.Show(e.ToString());
}
}
在用于操作数据的代码块中,可以找到AcceptChanges()
方法。此时,我的datatable可能没有出现任何其他更改,因此在应用程序启动后,删除一行可能是用户的第一个操作
另外:我的数据集中的每个条目都是唯一的(标有唯一类名的学校类)
任何帮助都将不胜感激
问候,
阿兰好吧——我终于成功了。调试没有显示任何问题,除了AcceptChanges()得到了调用,但没有导致实际删除dataRow
我现在所能想到的是,我处理它的主要方法是问题的一部分:我从数据库中获得正确的DataTable,使用它执行所有操作,然后通过Update将其发送回DB(请参见上面的Update方法)。SQLite CommandBuilder似乎无法解释当时标记为“Deleted”的我的DAtaRow,但在不引发异常的情况下,可以将其橡皮戳到方法的末尾
通过这样做,我尝试了GetChanges方法:
DataRow[] dr = killTable.Select("" + cmVariables.ClassName + " = '" + cmbClasses.Text + "'");
string indexName = (killTable.Rows.IndexOf(dr[0])).ToString();
int i = Int32.Parse(indexName);
killTable.Rows[i].Delete();
DataTable killItTable = killTable.GetChanges(DataRowState.Deleted);
killClass_Execution(killItTable, cmbClasses.Text, ShortClassNm);
现在它开始工作了——killTable的副本(killItTable——我知道这很愚蠢,但明天早上我要做的第一件事就是给它起个更好的名字:))为sql commandbuilder提供了信息,它似乎能识别。
不管它是什么,它现在起作用了
我认为这是不应该发生的——如果有人能就如何改进我的方法提出任何好的建议,我将很高兴。
向您致意那么,myAdapter.DeleteCommand看起来怎么样?@TaW-据我所知,myAdapter.DeleteCommand=comBuild.GetDeleteCommand(true)应该可以做到这一点吗?(在第三个代码块中,接近结尾处)-->我不确定这一点,但是尽管有这个命令,我是否需要一个特殊的delete命令?您可以查看它的属性吗?您应该可以在那里看到删除sql..我将在再次到达工作站后检查属性。我在逐步调试中检查了rowproperties-行在那里并标记为已删除。然而,AcceptChanges()似乎没有效果。你能在我的代码中找到任何关键行吗?我不是100%确定,如果我正确地得到了整个更新数据表的东西。。。谢谢你一直坚持下去!它认为改变更多的是为了输入新数据。您可以查看myAdapter.DeleteCommand属性以查看其sql。。