gpt4 book ai didi

mysql - 包含从 dll 调用的 vb6 中的 "Mysql Insert into"命令的函数

转载 作者:行者123 更新时间:2023-11-30 01:10:32 24 4
gpt4 key购买 nike

我制作了一个在表中添加记录的函数:

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

异常(exception)在这一行:

    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

最佳答案

这可能与您将参数传递给 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

关于mysql - 包含从 dll 调用的 vb6 中的 "Mysql Insert into"命令的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19492679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com