0

SQL Data Adapter を介して ComboBox に入力していますが、アダプターが 4 つのテーブルのカウントを戻すという問題に遭遇しました。データベース内にテーブルが 3 つしかなく、最初のテーブル (ds.Tables[0]) ではなく最終テーブル (ds.Tables[3]) に適切な行が入力されているため、これは奇妙です。

次のコードは ComboBox にデータを入力しません (cboCities.DataSource (最後の 2 行目) に注意してください)。

         private void Form1_Load(object sender, EventArgs e)
    {
        // Establishes a connection to the database.
        SqlCeConnection cn = new SqlCeConnection(@"Data Source = C:\Users\user\Desktop\Projects\ParkSurvey WF\ParkSurvey WF\ParkSurvey.sdf; Persist Security Info = False; Password = *");
        cn.Open();
        // Gathering the names of cities from the Cities database to populate cboCities.
        SqlCeCommand cmd = cn.CreateCommand();
        cmd.CommandText = "SELECT CityId, Name FROM Cities ORDER BY Name ASC";
        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        // Closing the connection and setting the data bindings for cboCities.
        cn.Close();
        cboCities.ValueMember = "CityId";
        cboCities.DisplayMember = "Name";
        cboCities.DataSource = ds.Tables[0];
        cboCities.SelectedIndex = -1;
    }

これにより、ComboBox が適切に読み込まれます (cboCities.DataSource にもう一度注意してください)。

        private void Form1_Load(object sender, EventArgs e)
    {
        // Establishes a connection to the database.
        SqlCeConnection cn = new SqlCeConnection(@"Data Source = C:\Users\user\Desktop\Projects\ParkSurvey WF\ParkSurvey WF\ParkSurvey.sdf; Persist Security Info = False; Password = *");
        cn.Open();
        // Gathering the names of cities from the Cities database to populate cboCities.
        SqlCeCommand cmd = cn.CreateCommand();
        cmd.CommandText = "SELECT CityId, Name FROM Cities ORDER BY Name ASC";
        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        // Closing the connection and setting the data bindings for cboCities.
        cn.Close();
        cboCities.ValueMember = "CityId";
        cboCities.DisplayMember = "Name";
        cboCities.DataSource = ds.Tables[3];
        cboCities.SelectedIndex = -1;
    }

DataAdapter が JUST Citiesではなく 4 つのテーブルを返す原因は何ですか? また、最初のテーブルではなく 4 番目のテーブルに値を設定するのはなぜですか? この問題の解決に役立つコード サンプルがさらに必要な場合はお知らせください。どうもありがとうございました!

4

1 に答える 1

1

とにかくテーブルを1つだけ選択しているので、 a のDataTable代わりに a を入力するとうまくいくはずです。DataSet

それとは別に、この動作の理由がわからないことを認めなければなりません。

于 2012-05-16T22:01:38.037 に答える