Public Function MySqlConnectToCn() As Boolean Dim i As Single Dim m As Long
On Error GoTo ERR_CONN
Set MySQLCn = New Connection '连接到数据库
With MySQLCn .CommandTimeout = 10 .ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _ & "SERVER=" & MySQLServer & ";" _ & "DATABASE=punchBJ;" _ & "UID=pun;PWD=punch; OPTION=3" .Open End With
MySqlConnectToCn = True Exit Function ERR_CONN: MySqlConnectToCn = False Set MySQLCn = Nothing If err.Number = -2147467259 Then If err.Description = "[MySQL][ODBC 3.51 Driver]Can't connect to MySQL server on '" & MySQLServer & "' (10061)" Then MsgBox "无法连接本地子服务器主机" & Chr(13) & err.Description, vbExclamation, "连接错误" Call ExitApp
ElseIf err.Description = "[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序" Then i = MsgBox("无法获取MySQL驱动信息,是否下载安装?", vbYesNo + vbDefaultButton2 + vbExclamation, "MySQL驱动") If i = vbYes Then If Dir("D:\pls\bin\MyODBC-3.51.06.exe", vbDirectory) = "" Then FileCopy "\\192.168.0.3\punch\NewExe\plsbinBak\DLL\MyODBC-3.51.06.exe", "D:\pls\bin\MyODBC-3.51.06.exe" End If m = ShellExecute(FrmBJDLR.hwnd, vbNullString, "D:\pls\bin\MyODBC-3.51.06.exe", "D:\pls\bin\MyODBC-3.51.06.exe", "D:\pls\bin\MyODBC-3.51.06.exe", 1) If m <> 0 Then Call ExitApp Else MsgBox "安装驱动失败,与信息管理部联络", vbExclamation, "安装驱动" Call ExitApp End If Else Call ExitApp End If Else MsgBox "MySQL服务器通讯错误" & err.Number & Chr(13) & err.Description, vbExclamation, "MySQL服务器错误" Call ExitApp End If
Else err.Raise err.Number, err.Source, err.Description End If End Function
http://www.vckbase.com/code/listcode.asp?mclsid=11&sclsid=1121
Dim i As Single
Dim m As Long
On Error GoTo ERR_CONN
Set MySQLCn = New Connection
'连接到数据库
With MySQLCn
.CommandTimeout = 10
.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
& "SERVER=" & MySQLServer & ";" _
& "DATABASE=punchBJ;" _
& "UID=pun;PWD=punch; OPTION=3"
.Open
End With
MySqlConnectToCn = True
Exit Function
ERR_CONN:
MySqlConnectToCn = False
Set MySQLCn = Nothing
If err.Number = -2147467259 Then
If err.Description = "[MySQL][ODBC 3.51 Driver]Can't connect to MySQL server on '" & MySQLServer & "' (10061)" Then
MsgBox "无法连接本地子服务器主机" & Chr(13) & err.Description, vbExclamation, "连接错误"
Call ExitApp
ElseIf err.Description = "[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序" Then
i = MsgBox("无法获取MySQL驱动信息,是否下载安装?", vbYesNo + vbDefaultButton2 + vbExclamation, "MySQL驱动")
If i = vbYes Then
If Dir("D:\pls\bin\MyODBC-3.51.06.exe", vbDirectory) = "" Then
FileCopy "\\192.168.0.3\punch\NewExe\plsbinBak\DLL\MyODBC-3.51.06.exe", "D:\pls\bin\MyODBC-3.51.06.exe"
End If
m = ShellExecute(FrmBJDLR.hwnd, vbNullString, "D:\pls\bin\MyODBC-3.51.06.exe", "D:\pls\bin\MyODBC-3.51.06.exe", "D:\pls\bin\MyODBC-3.51.06.exe", 1)
If m <> 0 Then
Call ExitApp
Else
MsgBox "安装驱动失败,与信息管理部联络", vbExclamation, "安装驱动"
Call ExitApp
End If
Else
Call ExitApp
End If
Else
MsgBox "MySQL服务器通讯错误" & err.Number & Chr(13) & err.Description, vbExclamation, "MySQL服务器错误"
Call ExitApp
End If
Else
err.Raise err.Number, err.Source, err.Description
End If
End Function
Database_Param m_dbParam; m_dbParam.host = m_ip;//ip地址
m_dbParam.user = "root";
m_dbParam.password = "";
m_dbParam.db = "ruixingzj";//数据库
m_dbParam.port = 0;
m_dbParam.unix_socket = NULL;
m_dbParam.client_flag = 0; m_db.ConnectDB (&m_dbParam);
//以上就是连接的程序
/*要包含mysql.h和libmysql.lib*/
MYSQL *myconn;
if(!mysql_real_connect(myconn, szIpaddr, root , szPassword, tiki,0,NULL,0))
{
szTemp.Format("%s",mysql_error(myconn));
AfxMessageBox(szTemp);
return false;
}
else
{
return true;
}
{
if (!mysql_real_connect(&mysql, p->host, p->user, p->password, p->db, p->port, p->unix_socket, p->client_flag))
{
OutErrors();
return FALSE;
}
return TRUE;
}
这就是连接的代码