0

我试图弄清楚如何使用命令参数通过 SQL 语句传递值,因为连接根本不起作用,但我遇到了一个奇怪的问题,我找不到任何信息。

每当调用 SQL 语句时,参数的名称(例如 @Address1)就会被放入我的数据库中,而不是参数的值。

这是我的 SQL 语句、命令和连接代码:

Private Function qDBase()
    con.ConnectionString = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\pos408_w4_Team_B_Database.accdb;Persist Security Info=False" ' database should be located with the .exe file so that it can be detected and connected to
    Try 'Make sure we also wrap connection statement in try in case something fails there
        con.Open()

        Try ' Wrap each query in a try/catch so that if it fails it will not fail silently due to code in Button1_Click
            ' Always do Customer_Index first, the primary key here is a secondary key in Account_Index, via one-to-many relationship, and unless the corresponding value can be loaced in Customer_Index it will fail to insert the data
            qStr = "Insert Into `Customer_Index` Values('@Soc2','@FName','@MInitial','@LName','@DriverL','@Address1','@Address2','@City','@State','@Zip2','@Phone2','@Email');"


            ' Create and append parameters list
            com.Parameters.AddWithValue("Soc2", Soc2)
            com.Parameters.AddWithValue("FName", FName)
            com.Parameters.AddWithValue("MInitial", MInitial)
            com.Parameters.AddWithValue("LName", LName)
            com.Parameters.AddWithValue("DriverL", DriverL)
            com.Parameters.AddWithValue("Address1", Address1)
            com.Parameters.AddWithValue("Address2", Address2)
            com.Parameters.AddWithValue("City", City)
            com.Parameters.AddWithValue("State", State)
            com.Parameters.AddWithValue("Zip2", Zip2)
            com.Parameters.AddWithValue("Phone2", Phone2)
            com.Parameters.AddWithValue("Email", Email)




            com.CommandText = qStr ' Set command value to qStr
            com.Connection = con ' Set command connection
            com.ExecuteNonQuery() ' Execute the command

            qSuccesses += 1
        Catch ex As Exception
            MsgBox(ex) ' Show thrown exception to user
        End Try

        'Try
        'qStr = "Insert Into `Account_Index` Values('" & Account & "','" & Soc & "','" & AccountType & "');"

        'com.CommandText = qStr ' Set command value to qStr
        'com.ExecuteNonQuery() ' Execute the command

        '            qSuccesses += 1
        '       Catch ex As Exception
        '          MsgBox(ex) ' Show thrown exception to user
        '     End Try

    Catch ex As Exception
        MsgBox(ex) ' Show thrown exception to user
        Return False ' REturn prematurely because connection could not be made, no point going any further
    End Try

    If (qSuccesses = 1) Then ' If both try's work
        qSuccesses = 0 ' reset to zero
        Return True ' return true
    Else ' If one or both try's fail
        qSuccesses = 0 'reset to zero
        Return False ' return false
    End If
End Function

以及在表单加载时初始化的应该发送的值:

    Soc2 = "123-45-6789"
    FName = "Test"
    MInitial = "D"
    LName = "Test"
    DriverL = "Test"
    Address1 = "123 Test Rd"
    Address2 = " "
    City = "Denver"
    State = "CO"
    Zip2 = "12345-6789"
    Phone2 = "(123)456-7890"
    Email = "Test@Test.com"

然后是我的 Access 2010 数据库中的输出:

@Soc2 @FName @MInitial @LName @DriverL @Address1 @Address2 @City @State @Zip2 @Phone2 @Email

AddWithValue 是否不再正常工作?

4

1 回答 1

0

我讨厌这种情况发生,在发布问题后几分钟就解决问题。我不应该在 SQL 语句中使用撇号。只是决定尝试一下,而不用那些地狱般的东西,这次它工作得很好。

于 2013-04-13T09:53:38.477 回答