gpt4 book ai didi

c# - 我怎样才能让这个函数删除多行

转载 作者:太空宇宙 更新时间:2023-11-03 12:43:04 26 4
gpt4 key购买 nike

我有一个 C# 函数和 SQL Server 存储过程,它在我的 gridview 中删除行。我怎样才能让它删除多个选定的行?

C# 函数:

public void DeleteReciver(int Reciver_number)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.Open();
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@Reciver_number", SqlDbType.Int);
param[0].Value = Reciver_number;
DAL.excutecommand("DeleteReciver", param);
DAL.close();
}

SQLSERVER 存储过程:

create proc DeleteReciver
@Reciver_number int
as
delete from Recivers where Reciver_number=@Reciver_number

使用

if (MessageBox.Show("Are you sure you want delete this reciver? ", "delete row", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
REC.DeleteReciver(Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[2].Value));
MessageBox.Show("Reciver deleted successfully", "delete row", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.dataGridView1.DataSource = REC.GET_ALL_RECIVERS();
}
else
{
MessageBox.Show("cancelled", "delete row", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

最佳答案

这对你有用

public void DeleteReciver(int[] Reciver_numbers)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.Open();
SqlParameter[] param = new SqlParameter[1];
for (int i = 0; i < Reciver_numbers.Length; i++)
{
param[0] = new SqlParameter("@Reciver_number", SqlDbType.Int);
param[0].Value = Reciver_number[i];
DAL.excutecommand("DeleteReciver", param);
}
DAL.close();

}

传递数组中的 Id 并使用 for look 执行删除命令。希望这会有所帮助。

关于c# - 我怎样才能让这个函数删除多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38232010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com