根本还不熟悉vc,可是有赶着要做用到访问数据库的东西,也搞不清楚什么是ado,找了好多相关文章看,但是还是很糊涂,想写个例子看看,可是文章中提到的如下的一些代码根本不知道该放到程序的哪里去。:((((很弱智的问题吧,可是实在搞不清楚,有谁能好心回答一下么?在CAboutDlg中声明
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
在OnInitDialog()中建立连接
try{
m_pConnection.CreateInstance(__uuidof(Connetion));
m_pConnection.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\GCL6\\Data\\Test.mdb;",
"","",-1); //在"="两边莫加空格!
catch(...)
{
AfxMessageBox("数据库连接失败!");
}
打开查询,访问数据
_variant TheValue;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_list.ResetContent();
try
{
m_pRecordset->Open("select * from property", //注意:m_pRecordset后加 . 和加 -> 功能不同
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText
);
while (!m_pRecordset->adoEOF)
{
TheValue = m_pRecordset->GetCollect("Caption");
if (TheValue.vt != VT_NULL)
m_list.AddString((char *)_bstr_t(TheValue));
m_pRecordset -> MoveNext();
}
m_pRecordset -> Close();
}
catch(...)
{
AfxMessageBox("Error!");
}
访问域
FieldPrt pField;
for (int i=0; i<m_pRecordset->Fields->Count-1; i++)
{
pField = m_pRecordset->Fields->GetItem(long(i)));
}
(char *)pField->Name 即域名。
pField->Type是数据类型
pField->Precision是精度
注:数值类型和货币类型的Precision小于255,其余类型都是255。
枚举所有表/查询
m_pRecordset = m_pConnetion->OpenSchema(adSchemaTable);
while(!m_pRecordset->adoEOF)
{
char pType[40];
strcpy(pType, (char *)_bstr_t(m_pRecordset->GetCollect("TABLE_TYPE")));
if (!strcmp(pType, "TABLE")) //"VIEW"对应于查询
m_pRecordset -> GetCollect("TABLE_NAME"); //得到表名(需要转化一下)
}
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
在OnInitDialog()中建立连接
try{
m_pConnection.CreateInstance(__uuidof(Connetion));
m_pConnection.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\GCL6\\Data\\Test.mdb;",
"","",-1); //在"="两边莫加空格!
catch(...)
{
AfxMessageBox("数据库连接失败!");
}
打开查询,访问数据
_variant TheValue;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_list.ResetContent();
try
{
m_pRecordset->Open("select * from property", //注意:m_pRecordset后加 . 和加 -> 功能不同
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText
);
while (!m_pRecordset->adoEOF)
{
TheValue = m_pRecordset->GetCollect("Caption");
if (TheValue.vt != VT_NULL)
m_list.AddString((char *)_bstr_t(TheValue));
m_pRecordset -> MoveNext();
}
m_pRecordset -> Close();
}
catch(...)
{
AfxMessageBox("Error!");
}
访问域
FieldPrt pField;
for (int i=0; i<m_pRecordset->Fields->Count-1; i++)
{
pField = m_pRecordset->Fields->GetItem(long(i)));
}
(char *)pField->Name 即域名。
pField->Type是数据类型
pField->Precision是精度
注:数值类型和货币类型的Precision小于255,其余类型都是255。
枚举所有表/查询
m_pRecordset = m_pConnetion->OpenSchema(adSchemaTable);
while(!m_pRecordset->adoEOF)
{
char pType[40];
strcpy(pType, (char *)_bstr_t(m_pRecordset->GetCollect("TABLE_TYPE")));
if (!strcmp(pType, "TABLE")) //"VIEW"对应于查询
m_pRecordset -> GetCollect("TABLE_NAME"); //得到表名(需要转化一下)
}
http://www.vckbase.com/vckbase/vckbase10/vc/nonctrls/database_07/1007001.htm
【3】创建Connection对象并连接数据库
首先我们需要添加一个指向Connection对象的指针:
_ConnectionPtr m_pConnection;
下面的代码演示了如何创建Connection对象实例及如何连接数据库并进行异常捕捉。
BOOL CADOTest1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
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);///显示错误信息
} 在这段代码中我们是通过Connection对象的Open方法来进行连接数据库的,下面是该方法的原型
HRESULT Connection15::Open ( _bstr_t ConnectionString, _bstr_t UserID, _bstr_t Password, long Options )
这一段的代码要写到哪里我搞不清楚啊。:(
你可以在 自己程序的初始化函数中实现,也可以在文档或视图或对话框的初始化函数中实现,这要取决于你的应用。
另外,如果不是万不得以,不要随便写什么快餐式的程序,使用一些没有把握的代码,到出问题的时候后悔就晚了