21 个解决方案
#1
貌似该到VC++6.0相关论坛去问
#2
有木有高手指教下呀,新手求急救,坐等回复,高分求问题的解决方法呀
#3
人生第一帖,不知道往哪发,这个不算数据库的问题吗?
#4
能写不能读??
操作对象用错了吧??
操作对象用错了吧??
#5
VC++用ADO?
为什么不用ODBC直接调用?
为什么不用ODBC直接调用?
#6
你可以把代码贴出来看看。
#7
try
{
mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
while(!mRecordset->adoEOF)
{
vBID=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("BID"))->Value);
vBname=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bname"))->Value);
vBauthor=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bauthor"))->Value);
vBpress=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bpress"))->Value);
vBprice=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bprice"))->Value);
vBclass=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bclass"))->Value);
vBstock=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bstock"))->Value);
nItem=m_mainlist.InsertItem(0xffff,vBID);
m_mainlist.SetItem(nItem,1,1,vBname,NULL,0,0,0);
m_mainlist.SetItem(nItem,2,1,vBauthor,NULL,0,0,0);
m_mainlist.SetItem(nItem,3,1,vBpress,NULL,0,0,0);
m_mainlist.SetItem(nItem,4,1,vBprice,NULL,0,0,0);
m_mainlist.SetItem(nItem,5,1,vBclass,NULL,0,0,0);
m_mainlist.SetItem(nItem,6,1,vBstock,NULL,0,0,0);
mRecordset->MoveNext();
}
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
vBID,vBname,vBauthor,vBpress,vBprice,vBclass,vBstock这几个我定义成CString类型的
{
mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
while(!mRecordset->adoEOF)
{
vBID=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("BID"))->Value);
vBname=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bname"))->Value);
vBauthor=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bauthor"))->Value);
vBpress=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bpress"))->Value);
vBprice=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bprice"))->Value);
vBclass=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bclass"))->Value);
vBstock=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bstock"))->Value);
nItem=m_mainlist.InsertItem(0xffff,vBID);
m_mainlist.SetItem(nItem,1,1,vBname,NULL,0,0,0);
m_mainlist.SetItem(nItem,2,1,vBauthor,NULL,0,0,0);
m_mainlist.SetItem(nItem,3,1,vBpress,NULL,0,0,0);
m_mainlist.SetItem(nItem,4,1,vBprice,NULL,0,0,0);
m_mainlist.SetItem(nItem,5,1,vBclass,NULL,0,0,0);
m_mainlist.SetItem(nItem,6,1,vBstock,NULL,0,0,0);
mRecordset->MoveNext();
}
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
vBID,vBname,vBauthor,vBpress,vBprice,vBclass,vBstock这几个我定义成CString类型的
#8
没有高手指点下吗,没有那我删贴去C++那请教下了
#9
可以让版主转。
#10
你的 strSQL 是什么?
把这个查询字符串到企业管理器里去运行一下,如果能读出数据来,才能进一步调试.
把这个查询字符串到企业管理器里去运行一下,如果能读出数据来,才能进一步调试.
#11
strSQL是查询的SQL语句,这个没错,我设断点查看过这个语句了。另外再说明的是连接是打开连上了的,Recordset也有分配了内存
#12
我也是新手,我RecordSet读取都是用:
pRecordPtr->GetCollect( "NodeName" );
你可以把你的改成:
vBID=(char*)(_bstr_t)(mRecordset->GetCollect("BID");
试试看,
pRecordPtr->GetCollect( "NodeName" );
你可以把你的改成:
vBID=(char*)(_bstr_t)(mRecordset->GetCollect("BID");
试试看,
#13
那个方法之前试过了,不行才改用现在 在这种vBstock=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bstock"))->Value); 方法的
#14
这个不是sql问题吧
#15
呃,那要怎么转帖,应该转去哪,C++版块吗?
#16
为什么不能用那个方法?好奇一下,因为我也是菜鸟一个。。。
我现在的程序,SQL查询都是用的那个方法。并且也是VC++环境
#17
这个没什么关系的吧,反正都是取字段的方法,个人喜欢吧,不过那个确实简洁很多
#18
vc 不懂呀
建议跟踪测
并且没看到strSQL,是否有返回记录
建议跟踪测
并且没看到strSQL,是否有返回记录
#19
高分诚心求教呀,ADO第一次用呀,有哪位高手诚心指点下吗,如果这样看不清代码我可能把整份工程发给你,邮箱可以,加我QQ也行,QQ:936900708,不过加时要注明哦。我是真的非常非常诚心的求教,毕业设计就差这个问题,离答辩日子不远了,求大神赐教呀
#20
另外,想问下,一个源文件无论要访问多少个表是不是都只能有一个Recordset的,还有就是有一个功能的部分能读到数据库,但为什么读出来的字段后面有很多空格的?急救赐教,哪位大神打救下我这个IT行业中的一个迷途小童子吧
#21
算了,我贴代码出来吧。请高手指出错误
BOOL CLibManSysDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CoInitialize(NULL);
HRESULT hr;
try
{
hr = mConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
_bstr_t strConnect = "Provider=SQLOLEDB; Server=PC-201103311850;Database=LM; uid=sa; pwd=107136;";
mConnection->Open(strConnect,"","",NULL); //连接数据库
}
}
catch( _com_error e )
{
AfxMessageBox("连接数据库失败!");
return FALSE;
}
mRecordset.CreateInstance(__uuidof(Recordset));
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("书名");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("类别");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("编号");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("作者");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->SetCurSel(0);
m_mainlist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
m_mainlist.InsertColumn(0,_T("编号"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(1,_T("书名"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(2,_T("作者"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(3,_T("出版社"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(4,_T("定价"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(5,_T("类别"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(6,_T("数量"),LVCFMT_LEFT,50);
return TRUE; // return TRUE unless you set the focus to a control
}
void CLibManSysDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLibManSysDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLibManSysDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
////////////////////查询/////////////////////////
void CLibManSysDlg::OnBtnQuery()
{
// TODO: Add your control notification handler code here
CString querycon ;
GetDlgItem(IDC_QUERYCON)->GetWindowText(querycon);
int quesel ;
quesel = ((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->GetCurSel();
//UpdateData();
CString strSQL;
if(!querycon)
strSQL = "select * from Book";
else
{
if(0==quesel)
strSQL = "select * from Book where Bname = '"+querycon+"'";
else if(1==quesel)
strSQL = "select * from Book where Bclass = '"+querycon+"'";
else if(2==quesel)
strSQL = "select * from Book where BID = '"+querycon+"'";
else
strSQL = "select * from Book where Bauthor = '"+querycon+"'";
}
int nItem ;
_variant_t vBID,vBname,vBauthor,vBpress,vBprice,vBclass,vBstock;
m_mainlist.SetRedraw(FALSE);
m_mainlist.DeleteAllItems();
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
while(!mRecordset->adoEOF)
{
//vBID=(char*)(_bstr_t)(mRecordset->GetCollect("BID"));
vBname=(char*)(_bstr_t)(mRecordset->GetCollect("Bname"));
vBauthor=(char*)(_bstr_t)(mRecordset->GetCollect("Bauthor"));
vBpress=(char*)(_bstr_t)(mRecordset->GetCollect("Bpress"));
vBprice=(char*)(_bstr_t)(mRecordset->GetCollect("Bprice"));
vBclass=(char*)(_bstr_t)(mRecordset->GetCollect("Bclass"));
vBstock=(char*)(_bstr_t)(mRecordset->GetCollect("Bstock"));
nItem=m_mainlist.InsertItem(0xffff,(_bstr_t)vBID);
m_mainlist.SetItem(nItem,1,1,(_bstr_t)vBname,NULL,0,0,0);
m_mainlist.SetItem(nItem,2,1,(_bstr_t)vBauthor,NULL,0,0,0);
m_mainlist.SetItem(nItem,3,1,(_bstr_t)vBpress,NULL,0,0,0);
m_mainlist.SetItem(nItem,4,1,(_bstr_t)vBprice,NULL,0,0,0);
m_mainlist.SetItem(nItem,5,1,(_bstr_t)vBclass,NULL,0,0,0);
m_mainlist.SetItem(nItem,6,1,(_bstr_t)vBstock,NULL,0,0,0);
mRecordset->MoveNext();
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
m_mainlist.SetRedraw(TRUE);
m_mainlist.Invalidate();
m_mainlist.UpdateWindow();
}
/////////////////////////登录///////////////////////
void CLibManSysDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData();
CString strSQL;
ID=m_id;
if(0==m_inselect)
{
strSQL= "select * from Reader where RID = '"+m_id+"'";
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset.CreateInstance("ReaderLogin.Recordset");
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
if(!mRecordset->adoEOF)
{
name=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rname"));
sex=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rsex"));
tel=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rtel"));
addr=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Raddr"));
pwd=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rpassword"));
avail=(long)(mRecordset->GetCollect("Ravailable"));
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("不存在的帐户!");
return;
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!"); //显示错误信息
return ;
}
}
if(1==m_inselect)
{
strSQL= "select * from Admin where AdID = '"+m_id+"'";
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset.CreateInstance("AdminLogin.Recordset");
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
if(!mRecordset->adoEOF)
{
name=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adname"));
sex=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adsex"));
tel=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adtel"));
addr=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adaddr"));
pwd=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adpassword"));
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("不存在的帐户!");
return;
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
}
if(pwd==m_pwd)
{
CDialog::OnOK();
if(m_inselect==0)
{
CReader Reader;
Reader.DoModal();
}
else
{
CAdmin Admin;
Admin.DoModal();
}
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("请输入正确的帐号密码!");
return;
}
}
/////////////////////注册/////////////////////////////////
void CLibManSysDlg::OnBtnEnroll()
{
// TODO: Add your control notification handler code here
CPerInfo Enroll;
if(Enroll.DoModal()==IDOK)
{
if(Enroll.m_PerID&&Enroll.m_PerName&&Enroll.m_PerTel&&Enroll.m_PerPwd)
{
try
{
mRecordset->Open("select * from Reader",_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
if(!mRecordset->Supports(adAddNew))
return;
mRecordset->AddNew();
mRecordset->Fields->GetItem(_variant_t("RID"))->Value=_bstr_t(Enroll.m_PerID);
mRecordset->Fields->GetItem(_variant_t("Rname"))->Value=_bstr_t(Enroll.m_PerName);
mRecordset->Fields->GetItem(_variant_t("Rsex"))->Value=_bstr_t(Enroll.m_PerSex);
mRecordset->Fields->GetItem(_variant_t("Rtel"))->Value=_bstr_t(Enroll.m_PerTel);
mRecordset->Fields->GetItem(_variant_t("Raddr"))->Value=_bstr_t(Enroll.m_PerAddr);
mRecordset->Fields->GetItem(_variant_t("Rmax"))->Value=_bstr_t((long)5);
mRecordset->Fields->GetItem(_variant_t("Ravailable"))->Value=_bstr_t((long)5);
mRecordset->Fields->GetItem(_variant_t("Rpassword"))->Value=_bstr_t(Enroll.m_PerPwd);
mRecordset->Update();
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("注册失败!");
return ;
}
}
else
{
AfxMessageBox("请输入完整的信息!");
return;
}
}
else
return;
}
///////////////////退出////////////////////////
void CLibManSysDlg::OnCancel()
{
// TODO: Add extra cleanup here
mConnection->Close();
if(mRecordset)
mRecordset.Release();
mConnection.Release();
CoUninitialize();
CDialog::OnCancel();
}
另外,还有两个界面也和这个差不多,也有COM初始化,数据库连接,会不会是这里有问题?万分诚恳的请求各种牛人指教呀,再不搞掂这个问题就没得毕业啦,TT
BOOL CLibManSysDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CoInitialize(NULL);
HRESULT hr;
try
{
hr = mConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
_bstr_t strConnect = "Provider=SQLOLEDB; Server=PC-201103311850;Database=LM; uid=sa; pwd=107136;";
mConnection->Open(strConnect,"","",NULL); //连接数据库
}
}
catch( _com_error e )
{
AfxMessageBox("连接数据库失败!");
return FALSE;
}
mRecordset.CreateInstance(__uuidof(Recordset));
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("书名");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("类别");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("编号");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("作者");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->SetCurSel(0);
m_mainlist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
m_mainlist.InsertColumn(0,_T("编号"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(1,_T("书名"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(2,_T("作者"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(3,_T("出版社"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(4,_T("定价"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(5,_T("类别"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(6,_T("数量"),LVCFMT_LEFT,50);
return TRUE; // return TRUE unless you set the focus to a control
}
void CLibManSysDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLibManSysDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLibManSysDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
////////////////////查询/////////////////////////
void CLibManSysDlg::OnBtnQuery()
{
// TODO: Add your control notification handler code here
CString querycon ;
GetDlgItem(IDC_QUERYCON)->GetWindowText(querycon);
int quesel ;
quesel = ((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->GetCurSel();
//UpdateData();
CString strSQL;
if(!querycon)
strSQL = "select * from Book";
else
{
if(0==quesel)
strSQL = "select * from Book where Bname = '"+querycon+"'";
else if(1==quesel)
strSQL = "select * from Book where Bclass = '"+querycon+"'";
else if(2==quesel)
strSQL = "select * from Book where BID = '"+querycon+"'";
else
strSQL = "select * from Book where Bauthor = '"+querycon+"'";
}
int nItem ;
_variant_t vBID,vBname,vBauthor,vBpress,vBprice,vBclass,vBstock;
m_mainlist.SetRedraw(FALSE);
m_mainlist.DeleteAllItems();
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
while(!mRecordset->adoEOF)
{
//vBID=(char*)(_bstr_t)(mRecordset->GetCollect("BID"));
vBname=(char*)(_bstr_t)(mRecordset->GetCollect("Bname"));
vBauthor=(char*)(_bstr_t)(mRecordset->GetCollect("Bauthor"));
vBpress=(char*)(_bstr_t)(mRecordset->GetCollect("Bpress"));
vBprice=(char*)(_bstr_t)(mRecordset->GetCollect("Bprice"));
vBclass=(char*)(_bstr_t)(mRecordset->GetCollect("Bclass"));
vBstock=(char*)(_bstr_t)(mRecordset->GetCollect("Bstock"));
nItem=m_mainlist.InsertItem(0xffff,(_bstr_t)vBID);
m_mainlist.SetItem(nItem,1,1,(_bstr_t)vBname,NULL,0,0,0);
m_mainlist.SetItem(nItem,2,1,(_bstr_t)vBauthor,NULL,0,0,0);
m_mainlist.SetItem(nItem,3,1,(_bstr_t)vBpress,NULL,0,0,0);
m_mainlist.SetItem(nItem,4,1,(_bstr_t)vBprice,NULL,0,0,0);
m_mainlist.SetItem(nItem,5,1,(_bstr_t)vBclass,NULL,0,0,0);
m_mainlist.SetItem(nItem,6,1,(_bstr_t)vBstock,NULL,0,0,0);
mRecordset->MoveNext();
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
m_mainlist.SetRedraw(TRUE);
m_mainlist.Invalidate();
m_mainlist.UpdateWindow();
}
/////////////////////////登录///////////////////////
void CLibManSysDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData();
CString strSQL;
ID=m_id;
if(0==m_inselect)
{
strSQL= "select * from Reader where RID = '"+m_id+"'";
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset.CreateInstance("ReaderLogin.Recordset");
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
if(!mRecordset->adoEOF)
{
name=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rname"));
sex=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rsex"));
tel=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rtel"));
addr=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Raddr"));
pwd=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rpassword"));
avail=(long)(mRecordset->GetCollect("Ravailable"));
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("不存在的帐户!");
return;
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!"); //显示错误信息
return ;
}
}
if(1==m_inselect)
{
strSQL= "select * from Admin where AdID = '"+m_id+"'";
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset.CreateInstance("AdminLogin.Recordset");
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
if(!mRecordset->adoEOF)
{
name=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adname"));
sex=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adsex"));
tel=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adtel"));
addr=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adaddr"));
pwd=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adpassword"));
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("不存在的帐户!");
return;
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
}
if(pwd==m_pwd)
{
CDialog::OnOK();
if(m_inselect==0)
{
CReader Reader;
Reader.DoModal();
}
else
{
CAdmin Admin;
Admin.DoModal();
}
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("请输入正确的帐号密码!");
return;
}
}
/////////////////////注册/////////////////////////////////
void CLibManSysDlg::OnBtnEnroll()
{
// TODO: Add your control notification handler code here
CPerInfo Enroll;
if(Enroll.DoModal()==IDOK)
{
if(Enroll.m_PerID&&Enroll.m_PerName&&Enroll.m_PerTel&&Enroll.m_PerPwd)
{
try
{
mRecordset->Open("select * from Reader",_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
if(!mRecordset->Supports(adAddNew))
return;
mRecordset->AddNew();
mRecordset->Fields->GetItem(_variant_t("RID"))->Value=_bstr_t(Enroll.m_PerID);
mRecordset->Fields->GetItem(_variant_t("Rname"))->Value=_bstr_t(Enroll.m_PerName);
mRecordset->Fields->GetItem(_variant_t("Rsex"))->Value=_bstr_t(Enroll.m_PerSex);
mRecordset->Fields->GetItem(_variant_t("Rtel"))->Value=_bstr_t(Enroll.m_PerTel);
mRecordset->Fields->GetItem(_variant_t("Raddr"))->Value=_bstr_t(Enroll.m_PerAddr);
mRecordset->Fields->GetItem(_variant_t("Rmax"))->Value=_bstr_t((long)5);
mRecordset->Fields->GetItem(_variant_t("Ravailable"))->Value=_bstr_t((long)5);
mRecordset->Fields->GetItem(_variant_t("Rpassword"))->Value=_bstr_t(Enroll.m_PerPwd);
mRecordset->Update();
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("注册失败!");
return ;
}
}
else
{
AfxMessageBox("请输入完整的信息!");
return;
}
}
else
return;
}
///////////////////退出////////////////////////
void CLibManSysDlg::OnCancel()
{
// TODO: Add extra cleanup here
mConnection->Close();
if(mRecordset)
mRecordset.Release();
mConnection.Release();
CoUninitialize();
CDialog::OnCancel();
}
另外,还有两个界面也和这个差不多,也有COM初始化,数据库连接,会不会是这里有问题?万分诚恳的请求各种牛人指教呀,再不搞掂这个问题就没得毕业啦,TT
#1
貌似该到VC++6.0相关论坛去问
#2
有木有高手指教下呀,新手求急救,坐等回复,高分求问题的解决方法呀
#3
人生第一帖,不知道往哪发,这个不算数据库的问题吗?
#4
能写不能读??
操作对象用错了吧??
操作对象用错了吧??
#5
VC++用ADO?
为什么不用ODBC直接调用?
为什么不用ODBC直接调用?
#6
你可以把代码贴出来看看。
#7
try
{
mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
while(!mRecordset->adoEOF)
{
vBID=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("BID"))->Value);
vBname=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bname"))->Value);
vBauthor=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bauthor"))->Value);
vBpress=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bpress"))->Value);
vBprice=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bprice"))->Value);
vBclass=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bclass"))->Value);
vBstock=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bstock"))->Value);
nItem=m_mainlist.InsertItem(0xffff,vBID);
m_mainlist.SetItem(nItem,1,1,vBname,NULL,0,0,0);
m_mainlist.SetItem(nItem,2,1,vBauthor,NULL,0,0,0);
m_mainlist.SetItem(nItem,3,1,vBpress,NULL,0,0,0);
m_mainlist.SetItem(nItem,4,1,vBprice,NULL,0,0,0);
m_mainlist.SetItem(nItem,5,1,vBclass,NULL,0,0,0);
m_mainlist.SetItem(nItem,6,1,vBstock,NULL,0,0,0);
mRecordset->MoveNext();
}
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
vBID,vBname,vBauthor,vBpress,vBprice,vBclass,vBstock这几个我定义成CString类型的
{
mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
while(!mRecordset->adoEOF)
{
vBID=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("BID"))->Value);
vBname=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bname"))->Value);
vBauthor=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bauthor"))->Value);
vBpress=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bpress"))->Value);
vBprice=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bprice"))->Value);
vBclass=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bclass"))->Value);
vBstock=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bstock"))->Value);
nItem=m_mainlist.InsertItem(0xffff,vBID);
m_mainlist.SetItem(nItem,1,1,vBname,NULL,0,0,0);
m_mainlist.SetItem(nItem,2,1,vBauthor,NULL,0,0,0);
m_mainlist.SetItem(nItem,3,1,vBpress,NULL,0,0,0);
m_mainlist.SetItem(nItem,4,1,vBprice,NULL,0,0,0);
m_mainlist.SetItem(nItem,5,1,vBclass,NULL,0,0,0);
m_mainlist.SetItem(nItem,6,1,vBstock,NULL,0,0,0);
mRecordset->MoveNext();
}
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
vBID,vBname,vBauthor,vBpress,vBprice,vBclass,vBstock这几个我定义成CString类型的
#8
没有高手指点下吗,没有那我删贴去C++那请教下了
#9
可以让版主转。
#10
你的 strSQL 是什么?
把这个查询字符串到企业管理器里去运行一下,如果能读出数据来,才能进一步调试.
把这个查询字符串到企业管理器里去运行一下,如果能读出数据来,才能进一步调试.
#11
strSQL是查询的SQL语句,这个没错,我设断点查看过这个语句了。另外再说明的是连接是打开连上了的,Recordset也有分配了内存
#12
我也是新手,我RecordSet读取都是用:
pRecordPtr->GetCollect( "NodeName" );
你可以把你的改成:
vBID=(char*)(_bstr_t)(mRecordset->GetCollect("BID");
试试看,
pRecordPtr->GetCollect( "NodeName" );
你可以把你的改成:
vBID=(char*)(_bstr_t)(mRecordset->GetCollect("BID");
试试看,
#13
那个方法之前试过了,不行才改用现在 在这种vBstock=(char*)(_bstr_t)(mRecordset->Fields->GetItem(_variant_t("Bstock"))->Value); 方法的
#14
这个不是sql问题吧
#15
呃,那要怎么转帖,应该转去哪,C++版块吗?
#16
为什么不能用那个方法?好奇一下,因为我也是菜鸟一个。。。
我现在的程序,SQL查询都是用的那个方法。并且也是VC++环境
#17
这个没什么关系的吧,反正都是取字段的方法,个人喜欢吧,不过那个确实简洁很多
#18
vc 不懂呀
建议跟踪测
并且没看到strSQL,是否有返回记录
建议跟踪测
并且没看到strSQL,是否有返回记录
#19
高分诚心求教呀,ADO第一次用呀,有哪位高手诚心指点下吗,如果这样看不清代码我可能把整份工程发给你,邮箱可以,加我QQ也行,QQ:936900708,不过加时要注明哦。我是真的非常非常诚心的求教,毕业设计就差这个问题,离答辩日子不远了,求大神赐教呀
#20
另外,想问下,一个源文件无论要访问多少个表是不是都只能有一个Recordset的,还有就是有一个功能的部分能读到数据库,但为什么读出来的字段后面有很多空格的?急救赐教,哪位大神打救下我这个IT行业中的一个迷途小童子吧
#21
算了,我贴代码出来吧。请高手指出错误
BOOL CLibManSysDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CoInitialize(NULL);
HRESULT hr;
try
{
hr = mConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
_bstr_t strConnect = "Provider=SQLOLEDB; Server=PC-201103311850;Database=LM; uid=sa; pwd=107136;";
mConnection->Open(strConnect,"","",NULL); //连接数据库
}
}
catch( _com_error e )
{
AfxMessageBox("连接数据库失败!");
return FALSE;
}
mRecordset.CreateInstance(__uuidof(Recordset));
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("书名");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("类别");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("编号");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("作者");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->SetCurSel(0);
m_mainlist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
m_mainlist.InsertColumn(0,_T("编号"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(1,_T("书名"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(2,_T("作者"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(3,_T("出版社"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(4,_T("定价"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(5,_T("类别"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(6,_T("数量"),LVCFMT_LEFT,50);
return TRUE; // return TRUE unless you set the focus to a control
}
void CLibManSysDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLibManSysDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLibManSysDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
////////////////////查询/////////////////////////
void CLibManSysDlg::OnBtnQuery()
{
// TODO: Add your control notification handler code here
CString querycon ;
GetDlgItem(IDC_QUERYCON)->GetWindowText(querycon);
int quesel ;
quesel = ((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->GetCurSel();
//UpdateData();
CString strSQL;
if(!querycon)
strSQL = "select * from Book";
else
{
if(0==quesel)
strSQL = "select * from Book where Bname = '"+querycon+"'";
else if(1==quesel)
strSQL = "select * from Book where Bclass = '"+querycon+"'";
else if(2==quesel)
strSQL = "select * from Book where BID = '"+querycon+"'";
else
strSQL = "select * from Book where Bauthor = '"+querycon+"'";
}
int nItem ;
_variant_t vBID,vBname,vBauthor,vBpress,vBprice,vBclass,vBstock;
m_mainlist.SetRedraw(FALSE);
m_mainlist.DeleteAllItems();
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
while(!mRecordset->adoEOF)
{
//vBID=(char*)(_bstr_t)(mRecordset->GetCollect("BID"));
vBname=(char*)(_bstr_t)(mRecordset->GetCollect("Bname"));
vBauthor=(char*)(_bstr_t)(mRecordset->GetCollect("Bauthor"));
vBpress=(char*)(_bstr_t)(mRecordset->GetCollect("Bpress"));
vBprice=(char*)(_bstr_t)(mRecordset->GetCollect("Bprice"));
vBclass=(char*)(_bstr_t)(mRecordset->GetCollect("Bclass"));
vBstock=(char*)(_bstr_t)(mRecordset->GetCollect("Bstock"));
nItem=m_mainlist.InsertItem(0xffff,(_bstr_t)vBID);
m_mainlist.SetItem(nItem,1,1,(_bstr_t)vBname,NULL,0,0,0);
m_mainlist.SetItem(nItem,2,1,(_bstr_t)vBauthor,NULL,0,0,0);
m_mainlist.SetItem(nItem,3,1,(_bstr_t)vBpress,NULL,0,0,0);
m_mainlist.SetItem(nItem,4,1,(_bstr_t)vBprice,NULL,0,0,0);
m_mainlist.SetItem(nItem,5,1,(_bstr_t)vBclass,NULL,0,0,0);
m_mainlist.SetItem(nItem,6,1,(_bstr_t)vBstock,NULL,0,0,0);
mRecordset->MoveNext();
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
m_mainlist.SetRedraw(TRUE);
m_mainlist.Invalidate();
m_mainlist.UpdateWindow();
}
/////////////////////////登录///////////////////////
void CLibManSysDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData();
CString strSQL;
ID=m_id;
if(0==m_inselect)
{
strSQL= "select * from Reader where RID = '"+m_id+"'";
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset.CreateInstance("ReaderLogin.Recordset");
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
if(!mRecordset->adoEOF)
{
name=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rname"));
sex=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rsex"));
tel=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rtel"));
addr=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Raddr"));
pwd=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rpassword"));
avail=(long)(mRecordset->GetCollect("Ravailable"));
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("不存在的帐户!");
return;
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!"); //显示错误信息
return ;
}
}
if(1==m_inselect)
{
strSQL= "select * from Admin where AdID = '"+m_id+"'";
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset.CreateInstance("AdminLogin.Recordset");
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
if(!mRecordset->adoEOF)
{
name=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adname"));
sex=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adsex"));
tel=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adtel"));
addr=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adaddr"));
pwd=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adpassword"));
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("不存在的帐户!");
return;
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
}
if(pwd==m_pwd)
{
CDialog::OnOK();
if(m_inselect==0)
{
CReader Reader;
Reader.DoModal();
}
else
{
CAdmin Admin;
Admin.DoModal();
}
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("请输入正确的帐号密码!");
return;
}
}
/////////////////////注册/////////////////////////////////
void CLibManSysDlg::OnBtnEnroll()
{
// TODO: Add your control notification handler code here
CPerInfo Enroll;
if(Enroll.DoModal()==IDOK)
{
if(Enroll.m_PerID&&Enroll.m_PerName&&Enroll.m_PerTel&&Enroll.m_PerPwd)
{
try
{
mRecordset->Open("select * from Reader",_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
if(!mRecordset->Supports(adAddNew))
return;
mRecordset->AddNew();
mRecordset->Fields->GetItem(_variant_t("RID"))->Value=_bstr_t(Enroll.m_PerID);
mRecordset->Fields->GetItem(_variant_t("Rname"))->Value=_bstr_t(Enroll.m_PerName);
mRecordset->Fields->GetItem(_variant_t("Rsex"))->Value=_bstr_t(Enroll.m_PerSex);
mRecordset->Fields->GetItem(_variant_t("Rtel"))->Value=_bstr_t(Enroll.m_PerTel);
mRecordset->Fields->GetItem(_variant_t("Raddr"))->Value=_bstr_t(Enroll.m_PerAddr);
mRecordset->Fields->GetItem(_variant_t("Rmax"))->Value=_bstr_t((long)5);
mRecordset->Fields->GetItem(_variant_t("Ravailable"))->Value=_bstr_t((long)5);
mRecordset->Fields->GetItem(_variant_t("Rpassword"))->Value=_bstr_t(Enroll.m_PerPwd);
mRecordset->Update();
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("注册失败!");
return ;
}
}
else
{
AfxMessageBox("请输入完整的信息!");
return;
}
}
else
return;
}
///////////////////退出////////////////////////
void CLibManSysDlg::OnCancel()
{
// TODO: Add extra cleanup here
mConnection->Close();
if(mRecordset)
mRecordset.Release();
mConnection.Release();
CoUninitialize();
CDialog::OnCancel();
}
另外,还有两个界面也和这个差不多,也有COM初始化,数据库连接,会不会是这里有问题?万分诚恳的请求各种牛人指教呀,再不搞掂这个问题就没得毕业啦,TT
BOOL CLibManSysDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CoInitialize(NULL);
HRESULT hr;
try
{
hr = mConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
_bstr_t strConnect = "Provider=SQLOLEDB; Server=PC-201103311850;Database=LM; uid=sa; pwd=107136;";
mConnection->Open(strConnect,"","",NULL); //连接数据库
}
}
catch( _com_error e )
{
AfxMessageBox("连接数据库失败!");
return FALSE;
}
mRecordset.CreateInstance(__uuidof(Recordset));
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("书名");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("类别");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("编号");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->AddString("作者");
((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->SetCurSel(0);
m_mainlist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
m_mainlist.InsertColumn(0,_T("编号"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(1,_T("书名"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(2,_T("作者"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(3,_T("出版社"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(4,_T("定价"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(5,_T("类别"),LVCFMT_LEFT,50);
m_mainlist.InsertColumn(6,_T("数量"),LVCFMT_LEFT,50);
return TRUE; // return TRUE unless you set the focus to a control
}
void CLibManSysDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLibManSysDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLibManSysDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
////////////////////查询/////////////////////////
void CLibManSysDlg::OnBtnQuery()
{
// TODO: Add your control notification handler code here
CString querycon ;
GetDlgItem(IDC_QUERYCON)->GetWindowText(querycon);
int quesel ;
quesel = ((CComboBox*)GetDlgItem(IDC_COMBO_QUESEL))->GetCurSel();
//UpdateData();
CString strSQL;
if(!querycon)
strSQL = "select * from Book";
else
{
if(0==quesel)
strSQL = "select * from Book where Bname = '"+querycon+"'";
else if(1==quesel)
strSQL = "select * from Book where Bclass = '"+querycon+"'";
else if(2==quesel)
strSQL = "select * from Book where BID = '"+querycon+"'";
else
strSQL = "select * from Book where Bauthor = '"+querycon+"'";
}
int nItem ;
_variant_t vBID,vBname,vBauthor,vBpress,vBprice,vBclass,vBstock;
m_mainlist.SetRedraw(FALSE);
m_mainlist.DeleteAllItems();
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
while(!mRecordset->adoEOF)
{
//vBID=(char*)(_bstr_t)(mRecordset->GetCollect("BID"));
vBname=(char*)(_bstr_t)(mRecordset->GetCollect("Bname"));
vBauthor=(char*)(_bstr_t)(mRecordset->GetCollect("Bauthor"));
vBpress=(char*)(_bstr_t)(mRecordset->GetCollect("Bpress"));
vBprice=(char*)(_bstr_t)(mRecordset->GetCollect("Bprice"));
vBclass=(char*)(_bstr_t)(mRecordset->GetCollect("Bclass"));
vBstock=(char*)(_bstr_t)(mRecordset->GetCollect("Bstock"));
nItem=m_mainlist.InsertItem(0xffff,(_bstr_t)vBID);
m_mainlist.SetItem(nItem,1,1,(_bstr_t)vBname,NULL,0,0,0);
m_mainlist.SetItem(nItem,2,1,(_bstr_t)vBauthor,NULL,0,0,0);
m_mainlist.SetItem(nItem,3,1,(_bstr_t)vBpress,NULL,0,0,0);
m_mainlist.SetItem(nItem,4,1,(_bstr_t)vBprice,NULL,0,0,0);
m_mainlist.SetItem(nItem,5,1,(_bstr_t)vBclass,NULL,0,0,0);
m_mainlist.SetItem(nItem,6,1,(_bstr_t)vBstock,NULL,0,0,0);
mRecordset->MoveNext();
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
m_mainlist.SetRedraw(TRUE);
m_mainlist.Invalidate();
m_mainlist.UpdateWindow();
}
/////////////////////////登录///////////////////////
void CLibManSysDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData();
CString strSQL;
ID=m_id;
if(0==m_inselect)
{
strSQL= "select * from Reader where RID = '"+m_id+"'";
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset.CreateInstance("ReaderLogin.Recordset");
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
if(!mRecordset->adoEOF)
{
name=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rname"));
sex=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rsex"));
tel=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rtel"));
addr=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Raddr"));
pwd=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Rpassword"));
avail=(long)(mRecordset->GetCollect("Ravailable"));
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("不存在的帐户!");
return;
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!"); //显示错误信息
return ;
}
}
if(1==m_inselect)
{
strSQL= "select * from Admin where AdID = '"+m_id+"'";
try
{
//mRecordset.CreateInstance(__uuidof(Recordset));
mRecordset.CreateInstance("AdminLogin.Recordset");
mRecordset->Open(_bstr_t(strSQL),_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
mRecordset->MoveFirst();
if(!mRecordset->adoEOF)
{
name=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adname"));
sex=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adsex"));
tel=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adtel"));
addr=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adaddr"));
pwd=(LPCTSTR)(_bstr_t)(mRecordset->GetCollect("Adpassword"));
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("不存在的帐户!");
return;
}
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("读取数据库失败!");
return ;
}
}
if(pwd==m_pwd)
{
CDialog::OnOK();
if(m_inselect==0)
{
CReader Reader;
Reader.DoModal();
}
else
{
CAdmin Admin;
Admin.DoModal();
}
}
else
{
GetDlgItem(IDC_ERR)->SetWindowText("请输入正确的帐号密码!");
return;
}
}
/////////////////////注册/////////////////////////////////
void CLibManSysDlg::OnBtnEnroll()
{
// TODO: Add your control notification handler code here
CPerInfo Enroll;
if(Enroll.DoModal()==IDOK)
{
if(Enroll.m_PerID&&Enroll.m_PerName&&Enroll.m_PerTel&&Enroll.m_PerPwd)
{
try
{
mRecordset->Open("select * from Reader",_variant_t((IDispatch*)mConnection),adOpenDynamic,adLockOptimistic,adCmdText);
if(!mRecordset->Supports(adAddNew))
return;
mRecordset->AddNew();
mRecordset->Fields->GetItem(_variant_t("RID"))->Value=_bstr_t(Enroll.m_PerID);
mRecordset->Fields->GetItem(_variant_t("Rname"))->Value=_bstr_t(Enroll.m_PerName);
mRecordset->Fields->GetItem(_variant_t("Rsex"))->Value=_bstr_t(Enroll.m_PerSex);
mRecordset->Fields->GetItem(_variant_t("Rtel"))->Value=_bstr_t(Enroll.m_PerTel);
mRecordset->Fields->GetItem(_variant_t("Raddr"))->Value=_bstr_t(Enroll.m_PerAddr);
mRecordset->Fields->GetItem(_variant_t("Rmax"))->Value=_bstr_t((long)5);
mRecordset->Fields->GetItem(_variant_t("Ravailable"))->Value=_bstr_t((long)5);
mRecordset->Fields->GetItem(_variant_t("Rpassword"))->Value=_bstr_t(Enroll.m_PerPwd);
mRecordset->Update();
mRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("注册失败!");
return ;
}
}
else
{
AfxMessageBox("请输入完整的信息!");
return;
}
}
else
return;
}
///////////////////退出////////////////////////
void CLibManSysDlg::OnCancel()
{
// TODO: Add extra cleanup here
mConnection->Close();
if(mRecordset)
mRecordset.Release();
mConnection.Release();
CoUninitialize();
CDialog::OnCancel();
}
另外,还有两个界面也和这个差不多,也有COM初始化,数据库连接,会不会是这里有问题?万分诚恳的请求各种牛人指教呀,再不搞掂这个问题就没得毕业啦,TT