CString strSQL;//定义SQL命令串,用来保存SQL语句
strSQL.Format("string here");
当需要用到strSQL的时候使用 strSQL.AllocSysString()进行类型转换使用;
连接Access数据库的连接字符串:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.mdb
连接Sqlserver2000的数据库字符串:provider=sqloledb;server=.;database=test;uid=sa;pwd=
连接Sqlserver2005的数据库字符串:provider=sqloledb;server=.\\sqlexpress;database=test;uid=sa;pwd=
1,在StdAfx.h中加入
#import "C:\Program Files\common files\system\ado\msado15.dll" no_namespace rename("EOF","EndOfFile") rename("BOF","FirstOfFile")
2,在APP的InitInstance中调用函数初始化Ole控件
if(!AfxOleInit())
{
AfxMessageBox("初始化连接失败!");
}
3,在调用的地方
_ConnectionPtr con;
if(FAILED(con.CreateInstance("ADODB.Connection")))
{
MessageBox("sorry,failed!");
return;
}
CString str;
str.Format("provider=sqloledb;server=.\\sqlexpress;database=test;uid=sa;pwd=chen");
HRESULT hr;
hr=con->Open(str.AllocSysString (),"","",adModeUnknown);
if(FAILED(hr))
{
MessageBox("failed");
}
else
{
MessageBox("success");
}