<?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>chibiegg日誌 &#187; VisualBasic</title>
	<atom:link href="http://blog.chibiegg.net/tag/visualbasic/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.chibiegg.net</link>
	<description>chibiegg’s Diary</description>
	<lastBuildDate>Fri, 06 Jan 2012 01:51:23 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>DataGridViewにMySQLからデータを取得[VB]</title>
		<link>http://blog.chibiegg.net/2007/11/28_23_97.htm</link>
		<comments>http://blog.chibiegg.net/2007/11/28_23_97.htm#comments</comments>
		<pubDate>Wed, 28 Nov 2007 14:20:00 +0000</pubDate>
		<dc:creator>chibiegg</dc:creator>
				<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[Windowsプログラミング]]></category>
		<category><![CDATA[VisualBasic]]></category>

		<guid isPermaLink="false">http://chibiegg.homeip.net/2007/11/28_23_97.htm</guid>
		<description><![CDATA[ちょっとVB 2005からMySQLのデータを使うということをやりたいのでわかった順番にいろいろと書いていこうと思います。 今回はまずDataGridViewをつかってMySQLのデータベースからテーブルの中身を取得し表 [...]]]></description>
			<content:encoded><![CDATA[<p>ちょっとVB 2005からMySQLのデータを使うということをやりたいのでわかった順番にいろいろと書いていこうと思います。</p>
<p>今回はまずDataGridViewをつかってMySQLのデータベースからテーブルの中身を取得し表示しようと思います。<br />
サンプルにつかったデータは日本郵便の大阪府の<a href="http://www.post.japanpost.jp/zipcode/dl/kogaki.html">郵便番号データ</a>です。<br />
<span id="more-97"></span><br />
と思ったらphpMyAdminから郵便番号情報のインポートを説明する<a href="http://plaza.rakuten.co.jp/pgmemo/diary/200512110000/">サイト</a>がありました。</p>
<p>で、「japanpost」というデータベースに「zipcode」というテーブルを作成しそこにデータをインポートしました。<br />
特にデータベースの内容は問わないのでこれにあわせる必要はありません。</p>
<p>追記：データベースの照合順序はsjis_japanese_ciにします。UTF8ではWindowsでODBCから開いたときに文字化けしました。</p>
<p>それと、ODBC経由でMySQLに接続するデータベースドライバ「<a href="http://dev.mysql.com/downloads/connector/odbc/5.1.html">MySQL Connector/ODBC 5.1</a>」を<a href="http://dev.mysql.com/downloads/connector/odbc/5.1.html">ここ</a>からダウンロードしインストールしておきます。</p>
<p>ではプログラムの作成をします。</p>
<p><code>"DRIVER={MySQL ODBC 5.1 Driver};SERVER=(サーバーのアドレス);DATABASE=(データベース名);UID=(ユーザー名);PWD=(パスワード);OPTION=3"</code><br />
の様な接続文字列を作りサーバーにODBCで接続し、</p>
<p><code>"SELECT * FROM zipcode"</code><br />
というSQLを発行します。<br />
(ここを変えることで違うクエリを発行できます。)</p>
<p>そしてその結果を<br />
<code>DAdapter.Fill(DSet, "テーブル名")</code><br />
Fill関数を使ってDataSetに(テーブル名)という名前のテーブルで登録します。<br />
このテーブル名はプログラム上に作成されるテーブルなのでMySQLサーバーでのテーブル名とは異なるものでも構いません。</p>
<p>最後に<br />
<code>DataGridView1.DataSource = DSet.Tables("テーブル名")</code><br />
でDataSetの(テーブル名)というテーブルをDataGridViewのデータソースに指定します。</p>
<p>具体的なコードは以下を参照してください。<br />
サーバーアドレス「192.168.1.50」にユーザー名「test」パスワード「testpass」で接続し、<br />
データベース「japanpost」のテーブル「zipcode」を取得しDetaGridViewに表示するサンプルです。</p>
<p>実行してボタンを押した結果<br />
<a href='http://blog.chibiegg.net/wp-content/uploads/2007/11/sqltest-form1-exec.jpg' title='sqltest-form1-exec.jpg'><img src='http://blog.chibiegg.net/wp-content/uploads/2007/11/sqltest-form1-exec.thumbnail.jpg' alt='sqltest-form1-exec.jpg' /></a></p>
<p>「Form1」に以下のようにコントロールを配置します。</p>
<p>「DataGridView1」という名前でDetaGridViewを(濃い灰色の枠)<br />
「Button1」という名前でButtonを作ります(「実行」というボタン)<br />
<img src='http://blog.chibiegg.net/wp-content/uploads/2007/11/sqltest-form1.jpg' alt='sqltest-form1.jpg' /></p>
<p>Form1のコード</p>
<pre><code>Public Class Form1
    Dim DSet As New System.Data.DataSet
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ConnectionString As String = "DRIVER={MySQL ODBC 5.1 Driver};" &#038; _
                                            "SERVER=192.168.1.50;" &#038; _
                                            "DATABASE=japanpost;" &#038; _
                                            "UID=test;PWD=testpass;OPTION=3"

        Dim connection As New System.Data.Odbc.OdbcConnection(ConnectionString)
        connection.Open()

        Dim SQLTest As String = "SELECT * FROM zipcode"
        Dim DAdapter As New System.Data.Odbc.OdbcDataAdapter(SQLTest, connection)

        DAdapter.Fill(DSet, "zipcode")
        DataGridView1.DataSource = DSet.Tables("zipcode")

        connection.Close()
    End Sub
End Class</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.chibiegg.net/2007/11/28_23_97.htm/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.chibiegg.net/tag/visualbasic/feed ) in 0.23356 seconds, on Feb 7th, 2012 at 12:27 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 7th, 2012 at 1:27 pm UTC -->
