我有一个表格可以访问,与OleDBConnection和Datagridview1组件连接。 有这样的代码:
if (this.dataGridView1.SelectedRows.Count > 0)
{
string queryString = "SELECT movieID, Title, MovieYear, Country,Located, Description, Poster, Actors, FilmDirector, Type FROM Movie,movieType WHERE movietype.typeID = Movie.typeID";
foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
{
int currentRow = int.Parse(dataGridView1.CurrentCell.RowIndex.ToString());
try
{
string movieIDString = dataGridView1[0, currentRow].Value.ToString();
movieIDInt = int.Parse(movieIDString);
string queryDeleteString = "DELETE FROM Movie where movieID = " + movieIDInt + ";";
OleDbCommand sqlDelete = new OleDbCommand();
sqlDelete.CommandText = queryDeleteString;
sqlDelete.Connection = database;
sqlDelete.ExecuteNonQuery();
loadDataGrid(queryString);
}
catch (Exception ex) { }
}
}
它必须删除选定的多行,但不能:(
我的错误在哪里,你能帮助我吗?
答案 0 :(得分:1)
我认为,加载DataGrid(查询字符串)应该在for循环之外。这只是猜测不确定。
if (this.dataGridView1.SelectedRows.Count > 0)
{
string queryString = "SELECT movieID, Title, MovieYear, Country,Located, Description, Poster, Actors, FilmDirector, Type FROM Movie,movieType WHERE movietype.typeID = Movie.typeID";
foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
{
int currentRow = int.Parse(dataGridView1.CurrentCell.RowIndex.ToString());
try
{
string movieIDString = dataGridView1[0, currentRow].Value.ToString();
movieIDInt = int.Parse(movieIDString);
string queryDeleteString = "DELETE FROM Movie where movieID = " + movieIDInt + ";";
OleDbCommand sqlDelete = new OleDbCommand();
sqlDelete.CommandText = queryDeleteString;
sqlDelete.Connection = database;
sqlDelete.ExecuteNonQuery();
}
catch (Exception ex) { }
}
loadDataGrid(queryString);
}