//
stdafx.h
#import " c:\program files\common files\system\ado\msado15.dll " no_namespace rename( " EOF " , " adoEOF " )
//主程序初始化函数
BOOL CADO2App::InitInstance()
{
AfxEnableControlContainer();
AfxOleInit();//初始化COM库
// --------------------------------------------
#import " c:\program files\common files\system\ado\msado15.dll " no_namespace rename( " EOF " , " adoEOF " )
//主程序初始化函数
BOOL CADO2App::InitInstance()
{
AfxEnableControlContainer();
AfxOleInit();//初始化COM库
// --------------------------------------------
下面是ACCESS的:
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance( " ADODB.Connection " ); /// 创建Connection对象
if (SUCCEEDED(hr)) {
hr = m_pConnection -> Open( " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb " , "" , "" ,adModeUnknown); /// 连接数据库
/// 上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch (_com_error e) /// 捕捉异常
{
CString errormessage;
errormessage.Format( " 连接数据库失败!\r\n错误信息:%s " ,e.ErrorMessage());
AfxMessageBox(errormessage); /// 显示错误信息
return FALSE;
}
try
{
hr = m_pConnection.CreateInstance( " ADODB.Connection " ); /// 创建Connection对象
if (SUCCEEDED(hr)) {
hr = m_pConnection -> Open( " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb " , "" , "" ,adModeUnknown); /// 连接数据库
/// 上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch (_com_error e) /// 捕捉异常
{
CString errormessage;
errormessage.Format( " 连接数据库失败!\r\n错误信息:%s " ,e.ErrorMessage());
AfxMessageBox(errormessage); /// 显示错误信息
return FALSE;
}
下面是连接SQL SERVER的
CString strSQL;
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection -> CursorLocation = adUseClient;
strSQL = " Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TEST;Data Source=yjm " ;
if (SUCCEEDED(hr))
{
hr = m_pConnection -> Open(_bstr_t(strSQL), "" , "" , - 1 );
}
}
catch (_com_error e) /// 捕捉异常
{
CString errormessage;
errormessage.Format( " 连接数据库失败!\r\n错误信息:%s " ,e.ErrorMessage());
AfxMessageBox(errormessage); /// 显示错误信息
return FALSE;
}
// AfxMessageBox("connected~~");
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection -> CursorLocation = adUseClient;
strSQL = " Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TEST;Data Source=yjm " ;
if (SUCCEEDED(hr))
{
hr = m_pConnection -> Open(_bstr_t(strSQL), "" , "" , - 1 );
}
}
catch (_com_error e) /// 捕捉异常
{
CString errormessage;
errormessage.Format( " 连接数据库失败!\r\n错误信息:%s " ,e.ErrorMessage());
AfxMessageBox(errormessage); /// 显示错误信息
return FALSE;
}
// AfxMessageBox("connected~~");
其中:
----- ADO连接SQL Server的数据库连接字符串模板 ----------
身份验证模式为:"sql server和windows"
Provider=SQLOLEDB.1;Persist Security Info=True;User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名
身份验证模式为:"仅windows"
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=数据库名;Data Source=SQL服务器名
注:表名有中文,sqlserver中select的时候要加[]