繁体   English   中英

SQL Server与C#中数据库的独特连接

[英]SQL Server distinct connection to database in C#

我正在尝试在设计中下拉框,但数据库表类别中有重复项。 我试图通过使用下面的代码执行。 但是它没有执行。 它只接收我更改过的所有命令的属性:

下拉菜单的属性

cmd.CommandText = @"Select  Distinct Category_Desc from 
                    Database***name  order 
                    by Category_Desc";
adapter.SelectCommand = cmd;

SqlDataReader dr1 = cmd.ExecuteReader();

dr1.Read();

comboBoxCategory.ValueMember = "Category_Desc";
comboBoxCategory.DisplayMember = "Category_Desc";
comboBoxCategory.DataSource = dr1;

dr1.Dispose();

谁能帮忙如何从代码中执行不同的查询?

数据读取器是仅向前的游标,您必须在最后一项之后迭代并关闭。请看此代码段

            SqlDataReader dr1= command.ExecuteReader();
            ArrayList arl= new ArrayList();
            while (dr1.Read())
            {
             arl.Add(dr1("Category_Desc"));
            }

            dr1.close();
         //If its a winform project use this               
           string [] str = al.ToArray(typeof(string));
            FarPoint.Win.Spread.ComboBoxCellType cb = new 
             FarPoint.Win.Spread.ComboBoxCellType();
            cb.Items = arl;

使用适配器来填充数据表。 您已经有适配器,并且已经分配了SelectCommand。

adapter.SelectCommand = cmd;
System.Data.DataTable dtCategories = new System.Data.DataTable();
adapter.Fill(dtCategories);

comboBoxCategory.ValueMember = "Category_Desc";
comboBoxCategory.DisplayMember = "Category_Desc";
comboBoxCategory.DataSource = dtCategories;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM