简体   繁体   English

如何使用C#从SQL Server检索数据并将其放入组合框?

[英]How to retrieve data from sql server and put into a combo box using C#?

I have a SQL database table create on Visual Studio. 我在Visual Studio上创建了一个SQL数据库表。 Now I want to access that database, read its value based on a certain column and save those value into the combo box list. 现在,我要访问该数据库,根据特定列读取其值,然后将这些值保存到组合框列表中。

For example, if I have a table like this 例如,如果我有一个这样的表

|StudentName    | Age    | ID    |
|---------------|--------|-------|
|A              | 19     | 1     |
|---------------|--------|-------|
|B              | 15     | 2     |
|---------------|--------|-------|
|C              | 20     | 3     |
|---------------|--------|-------|

and a combo box named nameCombo, what I want is to have something like this 和一个名为nameCombo的组合框,我想要的是这样的东西

nameCombo.Items.Add(A);
nameCombo.Items.Add(B);
nameCombo.Items.Add(C);

How am I going to do this ? 我该怎么做? Thanks. 谢谢。

EDIT 编辑

Assuming that you have already connect LINQ to your SQL database. 假设您已经将LINQ连接到SQL数据库。 Here is what you should do to update your combo box. 这是您应该执行的以更新您的组合框的操作。 All thanks to those awesome guys here in stackoverflow. 多亏了那些stackoverflow中的优秀人员。

locationLinqToSQLDataContext db = new locationLinqToSQLDataContext();
var nameData = from name in db.Locations
               select new { name.StudentName };

foreach (var name in nameData)
    {
        fromTextBox.Items.Add(name.StudentName);
    }            

Ok assuming LINQ then: 确定LINQ,然后:

Query it out into a list. 将其查询到列表中。

var myData = (from d in db.MyTable
              where d.Name.contains("A")
              select d).ToList();

Then assign it to the combo: 然后将其分配给组合:

mycombo.datasource=myData;
mycombo.dataTextField="Name";
mycombo.dataValueField="ID";
mycombo.dataBind();

Just made this up on the fly but it should work. 只是在飞行中弥补了这一点,但它应该起作用。

OR If you really want to iterate the items. 或者如果您真的要迭代这些项目。

myData.foreach(delegate(MyTableItem i) 
{
    mycombo.add(new listitem(i.id,i.name));
});

If you need it for windows form application here is the code: 如果您需要Windows窗体应用程序,请使用以下代码:

       DataTable dt = new DataTable("dataTable");

        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        //add DataRow
        DataRow row = dt.NewRow();
        row["Id"] = 1;
        row["Name"] = "One";

        dt.Rows.Add(row);
        //assign to ComboBox
        comboBox1.DataSource = dt;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Id";

For web applications you can see the other answer 对于Web应用程序,您可以看到其他答案

Here is a solution that uses SqlConnection (which naively assumes you don't need data binding). 这是使用SqlConnection的解决方案(天真地假定您不需要数据绑定)。 Of course you must replace "YourConnectionString" and "YourTable" with real values. 当然,您必须用实际值替换“ YourConnectionString”和“ YourTable”。

string connectionString = "YourConnectionString";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    string query = "SELECT Name FROM YourTable";
    SqlCommand command = new SqlCommand(query, connection);
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            // "0" refers to the 0th column in the result set.
            nameCombo.Items.Add(reader.GetString(0));
        }
    }
}

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

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