⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csdn_文档中心_ado 数据库连接.htm

📁 csdn10年中间经典帖子
💻 HTM
📖 第 1 页 / 共 2 页
字号:
          <TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
          <TD width=500>&nbsp;&nbsp;&nbsp;&nbsp;ADO 数据库</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>
            <P>这些天一直在学习ADO 
            数据库连接,感觉比较复杂,所以一边学习,一边使用,一边做了一些笔记,下面的一些东西就是我总结的一些关于ADO数据库连接方面的东西,因为是笔记,所以不一定很有序,贴出来的意思一方面是可以给大家一个参考,一边也有希望大家一起来完善的意思,其中错误和不足之处当然希望大家可以及时地通知我,在贴上来的时候,我有一个想法,因为我在使用的过程中,总是会遇上一些莫明其妙的错误,所以我想大家有可能也会遇上,于是,我给出了一个错误总结,当然这里就需要大家一起来完善了,如果大家有什么心得和见解,希望在评论中留言,我会及时地接受大家的意见并把他们加进来(当然是在得到您的允许的情况下)</P>
            <P>&nbsp;</P>
            <P>1、导入库文件</P>
            <P>&nbsp;&nbsp; 
            &nbsp;使用ADO前必须在工程的stdafx.h文件最后用直接引入符号#import引入ADO库文件,以使编译器能正确编译。代码如下:<BR>#import 
            "C:\Program Files\common files\system\ado\msado15.dll" no_namespace 
            rename("EOF","EndOfFile") rename("BOF","FirstOfFile")</P>
            <P>&nbsp;ADO类的定义是作为一种资源存储在ADO 
            DLL(msado15.dll)中,在其内部称为类型库。类型库描述了自治接口,以及C++使用的COM 
            vtable接口。当使用#import指令时,在运行时Visual C++需要从ADO 
            DLL中读取这个类型库,并以此创建一组C++头文件。这些头文件具有.tli 
            和.tlh扩展名,读者可以在项目的目录下找到这两个文件。在C++程序代码中调用的ADO类要在这些文件中定义。 <BR>&nbsp; 
             程序的第三行指示ADO对象不使用名称空间。在有些应用程序中,由于应用程序中的对象与ADO中的对象之间可能会出现命名冲突,所以有必要使用名称空间。如果要使用名称空间,则可把第三行程序修改为: 
            rename_namespace("AdoNS")。第四行代码将ADO中的EOF(文件结束)更名为adoEOF,以避免与定义了自己的EOF的其他库冲突。 
            </P>
            <P><BR>2、初始化COM环境<BR>&nbsp;&nbsp; (1)::CoInitialize(NULL); 
            //初始化OLE/COM库环境<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            ::CoUninitialize();//既然初始化了环境,当然就有必要释放他了<BR>&nbsp;&nbsp; 
            (2)也可以调用MFC全局函数<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            AfxOleInit();</P>
            <P>3、三大对象的定义和创建实例</P>
            <P>&nbsp;&nbsp; (1)&nbsp; _ConnectionPtr 
            pConnection("ADODB.Connection");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            _RecordsetPtr&nbsp; 
            pRecordset("ADODB.Recordset");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            _CommandPtr&nbsp;&nbsp;&nbsp; pCommand("ADODN.Command");</P>
            <P>&nbsp;&nbsp; (2) _ConnectionPtr 
            pConnection;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            _RecordsetPtr&nbsp; 
            pRecordset;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            _CommandPtr&nbsp;&nbsp;&nbsp; pCommand;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            pConnection.CreateInstance(__uuidof(Connection));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            pRecordset.CreateInstance(__uuidof(Recordset));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            pCommand.CreateInstance(__uuidof(Command));</P>
            <P>&nbsp;&nbsp; (3) _ConnectionPtr 
            pConnection;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            _RecordsetPtr&nbsp; 
            pRecordset;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            _CommandPtr&nbsp;&nbsp;&nbsp; pCommand;</P>
            <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            pConnection.CreateInstance("ADODB.Connection");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            pRecordset.CreateInstance("ADODB.Recordset");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            pCommand.CreateInstance("ADODB.Command");</P>
            <P>4、打开一个连接<BR>&nbsp; 
            pConnection-&gt;Open(ConnectionString,"","",adModeUnknown);///连接数据库<BR>&nbsp; 
            <BR>&nbsp; 上面的连接字符串ConnectionString根据不同的数据源,分别对应不同的写法<BR>&nbsp; 
            1)访问Access 2000<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=databaseName;User 
            ID=userName;Password=userPassWord"</P>
            <P>&nbsp; 2)访问ODBC数据<BR>&nbsp;&nbsp; 
            "Provider=MADASQL;DSN=dsnName;UID=userName;PWD=userPassword;"<BR>&nbsp; 
            <BR>&nbsp; 3)访问Oracle数据库<BR>&nbsp;&nbsp;&nbsp; 
            “Provider=MSDAORA;Data Sourse=serverName;User 
            ID=userName;Password=userPassword;"</P>
            <P>&nbsp; 3)访问MS SQL数据库<BR>&nbsp;&nbsp; "Provider=SQLOLEDB,Data 
            Source=serverName;Initial Catalog=databaseName;User 
            ID=userName;Password=userPassword;"</P>
            <P>4、执行SQL命令<BR>&nbsp;&nbsp; 
            SQL命令比较多,但是不去考虑细节,这里只说出通用的方法<BR>&nbsp;&nbsp; CString 
            strSQL;//定义SQL命令串,用来保存SQL语句</P>
            <P>&nbsp;&nbsp; strSQL.Format("SQL statement");</P>
            <P>&nbsp;&nbsp; 
            然后在每个要用到SQL命令串的方法中,使用strSQL.AllocSysString()的方法进行类型转换</P>
            <P>5、com的专用数据类型<BR>&nbsp;&nbsp; <BR>&nbsp;&nbsp; variant ,bstr 
            ,SafeArray<BR>&nbsp;&nbsp; <BR>&nbsp;&nbsp; 
            variant变量的范围包括很多,使用_variant_t 进行管理<BR>&nbsp;&nbsp; 
            bstr是一种字符串变量,使用_bstr_t进行管理<BR>&nbsp;&nbsp; 
            <BR>6、关闭连接<BR>&nbsp;&nbsp; <BR>&nbsp;&nbsp; 
            if(m_pConnection-&gt;State)//不能多次关闭,否则会出现错误<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pConnection-&gt;Close(); 
            </P>
            <P>7、结构化异常处理<BR>&nbsp;&nbsp; ADO封装了COM接口,所以需要进行错误处理<BR>&nbsp;&nbsp; 
            如下例:<BR>&nbsp; HRESULT hr;<BR>&nbsp; try<BR>&nbsp; {<BR>&nbsp; hr = 
            m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象<BR>&nbsp; 
            if(SUCCEEDED(hr))<BR>&nbsp; {<BR>&nbsp; hr = 
            m_pConnection-&gt;Open("Provider=Microsoft.Jet.OLEDB.4.0;Data 
            Source=test.mdb","","",adModeUnknown);///连接数据库<BR>&nbsp; 
            ///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;&nbsp; 
            }<BR>&nbsp; }<BR>&nbsp; catch(_com_error e)///捕捉异常<BR>&nbsp; 
            {<BR>&nbsp; CString errormessage;<BR>&nbsp; 
            errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());<BR>&nbsp; 
            AfxMessageBox(errormessage);///显示错误信息<BR>&nbsp; } </P>
            <P><BR>8、错误原因的分析<BR>&nbsp;&nbsp; (1)不支持接口,可能是不能插入空值</P>
            <P>&nbsp;</P>
            <P>好了,希望大家可以跟我一起完善这篇总结!<BR></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_文档中心_ADO 数据库连接.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_文档中心_ADO 数据库连接.files/ico_pencil.gif" width=16> 
      </SPAN>&nbsp;&nbsp;&nbsp;&nbsp; afei2002 <I>(2004-4-22 22:54:30)</I> 
</TD></TR>
  <TR>
    <TD bgColor=#ffffff colSpan=3 width=532><BR>最近转回来学vc,要使用ADO,谢谢~~~ 
  <BR></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_文档中心_ADO 数据库连接.files/ico_pencil.gif" width=16> 
      </SPAN>&nbsp;&nbsp;&nbsp;&nbsp; cuterain <I>(2004-4-21 16:44:12)</I> 
</TD></TR>
  <TR>
    <TD bgColor=#ffffff colSpan=3 width=532><BR>比较适合初学者 
<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>
<SCRIPT language=javascript>
	<!--
	function isEmpty(s)
	{  
		return ((s == null) || (s.length == 0))
	}
	function fubmitok()
	{
		if (isEmpty(document.add_critique.Critique_Content.value))
		{
			alert('评论不能为空!!!!')   ;
			return false;
		}
		document.add_critique.submit();
	}
	//-->
	</SCRIPT>

<DIV align=center>
<TABLE border=0 width=770>
  <TBODY>
  <TR>
    <TD>
      <FORM action=Critique_Sql.asp method=post name=add_critique><INPUT 
      name=Critique_State type=hidden value=add> &nbsp;&nbsp;评论人:xyj0323 
      &nbsp;&nbsp;评论:<BR>&nbsp;&nbsp;<TEXTAREA cols=104 name=Critique_Content rows=8></TEXTAREA><BR>&nbsp;&nbsp;<INPUT name=ubmit onclick=javascript:fubmitok(); type=button value=发表评论> 
      <INPUT name=Topic_id type=hidden value=26995> <INPUT name=From type=hidden 
      value=/Develop/Build_Article.asp?id=26995> 
</FORM></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_文档中心_ADO 数据库连接.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 &copy; 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 + -