0

我是 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
4

2 回答 2

0

你应该使用:

With cmdEmployee
    .CommandText = "DELETE FROM Users WHERE userID = @ID"
    .Connection = conn
    .Parameters.AddWithValue("@ID", 0)
End With
于 2013-07-16T08:05:37.107 回答
0

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

于 2013-07-16T07:59:30.817 回答