您的位置:首页 > 编程语言

VC6.0+ADO+Access编程常见问题勘误

2011-05-20 09:45 351 查看
1、出现warning C4146: unary minus operator applied to unsigned type, result still unsigned错误的话,这样处理:



//#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF")

改为:

#import "c:/Program Files/common files/system/ado/msado20.tlb" no_namespace rename( "EOF ", "adoEOF ")

2、关于ComboBox控件问题,必须放在Dlg对话框中的OnInitDialog()函数中,但是默认是没有这个函数的,需要通过Add Windows Message Handler来添加WM_INITDIALOG消息映射,自然就会出现该函数了。

示例代码:

BOOL CLoginVerify::OnInitDialog()
{
CDialog::OnInitDialog();

CString Username;

m_UserName.AddString("111");

return TRUE;

}

一般来说应该都没问题了,我在这里卡了有几个小时,今天才找到原因......

3、关于activex控件初始化问题,可以在OnCreate消息里面加入初始化函数.

提前进行了公用变量声明:

//定义公用变量
_ConnectionPtr pConn;
HRESULT hr;
_RecordsetPtr m_pSet;
_variant_t ra;

在其他的子程序里面可以使用下面的程序段来声明:

extern _ConnectionPtr pConn;
extern HRESULT hr;
extern _RecordsetPtr m_pSet;
extern _variant_t ra;

并进行数据库连接测试如下:

int CZhengZ1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{

if (CFormView::OnCreate(lpCreateStruct) == -1)
return -1;
//初始化ADO连接
if(!AfxOleInit())//这就是初始化COM库
{
AfxMessageBox("OLE初始化出错!");
return FALSE;
}
else
{
AfxMessageBox("OLE初始化成功!");
}
// TODO: Add your specialized creation code here

//连接数据库
try
{
//创建connection对象,并将结果放入hr对象中,并通过try catch进行错误捕捉,输出错误信息
HRESULT hr = pConn.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{

//下面的这句是为了打开access2000的数据库,默认情况下vc无法使用access2000数据库的
hr = pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb","","",adModeUnknown);
}
//if(pConn->State)
// pConn->Close();//如果已经打开了连接则关闭连接
//创建recordset对象,并创建查询,放入recordset对象中
m_pSet.CreateInstance("ADODB.Recordset");
CString sql;//创建sql查询语句
sql.Format("SELECT * FROM UserList");
m_pSet=pConn->Execute((_bstr_t)sql,&ra,adCmdText);
CString Userid,Username,Userpass;
int Recordcount1=0;
while(!m_pSet->adoEOF)
{
Userid=(LPCTSTR)(_bstr_t)(m_pSet->GetCollect((_variant_t)(long)(0)));
Username=(LPCTSTR)(_bstr_t)(m_pSet->GetCollect("Name"));
Userpass=(LPCTSTR)(_bstr_t)(m_pSet->GetCollect("Password"));
//AfxMessageBox("ID:"+Userid+"Username:"+Username+"UserPass:"+Userpass);//为了测试正确性
m_pSet->MoveNext();//取下一个记录
Recordcount1++;//计数器加1
}

//关闭连接及recordset对象。
CString message;
message.Format("共有%d条记录",Recordcount1);
m_pSet->Close();
pConn->Close();

AfxMessageBox(message);
}
catch(_com_error e)
{
CString strComError;
strComError.Format("错误编号:%08lx/n 错误信息:%s/n错误源:%s/n错误描述:%s",e.Error(),e.ErrorMessage(),(LPCSTR)e.Source(),(LPCSTR)e.Description());
::MessageBox(NULL,strComError,"Error",MB_ICONEXCLAMATION);
}
CLoginVerify logindlg;//创建登陆界面对象,实例化之。
logindlg.DoModal();//弹出登陆界面,并进行登陆判断
return 0;

}

4、取得记录数的方法:

::公用部分

//取得标准日期

SYSTEMTIME st;
CString strDate,strTime,strMonth,strDay;
GetLocalTime(&st);

//处理为20110510标准显示的日期
if(st.wMonth<10)
{
strMonth.Format("0%d",st.wMonth);
}
else
{
strMonth.Format("%d",st.wMonth);
}
if(st.wDay<10)
{
strDay.Format("0%d",st.wDay);
}
else
{
strDay.Format("%d",st.wDay);
}

//将新日期生成到strDate中
strDate.Format("%4d%s%s",st.wYear,strMonth,strDay);
strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);
//AfxMessageBox("现在的日期为:"+strDate);

1)、
CString sql,SerialNumber,sql1="";
long recordnumber=0;
sql1=sql1+strDate+"%";
sql.Format("Select COUNT(*) From ChengZJL Where DanJH like '%s'",sql1);
m_pSet=pConn->Execute((_bstr_t)sql,&ra,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount= m_pSet->GetCollect(vIndex);
recordnumber = vCount;
message.Format("记录的条数为:%d",vCount.iVal);
AfxMessageBox(message);

//得到信息,注意,一定不能用AfxMessageBox(vCount);这样在某些情况下会出错,但我不知道在什么情况下出错,即乱码。
2)、方法2

sql.Format("Select * From ChengZJL Where DanJH like '%s'",sql1);
AfxMessageBox("查询语句为:"+sql);
if(m_pSet->State)
m_pSet->Close();//如果已经打开了连接则关闭连接
m_pSet->Open(_variant_t(sql),_variant_t((IDispatch*)pConn,TRUE),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pSet->adoEOF)
{
m_pSet->MoveLast();
recordnumber=m_pSet->GetRecordCount();
message.Format("记录的条数为:%d",recordnumber);
AfxMessageBox(message);
}
else
{
recordnumber=0;
AfxMessageBox("No Record");
}

//得到记录条数 recordnumber

//公用部分,注意使用时要去掉,2方法使用的查询并不一致,一种情况用_ConnectionPtr 对象查询,一种为_RecordsetPtr查询,对应查询方法也不一样,千万不能混用,不然会出错的。

recordnumber++;
message.Format("记录编号为:%d",recordnumber);

//这里为调试信息,到真正生成的时候要注释掉
AfxMessageBox(message);
//生成真正的串号,规格为200101010001类型,长度为定长
if(recordnumber<10)SerialNumber.Format("%s000%d",strDate,recordnumber);
if(recordnumber<100&&recordnumber>9)SerialNumber.Format("%s00%d",strDate,recordnumber);
if(recordnumber<1000&&recordnumber>99)SerialNumber.Format("%s0%d",strDate,recordnumber);
//if(recordnumber<10000&&recordnumber>999)SerialNumber.Format("%s0%d",strDate,recordnumber);

程序正在编写,其他问题即将发出!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: