VC++用ADO连接数据库,登陆界面代码怎么写?
VC++用ADO连接数据库,登陆界面代码怎么写?在线等,急,高分C++新手,要详细注释!谢谢啊!...
VC++用ADO连接数据库,登陆界面代码怎么写?在线等,急,高分
C++新手,要详细注释!谢谢啊! 展开
C++新手,要详细注释!谢谢啊! 展开
1个回答
展开全部
1.提取单条记录
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL);
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%%0"; //*.mdb
//_bstr_t varSource="Data Source=127.0.0.1;Initial Catalog=db2;User Id=;Password=;"
_ConnectionPtr m_pConnection(_uuidof(Connection));
m_pConnection->Open(varSource,"","",adModeUnknow);
_RecordsetPtr m_pSet(_uuidof(Recordset));
try {
m_pSet->Open(%%1,m_pConnection.GetInterfacePtr()
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e){
{
AfxMessageBox(e->ErrorMessage());
return;
}
_variant_t var;
CString %%2="";
long fldc=m_pSet->GetFields()->GetCount();
long i=0;
try {
m_pSet->MoveFirst();
if(!m_pSet->adoEOF)
{
for(i=0;i<fldc;i++)
{
var=m_pSet->GetCollect((long)i);
var.ChangeType(VT_BSTR);
%%2+=var.bstrVal;
%%2+=" ";
}
//m_pSet->MoveNext();
}
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
delete e;
}
//m_pSet->MoveFirst();
CoUninitialize();
2.单值比较
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL);
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%%0"; //*.mdb
//_bstr_t varSource="Data Source=127.0.0.1;Initial Catalog=db2;User Id=;Password=;"
_ConnectionPtr m_pConnection(_uuidof(Connection));
m_pConnection->Open(varSource,"","",adModeUnknown);
_RecordsetPtr m_pSet(_uuidof(Recordset));
try {
m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e){
{
AfxMessageBox(e->ErrorMessage());
return;
}
_variant_t var;
try {
m_pSet->MoveFirst();
if(!m_pSet->adoEOF)
{
var=m_pSet->GetCollect((long)0);
var.ChangeType(VT_I2);
int %%3=var.intVal;
if(%%3==%%4)
{
%%5
}
//m_pSet->MoveNext();
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
delete e;
}
//m_pSet->MoveFirst();
CoUninitialize();
22.2、用ADO调用存储过程
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
_ConnectionPtr Conn = NULL;
_RecordsetPtr Rs = NULL;
_CommandPtr Cmd = NULL;
CoInitialize(NULL);
Conn.CreateInstance (_uuidof(Connection));
Rs.CreateInstance (_uuidof(Recordset));
Cmd.CreateInstance (_uuidof(Command));
Conn->Open(L"db", L"sa", L"", adOpenUnspecified); //打开正常
//如下为_CommandPtr对象参数的赋值和调用
Cmd->ActiveConnection = Conn;
Cmd->CommandText = "SP_TEST";
//数据库中实际存在这个测试存储过程,select * from atable,不传递参数
Cmd->CommandType = adCmdStoredProc;
Cmd->Parameters->Refresh();
Rs = Cmd->Execute( NULL,NULL, adCmdUnknown ); //COM出错。
//注释掉的代码为直接的SQL语句提交,运行正确。
//Cmd->ActiveConnection=Conn;
//Cmd->CommandText = "select * from atable";
//Cmd->CommandType = adCmdText;
//Cmd->Parameters->Refresh();
//Rs = Cmd->Execute(NULL,NULL,adCmdUnknown);
Rs->Close();
Conn->Close();
CoUninitialize();
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL);
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%%0"; //*.mdb
//_bstr_t varSource="Data Source=127.0.0.1;Initial Catalog=db2;User Id=;Password=;"
_ConnectionPtr m_pConnection(_uuidof(Connection));
m_pConnection->Open(varSource,"","",adModeUnknow);
_RecordsetPtr m_pSet(_uuidof(Recordset));
try {
m_pSet->Open(%%1,m_pConnection.GetInterfacePtr()
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e){
{
AfxMessageBox(e->ErrorMessage());
return;
}
_variant_t var;
CString %%2="";
long fldc=m_pSet->GetFields()->GetCount();
long i=0;
try {
m_pSet->MoveFirst();
if(!m_pSet->adoEOF)
{
for(i=0;i<fldc;i++)
{
var=m_pSet->GetCollect((long)i);
var.ChangeType(VT_BSTR);
%%2+=var.bstrVal;
%%2+=" ";
}
//m_pSet->MoveNext();
}
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
delete e;
}
//m_pSet->MoveFirst();
CoUninitialize();
2.单值比较
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
CoInitialize(NULL);
_bstr_t varSource="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%%0"; //*.mdb
//_bstr_t varSource="Data Source=127.0.0.1;Initial Catalog=db2;User Id=;Password=;"
_ConnectionPtr m_pConnection(_uuidof(Connection));
m_pConnection->Open(varSource,"","",adModeUnknown);
_RecordsetPtr m_pSet(_uuidof(Recordset));
try {
m_pSet->Open(%%1,m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);
}
catch(_com_error *e){
{
AfxMessageBox(e->ErrorMessage());
return;
}
_variant_t var;
try {
m_pSet->MoveFirst();
if(!m_pSet->adoEOF)
{
var=m_pSet->GetCollect((long)0);
var.ChangeType(VT_I2);
int %%3=var.intVal;
if(%%3==%%4)
{
%%5
}
//m_pSet->MoveNext();
}
catch(_com_error *e){
AfxMessageBox(e->ErrorMessage());
delete e;
}
//m_pSet->MoveFirst();
CoUninitialize();
22.2、用ADO调用存储过程
/*
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
*/
_ConnectionPtr Conn = NULL;
_RecordsetPtr Rs = NULL;
_CommandPtr Cmd = NULL;
CoInitialize(NULL);
Conn.CreateInstance (_uuidof(Connection));
Rs.CreateInstance (_uuidof(Recordset));
Cmd.CreateInstance (_uuidof(Command));
Conn->Open(L"db", L"sa", L"", adOpenUnspecified); //打开正常
//如下为_CommandPtr对象参数的赋值和调用
Cmd->ActiveConnection = Conn;
Cmd->CommandText = "SP_TEST";
//数据库中实际存在这个测试存储过程,select * from atable,不传递参数
Cmd->CommandType = adCmdStoredProc;
Cmd->Parameters->Refresh();
Rs = Cmd->Execute( NULL,NULL, adCmdUnknown ); //COM出错。
//注释掉的代码为直接的SQL语句提交,运行正确。
//Cmd->ActiveConnection=Conn;
//Cmd->CommandText = "select * from atable";
//Cmd->CommandType = adCmdText;
//Cmd->Parameters->Refresh();
//Rs = Cmd->Execute(NULL,NULL,adCmdUnknown);
Rs->Close();
Conn->Close();
CoUninitialize();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询