我正在通过一个填充一个ComboBox,我遇到了一个问题,在这个问题中,适配器要带回四个表的计数。这很奇怪,因为数据库中只有三个表,它用适当的行填充最终表(ds.Tables3)而不是初始表(ds.Tables)。
以下代码没有填充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[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带回四个表,而不仅仅是城市,为什么要填充第四个表而不是初始表?如果你需要更多的代码样本来帮助我解决这个问题,请告诉我。非常感谢!
发布于 2012-05-16 22:01:38
如果您填充了一个DataTable而不是一个DataSet,那么它应该可以工作,因为您只选择了一个表。
除此之外,我必须承认,我不知道这种行为的原因。
https://stackoverflow.com/questions/10626971
复制相似问题