查看: 474|回复: 2

sqlserver数据库C++连接

 关闭 [复制链接]

签到天数: 2122 天

连续签到: 10 天

[LV.Master]至尊海神IIIIII

发表于 2009-9-13 21:49 | 显示全部楼层 |阅读模式
网络上找到的资料,先mark下,随后再试试
1、在stdafx.h文件最后(即#endif // _AFX_NO_AFXCMN_SUPPORT下面)添加:
#import \"c:\\program files\\common files\\system\\ado\\msado15.dll\" no_namespace rename(\"EOF\",\"adoEOF\")

2、初始化COM:
AfxOleInit();//这行代码要放在功能执行前,如果是基于对话框建立的程序,那就放在第一个对话框类的OnInitDialog()函数的return TRUE;前

3、在用到数据库的地方:
_ConnectionPtr m_pConnection;///声明数据库连接变量
_RecordsetPtr m_pRecordset;///声明数据库集合变量
CString strCn;
strCn.Empty();

(1)连接数据库
HRESULT hr;
try
{
_variant_t RecordsAffected;
hr = m_pConnection.CreateInstance(\"ADODB.Connection\");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open(\"DSN=test;UID=;PWD=;\",\"\",\"\",adModeUnknown);///连接数据库
}
}
catch( _com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(\"连接数据库失败!\\r\\n错误信息:%s\",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}

(2)通过SQL读数据
CString sql;
try
{
m_pRecordset.CreateInstance(\"ADODB.Recordset\");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉异常
{
CString errorMessage = e.ErrorMessage();
AfxMessageBox(\"读取数据时出错:\"+sql+errorMessage);///显示错误信息
}

(3)通过sql语句添加、修改、删除记录
_variant_t RecordsAffected;

try
{
m_pConnection->Execute((_bstr_t)Sql,&RecordsAffected,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
PCOS系统下载站:http://zhuangji.wang

签到天数: 2122 天

连续签到: 10 天

[LV.Master]至尊海神IIIIII

 楼主| 发表于 2009-9-13 21:49 | 显示全部楼层

sqlserver数据库C++连接

网络上找到的资料,先mark下,随后再试试
1、在stdafx.h文件最后(即#endif // _AFX_NO_AFXCMN_SUPPORT下面)添加:
#import \"c:\\program files\\common files\\system\\ado\\msado15.dll\" no_namespace rename(\"EOF\",\"adoEOF\")

2、初始化COM:
AfxOleInit();//这行代码要放在功能执行前,如果是基于对话框建立的程序,那就放在第一个对话框类的OnInitDialog()函数的return TRUE;前

3、在用到数据库的地方:
_ConnectionPtr m_pConnection;///声明数据库连接变量
_RecordsetPtr m_pRecordset;///声明数据库集合变量
CString strCn;
strCn.Empty();

(1)连接数据库
HRESULT hr;
try
{
_variant_t RecordsAffected;
hr = m_pConnection.CreateInstance(\"ADODB.Connection\");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open(\"DSN=test;UID=;PWD=;\",\"\",\"\",adModeUnknown);///连接数据库
}
}
catch( _com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(\"连接数据库失败!\\r\\n错误信息:%s\",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}

(2)通过SQL读数据
CString sql;
try
{
m_pRecordset.CreateInstance(\"ADODB.Recordset\");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉异常
{
CString errorMessage = e.ErrorMessage();
AfxMessageBox(\"读取数据时出错:\"+sql+errorMessage);///显示错误信息
}

(3)通过sql语句添加、修改、删除记录
_variant_t RecordsAffected;

try
{
m_pConnection->Execute((_bstr_t)Sql,&RecordsAffected,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
PCOS系统下载站:http://zhuangji.wang
回复 支持 反对

使用道具 举报

签到天数: 2122 天

连续签到: 10 天

[LV.Master]至尊海神IIIIII

 楼主| 发表于 2009-9-13 21:49 | 显示全部楼层

sqlserver数据库C++连接

网络上找到的资料,先mark下,随后再试试
1、在stdafx.h文件最后(即#endif // _AFX_NO_AFXCMN_SUPPORT下面)添加:
#import \"c:\\program files\\common files\\system\\ado\\msado15.dll\" no_namespace rename(\"EOF\",\"adoEOF\")

2、初始化COM:
AfxOleInit();//这行代码要放在功能执行前,如果是基于对话框建立的程序,那就放在第一个对话框类的OnInitDialog()函数的return TRUE;前

3、在用到数据库的地方:
_ConnectionPtr m_pConnection;///声明数据库连接变量
_RecordsetPtr m_pRecordset;///声明数据库集合变量
CString strCn;
strCn.Empty();

(1)连接数据库
HRESULT hr;
try
{
_variant_t RecordsAffected;
hr = m_pConnection.CreateInstance(\"ADODB.Connection\");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open(\"DSN=test;UID=;PWD=;\",\"\",\"\",adModeUnknown);///连接数据库
}
}
catch( _com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(\"连接数据库失败!\\r\\n错误信息:%s\",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}

(2)通过SQL读数据
CString sql;
try
{
m_pRecordset.CreateInstance(\"ADODB.Recordset\");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉异常
{
CString errorMessage = e.ErrorMessage();
AfxMessageBox(\"读取数据时出错:\"+sql+errorMessage);///显示错误信息
}

(3)通过sql语句添加、修改、删除记录
_variant_t RecordsAffected;

try
{
m_pConnection->Execute((_bstr_t)Sql,&RecordsAffected,adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
PCOS系统下载站:http://zhuangji.wang
回复 支持 反对

使用道具 举报

本版积分规则