C语言怎么通过ADO连接SQL server数据库
1个回答
展开全部
举个例子,连接SQL:
// 打缓悄凳开数据库运铅
strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:Database Password=%s"), m_strUnEntryptMdbFilePath,m_strMDBPassword);
// 创建连接
HRESULT hr = m_pConnection.CreateInstance(_uuidof(Connection));
_ConnectionPtr m_pConnection->Open(m_strDBClass,_T(""),_T(""),adConnectUnspecified);
// 声明表单指针
_RecordsetPtr pBandRecordset;
pBandRecordset.CreateInstance(__uuidof(Recordset));
// 执行语句
CString strSQL(L"SELECT * FROM [Band]");
扰旅m_pConnection->Execute((LPCTSTR)strSQL,NULL,0);
// 提取某一项 例如BandInfo
int iBandInfo = wcscmp(colum, L"BandInfo");
while(!recordsetPtr->adoEOF)
{
var = recordsetPtr->GetCollect(colum);
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
recordsetPtr->MoveNext();
}
// 打缓悄凳开数据库运铅
strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:Database Password=%s"), m_strUnEntryptMdbFilePath,m_strMDBPassword);
// 创建连接
HRESULT hr = m_pConnection.CreateInstance(_uuidof(Connection));
_ConnectionPtr m_pConnection->Open(m_strDBClass,_T(""),_T(""),adConnectUnspecified);
// 声明表单指针
_RecordsetPtr pBandRecordset;
pBandRecordset.CreateInstance(__uuidof(Recordset));
// 执行语句
CString strSQL(L"SELECT * FROM [Band]");
扰旅m_pConnection->Execute((LPCTSTR)strSQL,NULL,0);
// 提取某一项 例如BandInfo
int iBandInfo = wcscmp(colum, L"BandInfo");
while(!recordsetPtr->adoEOF)
{
var = recordsetPtr->GetCollect(colum);
if(var.vt != VT_NULL)
strName = (LPCSTR)_bstr_t(var);
recordsetPtr->MoveNext();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询