📄 csdn_文档中心_单独使用crecordset.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0051)http://www.csdn.net/develop/Read_Article.asp?id=283 -->
<!--内容开始//--><HTML><HEAD><TITLE>CSDN_文档中心_单独使用CRecordSet</TITLE>
<META content="MSHTML 5.00.3315.2870" name=GENERATOR>
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK
href="CSDN_文档中心_单独使用CRecordSet.files/csdn.css" rel=stylesheet></HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<DIV align=center>
<STYLE>.title0 {
COLOR: #ffffff; FONT-FAMILY: arial; FONT-SIZE: 17px; FONT-WEIGHT: bold; TEXT-DECORATION: none
}
A.title2:link {
COLOR: #000000; TEXT-DECORATION: none
}
A.title2:visited {
COLOR: #000000; TEXT-DECORATION: none
}
A.title2:active {
COLOR: #ff0000; TEXT-DECORATION: none
}
A.title2:hover {
COLOR: #ff0000; TEXT-DECORATION: none
}
</STYLE>
<SCRIPT language=JavaScript>
<!--
function submitlog(sign)
{
var types=document.alogon.type.options[document.alogon.type.selectedIndex].value;
if (document.alogon.name.value!="" && document.alogon.pass.value!="")
{
if (types!="1")
document.alogon.action="http://www.csdn.net/member/logon.asp";
else
document.alogon.action="http://expert.csdn.net/member/logon.asp";
if(sign)
document.alogon.submit();
//return true;
}
else
{
if (document.alogon.name.value=="")
alert("请输入用户名!");
if (document.alogon.pass.value=="")
alert("请输入密码");
return false;
}
return true;
}
function GetMyPass()
{
if (document.alogon.name.value!="")
{
document.alogon.action="http://www.csdn.net/member/getpass.asp";
document.alogon.submit();
}
else
{
alert("请在姓名输入框里输入用户名或注册EMAIL!");
}
}
// -->
</SCRIPT>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=770>
<TBODY>
<TR>
<TD bgColor=#004d99 height=25 width=20></TD>
<TD bgColor=#004d99> <A
href="http://www.csdn.net/member/login.asp"><FONT color=#ffff33>登
录..</FONT></A> <A href="http://www.csdn.net/member/zc.asp"
target=_blank><FONT color=#ffff33>[注 册]</FONT></A> <A
href="http://www.csdn.net/member/login.asp"><FONT
color=#ffff33>忘记密码</FONT> </A> <A
href="http://www.csdn.net/member/logonout.asp"><FONT
color=#ffff33>注销登录</FONT> </A></TD>
<TD align=right bgColor=#990000 width=20><IMG height=25
src="CSDN_文档中心_单独使用CRecordSet.files/top_1.gif" width=20></TD>
<FORM action=http://www.csdn.net/search_ok.asp method=post target=_blank>
<TD align=right bgColor=#009999 width=250><FONT color=#ffffff><FONT
color=#ffffff><INPUT class=FormText_1 name=search size=15> <SELECT
class=FormText_1 name=type> <OPTION selected value=1>新闻标题</OPTION>
<OPTION value=2>软件名称</OPTION> <OPTION value=3>商品名称</OPTION> <OPTION
value=4>文档标题</OPTION> <OPTION value=6>职位名称</OPTION></SELECT> </FONT><INPUT class=td3 name=Submit type=submit value="搜 索">
</FONT></TD></FORM></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=770>
<TBODY>
<TR>
<TD align=middle height=70 width=150><IMG height=60
src="CSDN_文档中心_单独使用CRecordSet.files/csdn.gif" width=120></TD>
<TD align=middle width=470>
<SCRIPT language=javascript>
var rand = Math.random().toString();
var ordval = parseInt(rand.substring(2,4));
function Showad(width)
{
if(width=='468')
{
document.write("<a href='http://www.dearbook.com.cn/' target='_blank'><OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' WIDTH=468 HEIGHT=60><PARAM NAME=movie VALUE='/adv//468x60.swf'> <PARAM NAME=quality VALUE=high> <EMBED src='/adv//468x60.swf' quality=high border=0 WIDTH=468 HEIGHT=60 TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'></EMBED></OBJECT></a>");
return;
}
if(width=='120')
{
document.write("<a href='http://www.csdn.net/csdn/csdnClick.aspx?ad=Oracle-button1&adurl&adurl=http://www.oracle.com/global/cn/documentation/10g/index.html?content.html' target='_blank'><img src='/adv/develop/grid_computing_120x60_text1.gif' width=120 HEIGHT=60 border=0></a>");
return;
}
return;
}
Showad('468')</SCRIPT>
</TD>
<TD align=middle width=150>
<SCRIPT>Showad('120')</SCRIPT>
</TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=770>
<TBODY>
<TR>
<TD align=middle bgColor=#000000 colSpan=2 height=1 width=770></TD></TR>
<TR>
<TD align=middle bgColor=#004d99 height=28 width=150><FONT
class=title0>csdn.net</FONT></TD>
<TD align=middle width=620>
<SCRIPT src="CSDN_文档中心_单独使用CRecordSet.files/navigation1.js"></SCRIPT>
</TD></TR>
<TR>
<TD align=middle bgColor=#000000 colSpan=2 height=1
width=770></TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=770>
<TBODY>
<TR>
<TD bgColor=#f3f9fc height=23 width=10></TD>
<TD bgColor=#f3f9fc>
<MARQUEE onmouseout=this.start() onmouseover=this.stop() scrollAmount=5
scrollDelay=80>
<SCRIPT src="CSDN_文档中心_单独使用CRecordSet.files/marquee.htm"></SCRIPT>
</MARQUEE></TD>
<TD align=right bgColor=#f3f9fc width=101>
<SCRIPT>document.write("<img src=http://count.csdn.net/count/pageview1.asp?columnid=4&itemid=11 border=0 width=0 height=0>");</SCRIPT>
<B>
<SCRIPT language=JavaScript>
<!--
document.ns = navigator.appName == "Netscape"
tmpDate = new Date();
date = tmpDate.getDate();
month= tmpDate.getMonth() + 1 ;
if(document.ns)
{
year1=tmpDate.getYear()
year= year1.toString().substr(1,2);
}
else
year= tmpDate.getYear();
document.write(year);
document.write(".");
document.write(month);
document.write(".");
document.write(date);
// -->
</SCRIPT>
</B> </TD></TR>
<TR bgColor=#999999>
<TD colSpan=3 height=1></TD></TR></TBODY></TABLE>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD align=middle bgColor=#fafafa class=td1 vAlign=top width=150><BR>
<SCRIPT src="CSDN_文档中心_单独使用CRecordSet.files/microsoft.js"></SCRIPT>
</TD>
<TD align=middle width=620>
<TABLE bgColor=#eeeeee border=0 cellPadding=0 cellSpacing=0 width=600>
<TBODY>
<TR bgColor=#ffffff>
<TD align=middle height=10 width=50></TD>
<TD align=right><A href="http://www.csdn.net/">CSDN</A> - <A
href="http://www.csdn.net/develop/">文档中心</A> - <FONT
color=#003399>Visual C++</FONT> </TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399 height=10><FONT
color=#ffffff>标题</FONT></TD>
<TD><B> 单独使用CRecordSet</B> vchelpdev(原作)
</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
<TD width=500> 单独使用CRecordSet</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
<TABLE border=0 width=600>
<TBODY>
<TR>
<TD align=left><BR>闻怡洋 <A href="mailto:wyy_cq@21cn.com"><FONT
class=engul>wyy_cq@21cn.com</FONT></A> <A
href="http://www.vchelp.net/"><FONT
class=engul>http://www.vchelp.net/</FONT></A>
<P>一般情况下AppWizard会在数据库应用程序中自动产生CRecordset的派生类,并将派生类和某个数据源中的表联系起来也可以和视图上的子窗口联系起来。但是有时这样做会影响到程序的灵活性,这时候我们可以单独使用CRecordSet类。利用CRecordSet类我们可以执行SQL语句,并可以读出结果集中数据。</P>
<P>首先我们需要包含头文件afxdb.h,可以将#include
<AFXDB.H>添加到stdafx.h文件中。此外在使用CRecordset时必须有一个又一个CDatabase对象,该对象的作用是管理数据源连接。然后可以产生一个CRecordset对象,利用BOOL
CRecordset::Open( UINT nOpenType = AFX_DB_USE_DEFAULT_TYPE, LPCTSTR
lpszSQL = NULL, DWORD dwOptions = none )可以执行SQL语句。</P>
<P>但执行成功后,可以调用以下的函数滚动光标,读取数据。
<TABLE align=center border=1 width="90%">
<TBODY>
<TR>
<TD>MoveFirst</TD>
<TD>移动光标到第一条记录处</TD></TR>
<TR>
<TD>MoveNext</TD>
<TD>移动光标到后一条记录处</TD></TR>
<TR>
<TD>MovePrev</TD>
<TD>移动光标到前一条记录处</TD></TR>
<TR>
<TD>MoveLast</TD>
<TD>移动光标到最后一条记录处</TD></TR>
<TR>
<TD>IsBOF</TD>
<TD>检测光标是否在第一条记录上</TD></TR>
<TR>
<TD>IsEOF</TD>
<TD>检测光标是否在最后一条记录上</TD></TR>
<TR>
<TD>GetFieldValue</TD>
<TD>得到结果中数据</TD></TR></TBODY></TABLE>下面是具体代码: <PRE>/*
假设CDatabase m_dbConn为成员变量
假设有一个表有如下SQL语句产生:CREATE TABLE table1(loc_id not null)
*/
void CYourClass::ConnectToDB()
{//连接数据库
BOOL fOK = m_dbConn.Open("test");
TRACE("connect fOK=%d\n",m_dbConn);
}
void CYourClass::Select()
{//执行SELECT语句
CRecordset rec(&m_dbConn);
BOOL fOK = rec.Open(CRecordset::forwardOnly,"select loc_id from table1 order by loc_id");
TRACE("select fOK = %d\n",fOK);
TRACE("返回的列数为:%d\n",rec.GetRowsetSize());
CString szResult;
while(!rec.IsEOF())
{
rec.GetFieldValue((int)0,szResult);
rec.MoveNext();
TRACE("fetch : %s\n",szResult);
}
}
</PRE>
<P></P>
<P>此外CRecordset::GetFieldValue有很多种原型,你可以通过指定列位置或是字段名来获取数据:</P>
<P>void GetFieldValue( LPCTSTR <I>lpszName</I>, CDBVariant&
<I>varValue</I>, short <I>nFieldType</I> = DEFAULT_FIELD_TYPE
);<BR><BR>void GetFieldValue( short <I>nIndex</I>, CDBVariant&
<I>varValue</I>, short <I>nFieldType</I> = DEFAULT_FIELD_TYPE
);<BR><BR>void GetFieldValue( LPCTSTR <I>lpszName</I>, CString&
<I>strValue</I> );<BR><BR>void GetFieldValue( short <I>nIndex</I>,
CString& <I>strValue</I> );<BR></P>
<P>如果使用CDBVariant类型变量来获取结果,你可以得到任何类型的结果。在CDBVariant::m_dwType成员变量中记录了该变量所包含的数据类型,根据该变量的值你可以确定数据类型并引用CDBVariant对象中的相应成员变量。</P><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0
width=770>
<TBODY>
<TR bgColor=#006699>
<TD align=middle bgColor=#006699 id=white><FONT
color=#ffffff>对该文的评论</FONT></TD>
<TD align=middle>
<SCRIPT src="CSDN_文档中心_单独使用CRecordSet.files/readnum.htm"></SCRIPT>
</TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16
hspace=1 src="CSDN_文档中心_单独使用CRecordSet.files/ico_pencil.gif" width=16>
</SPAN> seashoreking <I>(2001-10-30 15:53:27)</I>
</TD></TR>
<TR>
<TD bgColor=#ffffff colSpan=3 width=532><BR>BOOL fOK =
rec.Open(CRecordset::forwardOnly,"select loc_id from table1 order by
loc_id"); 其中的打开方式(nOpenType)只能用CRecordset::forwardOnly么?
<BR></TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TH bgColor=#006699 id=white><FONT
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<DIV align=center>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD>你没有登陆,无法发表评论。 请先<A
href="http://www.csdn.net/member/login.asp?from=/Develop/read_article.asp?id=283">登陆</A>
<A
href="http://www.csdn.net/expert/zc.asp">我要注册</A><BR></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
<TBODY>
<TR align=middle>
<TD height=10 vAlign=bottom><A
href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
border=0 height=48 src="CSDN_文档中心_单独使用CRecordSet.files/biaoshi.gif"
width=40></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.net, Inc. All rights
reserved</FONT></TD></TR>
<TR>
<TD height=15></TD>
<TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -