我尝试从我的数据库中删除列表'ship'的列表项目。我使用2堂课将我的SQL查询分开(用于查询的shipdb.cs,用于我的船舶物业(。我想使用表单中的"删除"按钮在数据库中删除所选行。该按钮未删除行,单击删除按钮时未提及任何错误。
在shipdb.cs中:
public void Delete()
{
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
{
connection.Open();
Ship ship = new Ship();
SqlCommand commandDelete = new SqlCommand("DELETE FROM Ship WHERE id = " + ship.Id, connection);
commandDelete.ExecuteNonQuery();
}
}
在form1.cs中:
private void Button3_Click(object sender, EventArgs e)
{
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
{
if (dataGridViewShips.SelectedRows.Count == 1)
{
Ship ship = (Ship)dataGridViewShips.SelectedRows[0].DataBoundItem;
db.Delete();
dataGridViewShips.DataSource = db.GetAll();
}
}
}
我使用db.getall更新数据库
正如萨米(Sami(所说的,您没有告诉它需要删除的内容。
public void Delete(Ship ship)
{
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
{
connection.Open();
SqlCommand commandDelete = new SqlCommand("DELETE FROM Ship WHERE id = " + ship.Id, connection);
commandDelete.ExecuteNonQuery();
}
}
private void Button3_Click(object sender, EventArgs e)
{
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
{
if (dataGridViewShips.SelectedRows.Count == 1)
{
Ship ship = (Ship)dataGridViewShips.SelectedRows[0].DataBoundItem;
db.Delete(ship);
dataGridViewShips.DataSource = db.GetAll();
}
}
}