0

我已经编写代码来删除gridview 中的多条记录,但它只删除gridview 中的一条记录。有人可以帮我解决这个问题。这是我的代码

 'This is to Delete multiple records in gridview
Private Sub DeleteMultipleRecords(ByVal idCollection As StringCollection)
    'Create sql Connection and Sql Command
    Dim cnnOLEDB As New OleDbConnection(strConnectionString)
    Dim cmd As New OleDbCommand()
    Dim IDs As String = ""

    'Create string builder to store 
    'delete commands separated by ;

    For Each id As Integer In idCollection
        IDs += id & ","
    Next
    Try
        Dim test As Integer = IDs
        Dim sql As String = "DELETE FROM [1BK] WHERE [sampleID] in (" & test & ")"
        Dim ANS As Integer
        ANS = MsgBox("Are you sure want to delete selected record ?" & vbCrLf & vbCrLf, MsgBoxStyle.YesNo)
        If ANS = vbYes Then
            cmd.CommandType = CommandType.Text
            cmd.CommandText = sql
            cmd.Connection = cnnOLEDB
            cnnOLEDB.Open()
            cmd.ExecuteNonQuery()
        End If
        ANS = 0
    Catch ex As Exception
        Dim errorMsg As String = "Error in Deletion"
        errorMsg += ex.Message
        Throw New Exception(errorMsg)
    Finally
        cnnOLEDB.Close()
    End Try
End Sub
4

3 回答 3

0

你的代码有问题。

//Dim test As Integer
Dim test As String

test 是一个字符串值,而不是整数。

当你完成

IDs += id & ","

最后一个“,”应该被删除。

因此,您应该在完成循环后再添加一行代码。

IDs = IDs.TrimEnd(',')
于 2012-08-08T05:49:54.137 回答
0

使用下面的代码

IDs+= "'"&id &"'" & ","

代替

IDs += id & ","
于 2012-08-08T04:58:40.583 回答
0

测试变量应该是 a stringas you are usingIN子句。例如

string test="'1','2'"
if(test=="")
 test = "'" & id & "'"
else
 test += "," & "'" & id & "'"

然后查询将起作用

于 2012-08-08T04:14:38.237 回答