2

大家好,我有一个问题。当我尝试在我的程序中使用这段将供应商添加到数据库中的代码时,我遇到了数据类型不匹配错误。据我所知,供应商的状态造成了错误。

我将如何在数据库中存储名为radActive和的单选按钮的值radInactive?我应该使用Boolean还是String? 我使用 Microsoft Access 作为我的数据库,并且字段Status设置为Yes/No.

这是代码。

Public Sub SupplierInsertData()

    Dim conn As OleDb.OleDbConnection
    Dim cmd As OleDb.OleDbCommand

    Dim SupplierType As String
    Dim Status As Boolean

    'Check for supplier type
    If frmDatabaseSupplier.radLocal.Checked = True Then

        SupplierType = "Local"

    ElseIf frmDatabaseSupplier.radForeign.Checked = True Then

        SupplierType = "Foreign"

    End If

    'Check for supplier status
    If frmDatabaseSupplier.radActive.Checked = True Then

        Status = True

    ElseIf frmDatabaseSupplier.radInactive.Checked = True Then

        Status = False

    End If

    'For inserting of data in the database.
    Dim cmdString As String = "INSERT INTO Supplier(SupplierLastName, SupplierFirstName, SupplierMiddleInitial, " & _
                              "SupplierCompany, SupplierType, SupplierStreetAddress, SupplierCity, SupplierContactNumber, SupplierEmail, " & _
                              "Status)" & _
                              "VALUES('" & frmDatabaseSupplier.txtSupplierLastName.Text & "','" & frmDatabaseSupplier.txtSupplierFirstName.Text & "','" & frmDatabaseSupplier.txtSupplierMiddleInitial.Text & "','" _
                              & frmDatabaseSupplier.txtSupplierCompany.Text & "','" & SupplierType & "', '" & frmDatabaseSupplier.txtSupplierStreetAddress.Text & "','" & frmDatabaseSupplier.txtSupplierCity.Text & "','" _
                              & frmDatabaseSupplier.txtSupplierContactNumber.Text & "','" & frmDatabaseSupplier.txtSupplierEmail.Text & "','" & Status & "')"

    conn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ProjectAnalysisSystem.accdb")

    cmd = New OleDb.OleDbCommand(cmdString, conn)

    conn.Open()
    cmd.ExecuteNonQuery()
    conn.Close()

End Sub

谢谢!

4

2 回答 2

2

尝试更改代码的最后一部分。
您应该使用参数将文本框或变量的值传递给数据库引擎。
并且不要忘记将 OleDbConnection 等一次性对象封装在using语句中。

Dim cmdString As String = "INSERT INTO Supplier(SupplierLastName, SupplierFirstName, SupplierMiddleInitial, " & _ 
                          "SupplierCompany, SupplierType, SupplierStreetAddress, SupplierCity, SupplierContactNumber, SupplierEmail, " & _ 
                          "Status)" & _ 
                          "VALUES(@supplierName, @supplierFirst, @supplierMiddle, @supplierCo, @supplierType, @supplierStreet, @supplierCity, " & _
                          "@supplierContact, @supplierMail, @status)"
Using conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ProjectAnalysisSystem.accdb")
    Dim cmd As OleDbCommand = New OleDb.OleDbCommand(cmdString, conn))
    cmd.Parameters.AddWithValue("@supplierName", frmDatabaseSupplier.txtSupplierLastName.Text)
    cmd.Parameters.AddWithValue("@supplierFirst", frmDatabaseSupplier.txtSupplierFirstName.Text)
    cmd.Parameters.AddWithValue("@supplierMiddle", frmDatabaseSupplier.txtSupplierMiddleInitial.Text)
    cmd.Parameters.AddWithValue("@supplierCo", frmDatabaseSupplier.txtSupplierCompany.Text )
    cmd.Parameters.AddWithValue("@supplierType", SupplierType)
    cmd.Parameters.AddWithValue("@supplierStreet", frmDatabaseSupplier.txtSupplierStreetAddress.Text)
    cmd.Parameters.AddWithValue("@supplierCity", frmDatabaseSupplier.txtSupplierCity.Text)
    cmd.Parameters.AddWithValue("@supplierContact", frmDatabaseSupplier.txtSupplierContactNumber.Text)
    cmd.Parameters.AddWithValue("@supplierMail", frmDatabaseSupplier.txtSupplierEmail.Text)
    cmd.Parameters.AddWithValue("@status", Status) '<- Here the status var is correctly identified as a boolean, not as a string
    conn.Open() 
    cmd.ExecuteNonQuery() 
End Using
于 2012-04-28T22:55:11.473 回答
1

您遇到数据类型不匹配错误,因为您INSERT statement尝试为Status数据类型为“是/否”的字段存储字符串值。

这不是真正的 VB.Net 问题。如果您在 VBA 中尝试相同的操作,您会从 Access 的数据库引擎中得到相同的错误。这是下面 VBA 过程的输出。

INSERT INTO Supplier (Status)
VALUES('True')
Error -2147217913 (Data type mismatch in criteria expression.)

步骤 ...

Public Sub Ju_chan()
    Dim cmdString As String
    Dim Status As Boolean
    Dim strMsg As String

On Error GoTo ErrorHandler

    Status = True
    cmdString = "INSERT INTO Supplier (Status)" & vbCrLf & _
        "VALUES('" & Status & "')"
    Debug.Print cmdString
    CurrentProject.Connection.Execute cmdString

ExitHere:
    On Error GoTo 0
    Exit Sub

ErrorHandler:
    strMsg = "Error " & Err.Number & " (" & Err.Description _
        & ")"
    Debug.Print strMsg
    GoTo ExitHere
End Sub

请理解,这并不是要让您远离使用参数。我只是想澄清你为什么会收到这个错误。

于 2012-04-28T23:13:19.183 回答