我用的是一个ACCESS数据库 csjtxxgl.mdb 想查询数据库里的表 BusInfo的信息:
通过点击一个按钮,查询出我所输入的“编辑”里的东西~
比如,我在编辑里,输入1
查询出No=1的那一列的值,并且显示出来。有会的高手,详细的解释一下好么???
我是新人,所以,如果不详细,我肯定很晕
谢谢了~~~~~~~
通过点击一个按钮,查询出我所输入的“编辑”里的东西~
比如,我在编辑里,输入1
查询出No=1的那一列的值,并且显示出来。有会的高手,详细的解释一下好么???
我是新人,所以,如果不详细,我肯定很晕
谢谢了~~~~~~~
里面有
我在“编辑”里输入1,把 表中No列为1的那一行的数据显示在DATAGRID(或者DATALIST)中。
这个用 WHERE 子句可以实现,如下:
SELECT * FROM YourTable WHERE no = 1
str.Format("SELECT * FROM BusInfo WHERE no = %d",value);
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------由于目标机器积极拒绝,无法连接。 192.168.1.242:8086
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Net.Sockets.SocketException: 由于目标机器积极拒绝,无法连接。 192.168.1.242:8086源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。 堆栈跟踪:
[SocketException (0x274d): 由于目标机器积极拒绝,无法连接。 192.168.1.242:8086]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +2668969
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +717
CSDN.Community.TopicFileDataCenter.TopicFileComponent.Reply(ReplyGenerateData rgd) +0
CSDN.Community.TopicFileDataCenter.TopicFileDataCenterEntry.Reply(Guid topicId, DateTime createDate, ReplyInfo reply, CommunityUser user, UserSectionProfile usp) +102
CSDN.Community.PointForum.Services.ReplyTopicManager.ReplyTopic(ReplyInfo reply, DateTime topicPostDate, String& errorInfo, CommunityUser user, UserSectionProfile usp) +787
CSDN.Community.PointForum.WebControls.ReplyTopicPage.bt_Submit_Click(Object sender, EventArgs e) +603
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.1433; ASP.NET 版本:2.0.50727.1433
#import "C:\Program Files\Common Files\System\ado\msado15.dll" rename("EOF", "EndOfFile")
#import "C:\Program Files\Common Files\System\ado\MSJRO.DLL" no_namespace rename("ReplicaTypeEnum", "_ReplicaTypeEnum")
将ado2.h加入头文件等等,上面的具体操作随便找一本VC数据库编程的书照着做就可以了
CADORecordset m_pRs;
CADODatabase m_pDb;
CString m_strConn = _T("Driver={Microsoft Access Driver (*.mdb)};Dbq=csjtxxgl.mdb;Uid=yourUserID;Pwd=yourPassword");
CString StrSQL;
StrSQL.Format(_T("SELECT * FROM BusInfo WHERE no = %d"),value);
if (!m_pDb.IsOpen())
{
m_pDb.Open(m_strConn);
}
m_pRs =CADORecordset(&m_pDb);if(m_pRs.Open(StrSQL, CADORecordset::openQuery))
{
if(m_pRs.GetRecordCount()!= 0)
{
//do something
}
}
{
UpdateData(TRUE);
CString m_No;
m_DataGrid.SetRefDataSource("select * from BusInfo where No=mNo");
}
我对编写代码真的不在行所以想当然的这样写,当然肯定要出问题的。
出的问题是:
E:\CityTraffic\CityTraffic\DSEL.cpp(60) : error C2664: 'SetRefDataSource' : cannot convert parameter 1 from 'char [35]' to 'struct IUnknown *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
{
UpdateData(TRUE);
CString m_No;
_ConnectionPtr m_pConnection;
m_pConnection->Execute(_bstr_t("select * from BusInfo where No=m_No"),NULL,adCmdUnknown);
m_DataGrid.SetRefDataSource((LPUNKNOWN)m_pConnection);
m_DataGrid.Refresh;
}
我改用这个代码,结果编译可以。但是不能实现功能,运行的时候,按SEL键的时候,出现错误RUNTIME ERROR的提示!
CString strNo;
editCtrl.GetWindowText(strNo);
CString strSql;
strSql.Format(_T("select * from BusInfo where No=%s"), strNo);
m_pConnection->Execute(strSql,NULL,adCmdUnknown);
1、m_pConnection应该是一个全局变量或者类成员变量,之前就已经创建好了,在函数里就不应该再申明一个栈变量。
2、Refresh应该是一个方法,后面就应该添加括号m_DataGrid.Refresh();
http://www.vckbase.com/document/finddoc.asp?keyword=ado
{
//情况列表框的数据
while(m_BusLst.GetCount()>0)
{
m_BusLst.DeleteString(0);
}
UpdateData(TRUE);
//关闭上一次查询
m_pRs->Close();
//构造SQL检索语句
CString strSql;
strSql.Format("select * from BusInfo where No='%s'",m_strNumber);
//检索
BSTR bsSql=strSql.AllocSysString();
m_pRs->Open(bsSql,m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
//将查询结果在列表框中显示
while(VARIANT_FALSE==m_pRs->EndOfFile)
{
_variant_t vFieldValue;
CString strNo,strStation;
CString strItem;
vFieldValue=m_pRs->GetCollect("No");
strNo=(char*)_bstr_t(vFieldValue);
vFieldValue.Clear();
vFieldValue=m_pRs->GetCollect("Station");
strStation=(char*)_bstr_t(vFieldValue);
vFieldValue.Clear();
strItem.Format("%s %s",strNo,strStation);
m_BusLst.AddString(strItem);
m_pRs->MoveNext();
}
}
用这个语句进行查询,可以编译,运行时出现错误 Debug Assertion Failed!
Program:E:\....(目录)\CityTraffic.exe
File:oleinit.cpp
Line:54
这个是什么错误呢?
_ConnectionPtr m_pCon; //连接对象
_RecordsetPtr m_pRs; //记录集对象
AfxOleInit();
// Set small icon
//创建并打开数据库连接对象
_variant_t vFieldValue;
CString strFieldValue;
m_pCon.CreateInstance(__uuidof(Connection));
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
try
{
// 打开本地Access库csjtxxgl.mdb
m_pCon->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=csjtxxgl.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库csjtxxgl.mdb是否在当前路径下!");
return FALSE;
}
//创建并打开记录集对象
m_pRs.CreateInstance(__uuidof(Recordset));
m_pRs->Open("select* from BusInfo",m_pCon.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
CLoginDlg dlg;
if (dlg.DoModal() != IDOK)
OnOK();
else
CCityTrafficDlg dlg;
dlg.DoModal();
m_ToolBar.Create(this); //创建工具栏
m_ToolBar.LoadToolBar(IDR_TOOLBAR1);//加载工具栏
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);哪个是会出错的呢?应该怎么办?
有没有人能帮忙下!