如何使此功能删除多行

时间:2016-07-06 19:12:58

标签: c# sql-server winforms ado.net

我的网格视图中有一个C#函数和SQL服务器存储过程的删除行。如何删除多个选定的行?

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);

}

3 个答案:

答案 0 :(得分:1)

这对你有用

std::make_shared<std::condition_variable>(std::condition_variable{})

将Id传递给数组并用于查看执行delete命令。希望这会有所帮助。

答案 1 :(得分:0)

为什么不运行循环来收集逗号分隔字符串中的所有接收器编号,并在存储过程中删除它们,而不是传入一个Reciver_number。

create proc DeleteRecivers
@Reciver_numbers varchar(50)
as
delete from Recivers where Reciver_number in (@Reciver_numbers)

答案 2 :(得分:0)

您可以在SQL中通过发送varchar parm并像这样生成

create proc DeleteReciver
@Reciver_number int=null
@Reciver_numbers varchar(500)=null
as
IF isnull(@Reciver_number,0) <> 0 
   delete from Recivers where Reciver_number=@Reciver_number
ELSE 
   IF isnull(@Reciver_numbers,'') <> '' 
       exec ('delete from Recivers where Reciver_number in (' + @Reciver_numbers +')')

在C#上,您将以此格式发送要删除的所有数字的字符串12,435,234,6565,34