<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Full-house &#187; excel</title>
	<atom:link href="http://full-house.cn/index.php/index.php/excel/feed" rel="self" type="application/rss+xml" />
	<link>http://full-house.cn</link>
	<description>SELECT blog FROM TonyHu.full-house WHERE subject IN (Life,Web,Database)</description>
	<lastBuildDate>Wed, 25 Aug 2010 07:40:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>用Excel VBA 读取 SQL Server 数据</title>
		<link>http://full-house.cn/index.php/200912/excel-vba-sql-server.html</link>
		<comments>http://full-house.cn/index.php/200912/excel-vba-sql-server.html#comments</comments>
		<pubDate>Wed, 30 Dec 2009 14:49:59 +0000</pubDate>
		<dc:creator>Tony hu</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[vba]]></category>

		<guid isPermaLink="false">http://full-house.cn/?p=611</guid>
		<description><![CDATA[Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cn As New ADODB.Connection &#8216;定义数据链接对象 ，保存连接数据库信息；请先添加ADO引用
Dim rs As New ADODB.Recordset   &#8216;定义记录集对象，保存数据表
Dim strCn As String, strSQL As String &#8216;字符串变量
strCn = &#8220;Provider=sqloledb;Server=SCL;Database=grm;uid=grm;pwd=123456;&#8221;     &#8216;定义数据库链接字符串
Set sht = ThisWorkbook.Worksheets(&#8220;R-FN203-R1&#8243;)    &#8216;把sht指向当前工作簿的sheet1工作表
&#8216;下面的语句将读取数据表数据，并将它保存到excel工作表中：画两张表想像一下，工作表为一张两维表，记录集也是一张两维表
strSQL = &#8220;SELECT distinct [ShelfAddress]  FROM [GravureRoll] where  [ArtworkNum]=&#8217;&#8221; + Trim(sht.Cells(16, 7)) + &#8220;&#8216; [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Private Sub Worksheet_SelectionChange(ByVal Target As Range)<br />
Dim cn As New ADODB.Connection &#8216;定义数据链接对象 ，保存连接数据库信息；请先添加ADO引用<br />
Dim rs As New ADODB.Recordset   &#8216;定义记录集对象，保存数据表<br />
Dim strCn As String, strSQL As String &#8216;字符串变量<br />
strCn = &#8220;Provider=sqloledb;Server=SCL;Database=grm;uid=grm;pwd=123456;&#8221;     &#8216;定义数据库链接字符串<br />
Set sht = ThisWorkbook.Worksheets(&#8220;R-FN203-R1&#8243;)    &#8216;把sht指向当前工作簿的sheet1工作表</p>
<p>&#8216;下面的语句将读取数据表数据，并将它保存到excel工作表中：画两张表想像一下，工作表为一张两维表，记录集也是一张两维表</p>
<p>strSQL = &#8220;SELECT distinct [ShelfAddress]  FROM [GravureRoll] where  [ArtworkNum]=&#8217;&#8221; + Trim(sht.Cells(16, 7)) + &#8220;&#8216; and [State]=1&#8243;</p>
<p>cn.Open strCn    &#8216;与数据库建立连接，如果成功，返回连接对象cn<br />
rs.Open strSQL, cn   &#8216;执行strSQL所含的SQL命令，结果保存在rs记录集对象中<br />
sht.Cells(16, 8) = rs(&#8220;ShelfAddress&#8221;) &#8216;将得到的值放到Excel的单元格中。<br />
rs.Close<br />
cn.Close</p>
<p>End Sub</p></blockquote>
<p>注意点是要<strong>先添加ADO引用,否则会定义失败</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://full-house.cn/index.php/200912/excel-vba-sql-server.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
