VB 2008中的数据类型不匹配

时间:2012-04-28 21:58:53

标签: vb.net visual-studio-2008 ms-access ado.net oledb

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

如何将名为radActiveradInactive的单选按钮的值存储在数据库中?我应该使用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

谢谢!

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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

请理解,这并不是为了避免使用参数。我只是想澄清你为什么会收到这个错误。