最新消息:

VC ado sql数据库查询

未分类 admin 3069浏览 0评论

CString str;

::CoInitialize(NULL);
try
{
_ConnectionPtr conn(__uuidof(Connection));
_RecordsetPtr record(__uuidof(Recordset));

conn->ConnectionString = “Provider=SQLOLEDB.1;Persist Security Info=False;
User ID = sa;//用户名
Initial Catalog = CodeTest;//表名
Data Source = 164.70.6.219″; //服务器地址

conn->Open(conn->ConnectionString, “”, “”, -1);

record = conn->Execute( “select * from CPost”, NULL, adCmdUnknown);
while(!record->myEOF)
{
_variant_t temp = record->GetCollect(“PostName”);//列名,可以是数字
if(temp.vt != VT_NULL)
{
str = (LPCSTR)_bstr_t(temp);
MessageBox(str.GetBuffer(str.GetLength()));
}
record->MoveNext();
}
if(conn->State)//不能多次关闭,否则出错
conn->Close();
record->Clone();
conn.Release();
record.Release();//注意释放,避免引起内存泄露

::CoUninitialize();
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format(“连接数据库失败!rn错误信息:%s”,e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}

转载请注明:爱开源 » VC ado sql数据库查询

您必须 登录 才能发表评论!