包含来自dll的vb6中的“Mysql Insert into”命令的函数

时间:2013-10-21 10:58:52

标签: mysql dll vb6 ado

我创建了一个在表格中添加记录的功能:

Public Function AjouterCleint _ 
(ByVal pcode As String, ByVal prsoc As String, ByVal padresse As String, ByVal pcp As String) As Boolean


    On Error GoTo ErrorHandler
    MsgBox " code  " & "   : " & pcode & " / rsoc : " & prsoc & " / adresse : " & padresse & " /  cp : " & pcp
    AjouterCleint = False
    Dim rs As New Recordset
    Set rs = New Recordset
    Dim conn As ADODB.Connection
    Dim strIPAddress As String
    Set con = New ADODB.Connection
    con.CursorLocation = adUseClient
    con.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
    & "SERVER=LOCALHOST;" _
    & " DATABASE=ste002;" _
    & "UID=root;PWD=; OPTION=3"
    con.Open
    Set rs = Nothing  
    rs.CursorLocation = adUseClient  
    SQL = "INSERT INTO Client (code,rsoc,adresse,cp) VALUES ('" _
    & pcode & "','" & prsoc & "','" & padresse & "','" & pcp & "')"
    MsgBox "5"
    rs.Open SQL, con, 3, 3
    MsgBox "6"
    Set rs = Nothing
    MsgBox "7"
    con.Close
    MsgBox "8"
    Set con = Nothing
    MsgBox "9"
    AjouterCleint = True
    ErrorHandler:
    MsgBox Err.Number & vbLf & Err.Description & vbLf & Err.HelpContext & vbLf & Err.Source, , ""
 End Function

参数的值不是我使用的那些。 (我进入消息框:

 code: ????, rsoc: ????, adresse :???? , cp :1

当我以普通方式使用此功能时(我将其添加到模块中,我将其称为:

 a = AjouterCleint("13234", "1234", "1234", "1234")

它可以工作,但是当我把它放在一个DLL中时,会出现错误:

    2147217900
    [MySQL][ODBC 3.51 DRIVER] ... SYNTHAX error in “???????????????” in line 1.
    1000440
    Microsoft OLE DB providerfor ODBC drivers

例外是在这一行:

    rs.Open SQL, con, 3, 3

我删除了INSET COMMAND中的所有参数,并替换了

    SQL = "INSERT INTO Client (code,rsoc,adresse,cp) VALUES ('" _
    & pcode & "','" & prsoc & "','" & padresse & "','" & pcp & "')"

通过

    SQL = "INSERT INTO Client (code,rsoc,adresse,cp) VALUES (12,12,12,12)"

它在dll中工作(我在其他项目中调用它并且它有效) 但我必须使用参数 所以,任何建议!!
非常感谢。
PS:“recordset.addnew”也不起作用,也引起了错误。

这就是我如何调用dll中的函数:

Private Declare Function FunctionCalled Lib "C:\dlls\vbm2dll\Called.dll" _
(ByVal strValuePassed As String) As String

Private Declare Function AjouterCleint Lib "C:\dlls\vbm2dll\Called.dll" _
 (ByVal pcode As String, ByVal prsoc As String, ByVal padresse As String, ByVal pcp As String) As Boolean
Private Sub Form_load()

    txbValuePassed = "abc"

End Sub
Private Sub cmdCall_Click()
    txbValueReturned = FunctionCalled(txbValuePassed)
    a = AjouterCleint("1104", "1", "1", "1")
    MsgBox a & ""

End Sub

1 个答案:

答案 0 :(得分:0)

它可能与你如何将参数传递给DLL有关。您显示的代码未指定ByRefByVal,因此它使用默认值,我认为在VB6中为ByRef,但您可能没有通过该地址。这样:

 Public Function AjouterCleint(ByVal pcode As String, _
                      ByVal prsoc As String, _
                      ByVal padresse As String, _
                      ByVal pcp As String) As Boolean

可以解决问题。

修改

您的应用代码还需要与该声明相匹配,告诉VB如何传递变量:

 [Private|Public] Declare Function AjouterCleint Lib "MyLibName.dll" [Alias "AliasName"] _
         (ByVal pcode As String, _
          ByVal prsoc As String, _
          ByVal padresse As String, _
          ByVal pcp As String) As Boolean

由于BSTR基本上仍然是一个指针,你可能不得不复制字符串数据,虽然看起来它应该适用于VB中的双方):

Private Declare Sub RtlMoveMemory Lib "kernel32" (ptrDst As Any, _
       ptrSrc As Any, ByVal lLen As Long)

 Private Function vbstr(ptr As Long) As String
    Dim l As Long

    l = lstrlenW(ptr)
    vbstr = String$(l, vbNullChar)

    Call RtlMoveMemory(ByVal StrPtr(vbstr), ByVal ptr, l * 2)

 End Function