2016-07-06 38 views
2

我有一個C#函數和SQL服務器存儲過程它的刪除行在我的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存儲過程:使用

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

} 
+0

您可以更改'DeleteReciver'的簽名以接受'List '作爲參數,然後在該方法中,在for/foreach循環中打開連接後,調用存儲過程以傳遞值來刪除。 –

+0

也作爲另一種選擇,您可以使用[Table-Valued參數](https://msdn.microsoft.com/en-us/library/bb510489.aspx)。例如,看看這篇文章:[C#SQL Server - 將列表傳遞給存儲過程](http://stackoverflow.com/questions/7097079/c-sharp-sql-server-passing-a-list-to -a-stored-procedure) –

+0

如果DataGridView的'DataSource'是'DataTable',則可以使用'DataAdapter'來保存對數據庫的更改。這樣你就不需要使用該存儲過程。有足夠的'SELECT * FROM Recivers'命令。然後可以自動創建所有其他必需的命令,並且所有更改(包括刪除的記錄)都可以簡單地應用於數據庫例如,看看這篇文章:[使用DataGridView,DataTable和TableAdapter的CRUD操作](http://stackoverflow.com/a/36274706/3110834) –

回答

0

除了傳遞一個Reciver_number的

create proc DeleteReciver 
@Reciver_number int 
as 
delete from Recivers where [email protected]_number 

,爲什麼不跑一個循環來收集所有的接收器用逗號分隔字符串中的數字並在存儲過程中刪除它們。

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

因爲你不能以這種方式參數化IN子句 – Steve

0

您可以通過發送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 [email protected]_number 
ELSE 
    IF isnull(@Reciver_numbers,'') <> '' 
     exec ('delete from Recivers where Reciver_number in (' + @Reciver_numbers +')') 

而且在C#您發送所有你想要以這種格式刪號的字符串SQL 12,435,234,6565,34

+1

再次, IN子句不能以這種方式參數化。這裏的參考問題http://stackoverflow.com/questions/337704/parameterize-an-sql-in-clause – Steve

+0

Thank's Steve,我已經改變它的執行('從Recivers刪除,其中Reciver_number在('+ @Reciver_numbers + ')') –

1

這會爲你工作

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

    } 

傳遞標識的陣列和使用的外觀執行刪除COMM和。希望這可以幫助。