- PR -

テーブルのデータを取得する

投稿者投稿内容
yyy
会議室デビュー日: 2008/11/21
投稿数: 12
投稿日時: 2008-11-26 14:07
exには、
「Fill: SelectCommand.Connection プロパティが初期化されていません。」

と書いてありました。

どういう意味なんですか?

初心者で大変申し訳ございませんが、
ご指導のほどよろしくお願いいたします。
yyy
会議室デビュー日: 2008/11/21
投稿数: 12
投稿日時: 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/11
投稿数: 67
お住まい・勤務地: 福岡
投稿日時: 2008-11-26 14:18
引用:

exには、
「Fill: SelectCommand.Connection プロパティが初期化されていません。」



「command の Connection プロパティに値がセットされていませんよ」という意味です。

例えば
コード:

using(NpgsqlConnection connection = new NpgsqlConnection(connectionString))
{
connection.Open();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = connection; // ←が抜けている!!!
command.CommandText = sql.ToString();

// ここから後は同じなので省略
}


というコードを書けばいいと思います。



_________________
Nakamura Blog

[ メッセージ編集済み 編集者: なかむら 編集日時 2008-11-26 14:20 ]

[ メッセージ編集済み 編集者: なかむら 編集日時 2008-11-26 14:22 ]
ひろれい
ぬし
会議室デビュー日: 2006/03/02
投稿数: 486
お住まい・勤務地: 万博開催地
投稿日時: 2008-11-26 14:20
引用:

yyyさんの書き込み (2008-11-26 14:07) より:

exには、
「Fill: SelectCommand.Connection プロパティが初期化されていません。」

と書いてありました。

どういう意味なんですか?


エラーでググったら、参考になりそうなページが見つかりました。

DataGridでの編集について
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=23206&forum=7

#追記
すれ違いでソースが掲載されてました。Connection が null かと思ったのですが、違いましたのでスルーして下さい。

[ メッセージ編集済み 編集者: ひろれい 編集日時 2008-11-26 14:41 ]
まさる
ベテラン
会議室デビュー日: 2006/12/21
投稿数: 59
お住まい・勤務地: 越後の中ほど
投稿日時: 2008-11-26 14:21
引用:

yyyさんの書き込み (2008-11-26 14:07) より:
exには、
「Fill: SelectCommand.Connection プロパティが初期化されていません。」

と書いてありました。

どういう意味なんですか?

初心者で大変申し訳ございませんが、
ご指導のほどよろしくお願いいたします。



Kingさんが指摘している
引用:

データベースに接続し OPEN したことはわかりましたが
その接続を Command の Connection プロパティに設定していますか?


を行っていないためと考えられます。

#以下、OpenしたConnectionの変数をconnectionとして

Fillメソッドを呼ぶ前に、
コード:
command.Connection = connection;


が必要ですが、行っていますか。

または
コード:
NpgsqlCommand command = new NpgsqlCommand();



コード:
NpgsqlCommand command = connection.CreateCommand();


とすれば、Connectionが設定されたCommandを取得することが出来ます。


_________________
まさるblog
108
常連さん
会議室デビュー日: 2007/12/12
投稿数: 45
投稿日時: 2008-11-26 14:21
引用:
それと、コードでは command に Connection を設定していないようですが、書き忘れでしょうか?


引用:
connectionは、記載するのを忘れたのですが、


ということではないのですか?
yyy
会議室デビュー日: 2008/11/21
投稿数: 12
投稿日時: 2008-11-26 14:23
command.Connection = connectionManager;

とかいたら、
型 'AAAANetCommon.DB.Npgsql.NpgsqlConnectionManager' を型 'Npgsql.NpgsqlConnection' に暗黙的に変換できません。

となりましたが、
どうやって変換(キャスト?)をすればいいのでしょうか?
まさる
ベテラン
会議室デビュー日: 2006/12/21
投稿数: 59
お住まい・勤務地: 越後の中ほど
投稿日時: 2008-11-26 14:30
引用:

yyyさんの書き込み (2008-11-26 14:23) より:
command.Connection = connectionManager;

とかいたら、
型 'AAAANetCommon.DB.Npgsql.NpgsqlConnectionManager' を型 'Npgsql.NpgsqlConnection' に暗黙的に変換できません。

となりましたが、
どうやって変換(キャスト?)をすればいいのでしょうか?



connectionManagerはConnectionではありませんのでcast出来ません。
connectionManagerからConnectionを取得する方法を調べてみてください。

_________________
まさるblog

スキルアップ/キャリアアップ(JOB@IT)