CString strConn = "SERVER=192.168.0.100;DATABASE=db_name;UID=sa;PWD=sa_pwd;DRIVER={Sql Server Native Client 11.0};";
CDatabase db;
try
{
cout << "开始连接数据库...";
db.SetLoginTimeout(30);
db.OpenEx(g_strSqlConnStr, CDatabase::noOdbcDialog); //forceOdbcDialog
db.SetQueryTimeout(60);
cout << "连接数据库成功...";
}
catch (CDBException *e)
{
CString strErrMsg;
char buffer[1024];
e->GetErrorMessage(buffer, 1024);
strErrMsg.Format("连接数据库失败,原因:%s", buffer);
db.Close();
e->Delete();
return false;
}
//里面的SERVER要填写IP名称,不需要填写ODBC的配置名称