从VB6连接到数据库的错误-2147467259

时间:2011-06-24 12:23:46

标签: mysql vb6 windows-xp

我目前正在开发一个需要连接数据库的小应用程序。我使用以下代码执行此操作...

Set database_connection = New ADODB.Connection
database_connection.ConnectionString = _
 "Driver={MySQL ODBC 3.51 Driver}; Server=HOST; " & _
                              "Database=SCHEMA; " & _
                              "User=USER; " & _
                              "Password=PASSWORD; " & _
                              "Option=3;"
database_connection.Open

当我从IDE运行项目时,这工作正常,当我从命令行运行编译的exe时,它工作的文件。但是,如果我尝试通过调用CreateProcess函数来运行exe,它根本不起作用,产生以下错误消息......

(1) Error#: -2147467259
Desc. : Unspecified error
Source: Provider
Native Error: -2147467259
SQL State: 
Help Context: 1240640
Help File:

有谁知道我应该怎么做?我正在使用Windows XP,CreateProcess调用看起来像......

Dim create_result As Long
Dim startup_information As STARTUPINFO
Dim our_process_information As PROCESS_INFORMATION
Dim process_attributes As SECURITY_ATTRIBUTES
Dim thread_attributes As SECURITY_ATTRIBUTES

create_result = CreateProcess(vbNullString, _
                              command_line, _
                              process_attributes, _
                              thread_attributes, _
                              0, _
                              0, _
                              0, _
                              vbNullString, _
                              startup_information, _
                              our_process_information)

(我怀疑存在权限问题,但如果是,则不知道如何处理。服务器日志中没有任何内容。)

1 个答案:

答案 0 :(得分:2)

嗯,编码风格看起来非常熟悉......

人们通常犯的一个错误是使用来自 API Viewer 等来源的“库存”Declare签名,而不理解它们的含义,例如:

Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" ( _
    ByVal lpApplicationName As String, _
    ByVal lpCommandLine As String, _
    lpProcessAttributes As SECURITY_ATTRIBUTES, _
    lpThreadAttributes As SECURITY_ATTRIBUTES, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    lpEnvironment As Any, _
    ByVal lpCurrentDriectory As String, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long

这些As StringAs Any声明会产生人们经常忽略的影响。

通常,使用“W”入口点并将所有指针声明为ByVal xxx As Long,然后在需要时应用VB6指针函数,会更好。但是你可以带马到水......

试试这个:

create_result = CreateProcess(vbNullString, _
                              command_line, _
                              process_attributes, _
                              thread_attributes, _
                              0, _
                              0, _
                              ByVal 0&, _
                              vbNullString, _
                              startup_information, _
                              our_process_information)

或者远远好一点,尝试:

Private Declare Function CreateProcessW Lib "kernel32" ( _
    ByVal lpApplicationName As Long, _
    ByVal lpCommandLine As Long, _
    ByVal lpProcessAttributes As Long, _
    ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, _
    ByVal lpCurrentDriectory As Long, _
    ByVal lpStartupInfo As Long, _
    ByVal lpProcessInformation As Long) As Long

Dim create_result As Long
Dim startup_information As STARTUPINFO
Dim our_process_information As PROCESS_INFORMATION

create_result = CreateProcessW(0, _
                               StrPtr(command_line), _
                               0, _
                               0, _
                               0, _
                               0, _
                               0, _
                               0, _
                               VarPtr(startup_information), _
                               VarPtr(our_process_information))

我的猜测是,这正是你“破坏”你的ODBC驱动程序的地方(遗憾的是OLEDB提供商要好得多,但我不知道MySQL的免费版本。)

哦,问题(和这里的区别)?

您正在将环境块切换为空,即可能破坏PATH会使您的ODBC驱动程序无法正常工作。

空指针,零和空结构完全不同。