在vb.net中删除记录

时间:2014-02-07 01:06:03

标签: vb.net gridview

我的删除命令有问题。没有错误,但是当我运行时,查询说:

  

将varchar值'System.Data.Linq.SqlClient.SqlProvider + SingleResult`1 [WindowsApplication1.StoredProcedure3Result]'转换为数据类型int时,转换失败。*

这是我的代码:

Dim conn As New SqlConnection(
  "server=IAI-004;uid=admin;pwd=intelassist;database=payroll")
Dim cmd As SqlCommand = conn.CreateCommand
conn.Open()

cmd.CommandText = "DELETE FROM employee_info WHERE employee_id ='" &
                  DataGridView1.DataSource.ToString & "'"
cmd.ExecuteNonQuery()

conn.Close()

我认为问题是:

employee_id ='" & DataGridView1.DataSource.ToString & "'"

这是一个字符串。我的employee_id数据类型是Integer。

我需要转换吗?如果没有,那么可能的解决办法是什么?

2 个答案:

答案 0 :(得分:0)

取出单引号:

另外,DataGridView1.DataSource.ToString不会让你获得employee_id

cmd.CommandText = "DELETE FROM employee_info WHERE employee_id =" &  DataGridView1.DataSource.ToString

答案 1 :(得分:0)

声明特定行然后循环以进行删除查询。

    Dim Emp_ID As String
    For i As Integer = 0 To DataGridView1.Rows.Count - 1
     Emp_ID = DataGridView1.Rows(i).Cells(0).Value()   


    query = "DELETE FROM employee_info WHERE employee_id = '" + Emp_ID + "'"


            mysql_connect(True)
            cmd = New MySqlCommand(query, SQLConnection)
            cmd.Connection = SQLConnection
            cmd.CommandText = query
            cmd.ExecuteNonQuery()


Next
  MsgBox("Deleted")