如何从VB永久删除数据库记录

时间:2013-07-16 07:52:37

标签: database vb.net visual-studio-2010 sql-server-2008

我是VB的初学者,我试图从数据库中删除一条记录,但它不让我... 他们给了我这个错误信息,我不完全明白这意味着什么...... 或者还有其他方法永久删除记录吗?

  

找不到名为userID的列。   参数名称:columnName

继承我的代码

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class frmUserManagement
    Dim countID As Integer = 0
    Dim conn As New SqlConnection
    Dim drEmployee As SqlClient.SqlDataReader
    Dim cmdEmployee As New SqlClient.SqlCommand

    Dim sAdapter As New SqlDataAdapter
    Dim sTable As New DataTable

    Private Sub btnAddEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddEmp.Click
        Dim tranEmployee As SqlClient.SqlTransaction
        sAdapter = New SqlDataAdapter(cmdEmployee)
        Dim strID As String
        Dim strName As String

        Dim strPosition As String
        Dim strContactNo As String
        Dim strAddress As String
        Dim strDOB As String
        Dim strGender As String
        Dim strSQL As String

        conn.Open()


        strID = lblEmpID.Text
        strName = txtEmpName.Text
        strPosition = cboEmpPosition.Text
        strContactNo = mskEmpDOB.Text
        strDOB = mskEmpDOB.Text
        strAddress = txtEmpAddress.Text
        If radEmpMale.Checked Then
            strGender = "Male"
        Else
            strGender = "Female"
        End If


        strSQL = "INSERT INTO Users(userID,userName,userPosition,userGender,userDOB,userAddress)" & _
            "VALUES(@ID,@NAME,@POSITION,@GENDER,@DOB,@ADDRESS)"

        tranEmployee = conn.BeginTransaction() 

        With cmdEmployee
            .Transaction = tranEmployee
            .CommandText = strSQL
            .Parameters.AddWithValue("@ID", strID)
            .Parameters.AddWithValue("@NAME", strName)
            .Parameters.AddWithValue("@POSITION", strPosition)
            .Parameters.AddWithValue("@GENDER", strGender)
            .Parameters.AddWithValue("@DOB", strDOB)
            .Parameters.AddWithValue("@ADDRESS", strAddress)
            .Connection = conn

        End With

        Try
            cmdEmployee.ExecuteNonQuery()
            tranEmployee.Commit()

        Catch ex As Exception
            tranEmployee.Rollback()
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try

    End Sub

    Private Sub frmUserManagement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If mintIndex <= 9000 Then
            lblEmpID.Text = "EMP" & (countID + 1).ToString("000#")
        End If

        Try
            With conn
                .ConnectionString = strConnection
                .Open()
            End With

            With cmdEmployee
                .CommandText = "SELECT * FROM Users ORDER BY userID"
                .Connection = conn
            End With
            drEmployee = cmdEmployee.ExecuteReader()

            If drEmployee.HasRows Then
                While drEmployee.Read()

                    DataGridView1.Rows.Add(drEmployee(0), drEmployee(3), drEmployee(1), drEmployee(4), drEmployee(2), drEmployee(5))


                End While
                drEmployee.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try
    End Sub

    Private Sub btnDeleteEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteEmp.Click
        With cmdEmployee
            .CommandText = "DELETE FROM Users WHERE userID = " & DataGridView1.CurrentRow.Cells(0).Value & ""
            .Connection = conn
            .Parameters.AddWithValue("@ID", 0)
        End With
        Dim rows = DataGridView1.SelectedRows
        For Each row In rows
            cmdEmployee.Parameters("@ID").Value = row.Cells("userID").Value
            cmdEmployee.ExecuteNonQuery()
        Next
           drEmployee = cmdEmployee.ExecuteReader()

    End Sub

    Private Sub btnEditEmp_Click(sender As System.Object, e As System.EventArgs) Handles btnEditEmp.Click
        lblEmpID.Enabled = False
        txtEmpName.Enabled = False
        grpGender.Enabled = False
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

这意味着您的表中没有名为userID的列,您确定它不仅仅是ID吗?

答案 1 :(得分:0)

您应该使用:

With cmdEmployee
    .CommandText = "DELETE FROM Users WHERE userID = @ID"
    .Connection = conn
    .Parameters.AddWithValue("@ID", 0)
End With
相关问题