- - PR -
テーブルのデータを取得する
投稿者 | 投稿内容 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
投稿日時: 2008-11-26 14:07
exには、
「Fill: SelectCommand.Connection プロパティが初期化されていません。」 と書いてありました。 どういう意味なんですか? 初心者で大変申し訳ございませんが、 ご指導のほどよろしくお願いいたします。 | ||||||||||||||||||||
|
投稿日時: 2008-11-26 14:17
実際のコードを記載します。
try { using (NpgsqlConnectionManager connectionManager = new NpgsqlConnectionManager(new ConnectionUtility().GetNpgsqlConnection("Local"))) { StringBuilder sql = new StringBuilder(); sql.AppendLine("SELECT"); sql.AppendLine(" customer_d.customer_number,"); sql.AppendLine(" customer_d.customer_name,"); sql.AppendLine(" customer_d.customer_name_kana,"); sql.AppendLine(" customer_d.date_of_birth,"); sql.AppendLine(" customer_d.zip_code,"); sql.AppendLine(" customer_d.address1,"); sql.AppendLine(" customer_d.address2,"); sql.AppendLine(" customer_d.address3,"); sql.AppendLine(" customer_d.telephone_number,"); sql.AppendLine(" customer_d.mobile_phone_number"); sql.AppendLine("FROM"); sql.AppendLine(" customer_d,"); sql.AppendLine(" unregistrated_mels_member_d"); sql.AppendLine("WHERE"); sql.AppendLine(" customer_d.customer_number = '11111'"); sql.AppendLine("AND"); sql.AppendLine(" customer_d.customer_name_kana = unregistrated_mels_member_d.customer_name_kana"); NpgsqlCommand command = new NpgsqlCommand(); command.CommandText = sql.ToString(); DataTable dt = new DataTable(); DataSet ds = new DataSet(); NpgsqlDataAdapter da = new NpgsqlDataAdapter(command); da.Fill(ds,"customer_d"); ←ここでcatchにFillが初期化されてませんとエラー dt = ds.Tables["customer_d"]; int i, j; object a; for (i = 0; i < dt.Rows.Count; i++) { for (j = 0; j < dt.Columns.Count; j++) { a = dt.Rows[i][j]; this.customerNumericText.Text = a.ToString(); } } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } です。 [ メッセージ編集済み 編集者: yyy 編集日時 2008-11-26 14:19 ] | ||||||||||||||||||||
|
投稿日時: 2008-11-26 14:18
「command の Connection プロパティに値がセットされていませんよ」という意味です。 例えば
というコードを書けばいいと思います。 _________________ Nakamura Blog [ メッセージ編集済み 編集者: なかむら 編集日時 2008-11-26 14:20 ] [ メッセージ編集済み 編集者: なかむら 編集日時 2008-11-26 14:22 ] | ||||||||||||||||||||
|
投稿日時: 2008-11-26 14:20
エラーでググったら、参考になりそうなページが見つかりました。 DataGridでの編集について http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=23206&forum=7 #追記 すれ違いでソースが掲載されてました。Connection が null かと思ったのですが、違いましたのでスルーして下さい。 [ メッセージ編集済み 編集者: ひろれい 編集日時 2008-11-26 14:41 ] | ||||||||||||||||||||
|
投稿日時: 2008-11-26 14:21
Kingさんが指摘している
を行っていないためと考えられます。 #以下、OpenしたConnectionの変数をconnectionとして Fillメソッドを呼ぶ前に、
が必要ですが、行っていますか。 または
を
とすれば、Connectionが設定されたCommandを取得することが出来ます。 _________________ まさるblog | ||||||||||||||||||||
|
投稿日時: 2008-11-26 14:21
ということではないのですか? | ||||||||||||||||||||
|
投稿日時: 2008-11-26 14:23
command.Connection = connectionManager;
とかいたら、 型 'AAAANetCommon.DB.Npgsql.NpgsqlConnectionManager' を型 'Npgsql.NpgsqlConnection' に暗黙的に変換できません。 となりましたが、 どうやって変換(キャスト?)をすればいいのでしょうか? | ||||||||||||||||||||
|
投稿日時: 2008-11-26 14:30
connectionManagerはConnectionではありませんのでcast出来ません。 connectionManagerからConnectionを取得する方法を調べてみてください。 _________________ まさるblog |