我用ACCESS连数据库的时候
用打开对话框选择数据库,可以构造这样的连接字符串
strFileName=dlgOpen.GetPathName();
CString strLink;
strLink="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+strFileName+";";可是用SQL SERVER的时候,我就不知道怎么写了,呵呵
笨了
请各位请教!
用打开对话框选择数据库,可以构造这样的连接字符串
strFileName=dlgOpen.GetPathName();
CString strLink;
strLink="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+strFileName+";";可是用SQL SERVER的时候,我就不知道怎么写了,呵呵
笨了
请各位请教!
CString dbname; //默认的数据库名
CString user; //用户名
CString pass; //密码
CString strConnect = _T("Provider=SQLOLEDB.1; Data Source=") + dbsrc +
_T("; Initial Catalog=") + dbname +
_T("; User ID=") + user +
_T("; PWD=") + pass;
strConnt = "Provider = sqloledb; Data Source = JIAHF(你的服务器) ;Integrated Security = SSPI; Connect Timeout = 30; Initial Catalog = YOUDB()你的数据库";
_ConnectionPtr pConnection;
// open connection and record set
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
hr = pConnection->Open(
_bstr_t(strConnt),
_bstr_t(L""),
_bstr_t(L""),
adModeUnknown);
"Initial Catalog=MyDatabaseName;"
"User Id=MyUsername;Password=MyPassword;"); For Trusted Connection security: (Microsoft Windows NT integrated security):strConnect = _T("Provider=sqloledb;Data Source=MyServerName;"
"Initial Catalog=MyDatabaseName;"
"Integrated Security=SSPI;");If you want to connect ta a "Named Instance" (SQL Server 2000) you must to specifiy Data Source=Servere Name\Instance Name like in the following example:strConnect = _T("Provider=sqloledb;Data Source=MyServerName\MyInstanceName;"
"Initial Catalog=MyDatabaseName;User Id=MyUsername;Password=MyPassword;");
If you want To connect with a SQL Server running on the same computer, you must to specify the keyword (local) in the Data Source like in the following example:
strConnect = _T("Provider=sqloledb;Data Source=(local);"
"Initial Catalog=myDatabaseName;"
"User ID=myUsername;Password=myPassword;");To connect to SQL Server running on a remote computer ( via an IP address): strConnect = _T("Provider=sqloledb;Network Library=DBMSSOCN;"
"Data Source=130.120.110.001,1433;"
"Initial Catalog=MyDatabaseName;User ID=MyUsername;"
"Password=MyPassword;");
if (FAILED(pPtr.CreateInstance("ADODB.Recordset")))
{
AfxMessageBox("Create Instance failed!");
return FALSE;
} CString strSRC;
strSRC="Driver=SQL Server;Server=";
strSRC+="210.46.141.145";
strSRC+=";Database=";
strSRC+="mydb";
strSRC+=";UID=sa;PWD=";
strSRC+="sa"; CString strSQL = "select id,name,gender,address from personal"; _variant_t varSRC(strSRC);
_variant_t varSQL(strSQL); if(FAILED(pPtr->Open(varSQL,varSRC,adOpenStatic,adLockOptimistic,adCmdText)))
{
AfxMessageBox("Open table failed!");
pPtr.Release();
return FALSE;
}