スキップしてメイン コンテンツに移動

MySQLへのアクセスではまった

MySQLへの参照するプログラムではまった…

今日は郵便番号→住所変換の処理を作成している。 1時間もあればできると思っていたが、おおはまり(^^ゞ


Oracleでは列の連結で 「列名||列名」で連結できるのだが、MySQLでは出来ない…
仕方がないので、add1, add2, add3で取り出すことにした。

SELECT文にパラメータを埋め込んで、郵便番号から住所を取り出すようにしたが、ネットを検索すると、パラメータの指定に「@」を付けていたので、迷わず
select add1, add2, add3 as address from zip where zipcode=@zip
と、書いた。

全く住所が返ってこない…


リファレンスマニュアルをなめ回すと、なんと「?」で指定してある…

結果、以下のようなコードでやっと住所が取ってこれた。あ~疲れた…

MySqlConnection conn = new MySqlConnection(Test1.Properties.Settings.Default.ConnectionString);

string selectQuery = "select add1, add2, add3 as address from zip where zipcode=?zip";
MySqlCommand MyCmd = new MySqlCommand(selectQuery, conn);

conn.Open();

string zip = textBox1.Text.Replace("-", "");
Console.WriteLine("zip=[{0}]", zip);
MyCmd.Parameters.Add("?zip", MySqlDbType.VarChar).Value = zip;

MySqlDataReader reader;
reader = MyCmd.ExecuteReader();

Console.WriteLine("Count:{0}", reader.FieldCount);

reader.Read();
Console.WriteLine("text:[{0}]", reader.GetString(0));
textBox2.Text = reader.GetString(0) + reader.GetString(1) + reader.GetString(2);

reader.Close();
conn.Close();

try等の例外処理は、全く入っていません。悪しからずご了承ください。
 ちなみに、郵便番号のデータは 郵便局のサイト(http://www.post.japanpost.jp/zipcode/dl/kogaki.html)から入手したものを加工して参照しています。

コメント

このブログの人気の投稿

MySQL NotifierがHigh Severity Errorを出して立ち上がらない…

先日、わしのPCがコンセントが抜けかけが原因で落ちて以来、MySQLのNotifierがエラーを出して立ち上がらなくなった… 1週間放置しておいたが、毎日エラーダイアログが表示されるのも目障りなので、対処してみた。 結果、 %APPDATA%\Oracle\MySQL Notifier にある settings.config が壊れていることが判り、削除し解決した。 なんでこんなものが壊れたのかは不明…

Sun Fire V120のディスクの消去

Sunの撤去が決まったので、ディスクの消去のために色々調べてた結果です ■USB光学ドライブからBoot出来るかの資料 Can I boot from a USB disk or USB CD/DVD drive? Generally speaking, booting from USB disk, CD/DVD drive is supported on x86 platforms with S10 update 1 and later if the BIOS has USB 2.0 support. Sparc systems with USB 2.0 support can boot from USB disks if the OBP(Open Boot Prom) is upgraded to version 4.27 or later. Before you can boot Solaris from USB disk, you have to install Solaris on the disk first. Installing on USB memory sticks is not recommended as the number of writes to these sticks is limited. Problems observed on some hardware platforms: -Installation from USB CD/DVD drive starts, but then fails with "not finding media" error. This is because the CD/DVD drive is not mounted under the /cdrom directory as the installation process expects. Possible workaround is to re-plug in the CD/DVD drive, and make sure the device is in the device tree and the /dev/dsk/ link is correctly created, and mount i...