我需要帮助从列表框中删除帮助。如何在列表框中选择项目并单击删除按钮,如何在sql数据库中删除此项目?
以下是数据从数据库中读取的位置:
DataTable aucTable = new DataTable();
SqlConnection conn = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=AuctionBase;Integrated Security=True");
SqlDataAdapter aucDa = new SqlDataAdapter("SELECT * FROM Auction", conn);
aucDa.Fill(aucTable);
listBox1.DataContext = aucTable;
查看数据的xaml代码:
GridView ColumnHeaderToolTip="Auction"
GridViewColumn DisplayMemberBinding="{Binding Path=Product}" Header="Product" Width="150"
GridViewColumn DisplayMemberBinding="{Binding Path=StartingPrice}" Header="Starting Price" Width="120"
GridViewColumn DisplayMemberBinding="{Binding Path=CurrentPrice}" Header="Current Price" Width="120"
</GridView>
数据库:
CREATE TABLE Auction
(
AuctionID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Product nvarchar(20) NOT NULL,
StartingPrice int NOT NULL,
CurrentPrice int NOT NULL
);
删除按钮:
if(listBox1.SelectedIndex < 0)
{
return;
}
else
{
SqlConnection conn = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=AuctionBase;Integrated Security=True");
SqlDataAdapter aucDel = new SqlDataAdapter();
SqlCommand delete = new SqlCommand("DELETE FROM Auction WHERE AuctionID = @AuctionID", conn);
try
{
conn.Open();
delete.EndExecuteNonQuery();
conn.Close();
}
}
我错了什么,解决方案是什么?我希望在单击删除按钮时,应删除ListBox中的选定项目。