VC++ 连接SQL Server数据库(远程+本地)

时间:2021-12-08 13:38:35
连接远程数据库 SQLSERVER2005
CString strConn =  _T("driver={SQL Server};Server=192.168.57.49,1433;DATABASE=数据名;UID=用户名;PWD=密码");
(Driver={SQL Native Client}是连接本地数据库字符串,连接远程数据库会出错)
连接本地数据库
SQLSERVER2005
CString strConnServer2005 = _T("Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=DATABASENAME;Data Source=127.0.0.1");
SQLSERVER2008
CString strConnServer2008 = _T("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DATABASENAME;Data Source=.\\SQLEXPRESS;User ID=用户名;Password=密码;");
测试通过(
.\\SQLEXPRESS 转义\看情况,因为这里是放在字符串中所以需要
)
 m_pConnection->Open(_bstr_t(strConn),_bstr_t(strUserId),_bstr_t(strPassword),adModeUnknown);