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

📄 vb和excel及数据库问题 vb.htm

📁 vb与数据库的一些需要我们注意的地方
💻 HTM
📖 第 1 页 / 共 2 页
字号:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta content="all" name="robots" />
<meta http-equiv="Content-Language" content="zh-CN" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/ui/styles/topic.css" />
<script type="text/javascript" src="/ui/scripts/Csdn/Topic.js"></script>
<title>vb和EXCEL及数据库问题 VB / 基础类 - CSDN社区 community.csdn.net</title>
<meta content="数据库 字段 excel 数据 vb sql server 数据表 表 插入 VB 基础类" name="Keywords" />
<meta content="请教VB一下:    有一个EXECL,有一个SQL SERVER中的表,如何通过VB把excel中的数据,用insert语句插入到sqlserver的表中。不要用SQL SERVER的导入功能。比如:test.xls中有字段a,b,c,da  b  c  d1  a  a  a2  a  a  a3  a  a  asql server表test中有a,b,c,d如何通过VB把test.xls中的数据用insert逐行插入到表test中谢谢!!!" name="description" />
</head>
<body>
<div id="wrapper">
<div id="innerWrapper">
<table id="CSDNPHL" class="CSDN-PHF" border="0" cellspacing="1" cellpadding="0">
<tbody>
<tr>
<td><a rel="external" href='http://www.csdn.net/'>CSDN首页</a></td>
<td><a rel="external" href='http://news.csdn.net/'>新闻</a></td>
<td><a rel="external" href='http://bbs.csdn.net/'><strong>论坛</strong></a></td>
<td><a rel="external" href='http://groups.csdn.net/'><strong>小组</strong></a></td>
<td><a rel="external" href='http://blog.csdn.net/'><strong>Blog</strong></a></td>
<td><a rel="external" href='http://dev.csdn.net/'>文档</a></td>
<td><a rel="external" href='http://download.csdn.net/'>下载</a></td>
<td><a rel="external" href='http://book.csdn.net/'>读书</a></td>
<td><a rel="external" href='http://tag.csdn.net/'><strong>Tag</strong></a></td>
<td><a rel="external" href='http://wz.csdn.net/'><strong>网摘</strong></a></td>
<td><a rel="external" href='http://search.csdn.net/'><strong>搜索</strong></a></td>
<td><a rel="external" href='http://dotnet.csdn.net/'><strong>.NET</strong></a></td>
<td><a rel="external" href='http://java.csdn.net/'><strong>Java</strong></a></td>
<td><a rel="external" href='http://gamedev.csdn.net/'>游戏</a></td>
<td><a rel="external" href='http://live.csdn.net/'>视频</a></td>
<td><a rel="external" href='http://job.csdn.net/'>人才</a></td>
<td><a rel="external" href='http://prj.csdn.net/'>外包</a></td>
<td><a rel="external" href='http://www.dearbook.com.cn/'>第二书店</a></td>
<td><a rel="external" href='http://www.programmer.com.cn/'>程序员</a></td>
</tr>
</tbody>
</table>
<div id="logo">
<a href="http://community.csdn.net/" rel="external"><img class="logo" src="/ui/images/logo_csdn.gif" alt="CSDN社区" /></a><a
href="http://g.csdn.net/5021685" rel="external"><img src="http://topic.csdn.net/images/adv/468.60_csdn.gif" alt=""/></a><a
href="http://g.csdn.net/5021657" rel="external"><img src="http://topic.csdn.net/images/adv/120_60.gif" alt=""/></a>

<img src="http://counter.csdn.net/pv.aspx?id=94" alt="" style="display: none" />
</div><div id="navigation">
<div id="search">
<div id="innerSearch">
<input id="key" onfocus="value=''" /><a href="#" onclick="return Search()">搜索</a>&nbsp;|&nbsp;<a href="JavaScript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();" title="功能强大的网络收藏夹,一秒钟操作就可以轻松实现保存带来的价值、分享带来的快乐" >收藏</a>&nbsp;|&nbsp;<a
        href="#" onclick="return Print()">打印</a>&nbsp;|&nbsp;<a href="#" onclick="return Close()">关闭</a>
</div>
</div>
<div>
<a href="http://community.csdn.net/" rel="external" class="CSDNRoom">CSDN社区</a>&nbsp;>&nbsp;
<a href="http://community.csdn.net/Expert/ForumsList.asp?typenum=1&roomid=51" rel="external">
VB</a>&nbsp;>&nbsp; <a href="http://community.csdn.net/Expert/ForumList.asp?typenum=1&roomid=5101"
    rel="external">基础类</a>
</div>
</div>

<div id="mainWrapper">
<div id="pageContent">
<div id="innerContent">
<h1>vb和EXCEL及数据库问题</h1><div class="author">
<a class="anchor" name="Top">楼主</a>yusuyang()<input type="button" class="user1" title="一级用户 该版得分小于等于100分" />2005-12-20 13:33:46 在  VB / 基础类 提问</div>
<p>
请教VB一下: &nbsp; <br/> &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; 有一个EXECL,有一个SQL &nbsp; SERVER中的表,如何通过VB把excel中的数据,用insert语句插入到sql &nbsp; <br/> &nbsp; server的表中。不要用SQL &nbsp; SERVER的导入功能。 &nbsp; <br/> &nbsp; 比如:test.xls中有字段a,b,c,d &nbsp; <br/> &nbsp; a &nbsp;  &nbsp; b &nbsp;  &nbsp; c &nbsp;  &nbsp; d &nbsp; <br/> &nbsp; 1 &nbsp;  &nbsp; a &nbsp;  &nbsp; a &nbsp;  &nbsp; a &nbsp; <br/> &nbsp; 2 &nbsp;  &nbsp; a &nbsp;  &nbsp; a &nbsp;  &nbsp; a &nbsp; <br/> &nbsp; 3 &nbsp;  &nbsp; a &nbsp;  &nbsp; a &nbsp;  &nbsp; a &nbsp; <br/> &nbsp; sql &nbsp; server表test中有a,b,c,d &nbsp; <br/> &nbsp; 如何通过VB把test.xls中的数据用insert逐行插入到表test中 &nbsp; <br/> &nbsp; 谢谢!!!
<span>问题点数:100、回复次数:11</span><a href="#">Top</a>
</p>
<div class="tagad">
<iframe frameBorder="0" scrolling='no' src="/Include/Board.htm?Tags=数据库,字段,excel,数据,vb,sql,server,数据表,表,插入"></iframe></div>
<h3><strong><a class="anchor" name="r_32788091">1 楼</a>verious(随意秋风)<input type="button" class="user3" title="三级用户 该版得分小于等于1000分,大于500分" /></strong><span>回复于 2005-12-20 15:01:06 得分 <em>10</em></span></h3>
<p>调用ADO,将EXCEL和SQL分别做成两个数据集,然后可以直接用语句在数据集之间交换数据<a href="#">Top</a></p><h3><strong><a class="anchor" name="r_32788294">2 楼</a>yusuyang()<input type="button" class="user1" title="一级用户 该版得分小于等于100分" /></strong><span>回复于 2005-12-20 15:10:35 得分 0
</span></h3>
<p>可以写点例子吗? &nbsp; <br/> &nbsp; 比如,rs1,rs2 &nbsp; <br/> &nbsp; 怎么运用insert???<a href="#">Top</a></p><h3><strong><a class="anchor" name="r_32788643">3 楼</a>dqhuaying(不再留恋)<input type="button" class="user2" title="二级用户 该版得分小于等于500分,大于100分" /></strong><span>回复于 2005-12-20 15:27:49 得分 0
</span></h3>
<p>交换数据集????<a href="#">Top</a></p><h3><strong><a class="anchor" name="r_32788843">4 楼</a>cool_man(土豆)<input type="button" class="user5" title="五级用户 该版得分小于等于5000分,大于2000分" /></strong><span>回复于 2005-12-20 15:38:22 得分 <em>10</em></span></h3>
<p>可以先引用EXCEL对象,再读取EXCEL中数据,然后INSERT进SQL数据库<a href="#">Top</a></p><h3><strong><a class="anchor" name="r_32789595">5 楼</a>yusuyang()<input type="button" class="user1" title="一级用户 该版得分小于等于100分" /></strong><span>回复于 2005-12-20 16:17:50 得分 0
</span></h3>
<p>哥哥们,这些我都知道啊,只是insert时候的值我怎么一行行取啊!<a href="#">Top</a></p><h3><strong><a class="anchor" name="r_32789842">6 楼</a>zou19820704(天下英雄)<input type="button" class="star1" title="一星用户 该版得分小于等于10000分,大于5000分" /></strong><span>回复于 2005-12-20 16:28:02 得分 <em>80</em></span></h3>
<p>呵呵,忘记给个代码了,给你参考一下 &nbsp; <br/> &nbsp; Dim &nbsp; cn &nbsp; As &nbsp; New &nbsp; ADODB.Connection &nbsp; <br/> &nbsp; Dim &nbsp; rs &nbsp; As &nbsp; New &nbsp; ADODB.Recordset &nbsp; <br/> &nbsp; Dim &nbsp; str &nbsp; As &nbsp; String &nbsp; <br/> &nbsp; Dim &nbsp; cn1 &nbsp; As &nbsp; New &nbsp; ADODB.Connection &nbsp; <br/> &nbsp; Dim &nbsp; cmd &nbsp; As &nbsp; New &nbsp; ADODB.Command &nbsp; <br/> &nbsp; Dim &nbsp; rs1 &nbsp; As &nbsp; New &nbsp; ADODB.Recordset &nbsp; <br/> &nbsp;  &nbsp; <br/> &nbsp; cn.ConnectionString &nbsp; = &nbsp; &quot;Provider=Microsoft.Jet.OLEDB.4.0;Persist &nbsp; Security &nbsp; Info=false;DataSource=EXCEL文件名.xls;Extended &nbsp; Properties='Excel &nbsp; 8.0;HDR=Yes'&quot; &nbsp; <br/> &nbsp; cn.Open &nbsp; <br/> &nbsp; rs.Open &nbsp; &quot;select &nbsp; * &nbsp; from &nbsp; [EXCEL工作表名$]&quot;, &nbsp; cn, &nbsp; adOpenKeyset, &nbsp; adLockOptimistic &nbsp; <br/> &nbsp; str &nbsp; = &nbsp; &quot;Driver={SQL &nbsp; Server};Server=数据库服务器名;DataBase=数据库;UID=登陆用户;PWD=密码;&quot; &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; <br/> &nbsp; cn1.Open &nbsp; str &nbsp; <br/> &nbsp; rs1.Open &nbsp; &quot;select &nbsp; * &nbsp; from &nbsp; 数据库表名&quot;, &nbsp; cn1, &nbsp; adOpenKeyset, &nbsp; adLockOptimistic &nbsp; <br/> &nbsp;  &nbsp; <br/> &nbsp; cmd.CommandText &nbsp; = &nbsp; &quot;delete &nbsp; from &nbsp; 数据库表名 &nbsp; &quot; &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; '清空原数据表 &nbsp; <br/> &nbsp; cn1.Execute &nbsp; cmd.CommandText &nbsp; <br/> &nbsp;  &nbsp; <br/> &nbsp; For &nbsp; j &nbsp; = &nbsp; 1 &nbsp; To &nbsp; rs.RecordCount &nbsp; <br/> &nbsp; If &nbsp; rs.EOF &nbsp; = &nbsp; True &nbsp; Then &nbsp; <br/> &nbsp; Exit &nbsp; Sub &nbsp; <br/> &nbsp; Else &nbsp; <br/> &nbsp; rs1.AddNew &nbsp; <br/> &nbsp; For &nbsp; i &nbsp; = &nbsp; 0 &nbsp; To &nbsp; rs.Fields.Count &nbsp; - &nbsp; 1 &nbsp; <br/> &nbsp;  &nbsp; rs1.Fields(i) &nbsp; = &nbsp; rs.Fields(i) &nbsp; <br/> &nbsp; Next &nbsp; i &nbsp; <br/> &nbsp; rs1.Update &nbsp; <br/> &nbsp; rs.MoveNext &nbsp; <br/> &nbsp; End &nbsp; If &nbsp; <br/> &nbsp; Next &nbsp; j &nbsp; <br/> &nbsp; cn.Close &nbsp; <br/> &nbsp; cn1.Close &nbsp; <br/> &nbsp;  &nbsp; <br/> &nbsp;  &nbsp; <br/> &nbsp; 必须将EXCEL表放在程序的同一目录下! &nbsp; <br/> &nbsp; 数据库中要使用的数据表的数据项要和EXCEL表中的一样! &nbsp; <br/> &nbsp; <a href="#">Top</a></p><h3><strong><a class="anchor" name="r_32789926">7 楼</a>yusuyang()<input type="button" class="user1" title="一级用户 该版得分小于等于100分" /></strong><span>回复于 2005-12-20 16:32:12 得分 0
</span></h3>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -