首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当只有3个表时,SQL数据适配器返回4个表的计数。

当只有3个表时,SQL数据适配器返回4个表的计数。
EN

Stack Overflow用户
提问于 2012-05-16 21:27:39
回答 1查看 323关注 0票数 0

我正在通过一个填充一个ComboBox,我遇到了一个问题,在这个问题中,适配器要带回四个表的计数。这很奇怪,因为数据库中只有三个表,它用适当的行填充最终表(ds.Tables3)而不是初始表(ds.Tables)。

以下代码没有填充ComboBox (请注意cboCities.DataSource (第二行到最后一行)):

代码语言:javascript
复制
         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):

代码语言:javascript
复制
        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带回四个表,而不仅仅是城市,为什么要填充第四个表而不是初始表?如果你需要更多的代码样本来帮助我解决这个问题,请告诉我。非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-16 22:01:38

如果您填充了一个DataTable而不是一个DataSet,那么它应该可以工作,因为您只选择了一个表。

除此之外,我必须承认,我不知道这种行为的原因。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10626971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档