编写一个代码需要帮助 [英] Need help in writing one code

查看:84
本文介绍了编写一个代码需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我为组合框编写了以下代码



Hi all

I wrote the below code for a comboBox

if (comboBox1.Text == "Name")
            {
                SqlCommand cmd = new SqlCommand("select name from details",con);
                con.Open();





现在我想将结果添加到另一个组合框



假设查询结果为

a

b

c

then它应该显示在第二个组合框中..



请告诉我如何将结果绑定到第二个comboBox



Now i want to add the result to another comboBox

Suppose the result of the query comes as
a
b
c
then it should show in the second comboBox..

please tell me how to bind the result to the second comboBox

推荐答案

if (comboBox1.Text == "Name")
            {
                string cmd = "select * from details";
                SqlDataAdapter da = new SqlDataAdapterh(cmd, con);
                DataTable dt =  new DataTable();
                dah.fill(dt);

                foreach(DataRow dr in dt.Rows)
                {
                   comboBox2.Items.Add(dr["name"].ToString());
                }
            }


首先,您需要执行命令对象。您可以使用Execute方法实现此目的。完整示例如下:



Firstof all you need to execute your command object. You can achieve that with Execute method. A full example follows:

public DateTime GetServerDate()
        {
            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(@"SELECT GETDATE()", cn))
                {
                    cn.Open();

                    return (DateTime)cmd.ExecuteScalar();
                }
            }
        }





在这个例子中我们调用 ExecuteScalar 方法。它将仅返回所获取数据的第一行的第一列。您可以在此处找到有关它的更多信息: http://msdn.microsoft .com / zh-cn / library / system.data.sqlclient.sqlcommand.executescalar.aspx [ ^ ]
SqlCommand对象还提供其他方法 ExecuteReader() ExecuteNonQuery()

基本上你会调用ExecuteReader()方法,然后将结果绑定到你的组合。





In this example we are calling ExecuteScalar method. It will return only the first column of first row of the fetched data. More information about it you can find here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx[^]
SqlCommand object offers also other methods as ExecuteReader() and ExecuteNonQuery().
Basically you will call ExecuteReader() method and then bind the result to your combo.

[DataObject(true)]
public class DataService
{
/// <summary>
/// Retrives all the detail data
/// </summary>
/// <returns>Datatable contining the requested data.</returns>
public static DataTable GetAllByDetail()
{
    DataTable table = new DataTable();

    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("select name from details", cn))
        {
            cn.Open();

            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                table.Load(reader);
            }
        }
    }

    return table;
}
}





现在你可以调用这个方法,并将数据绑定到你的组合。 />




Now you can call this method, and bind the data to your combo.

private DataTable dt = DataService.Retrieve();

ComboBox1.DataSource = dt; 
ComboBox1.DisplayMember = "Name"; 
ComboBox1.ValueMember = "Name";





我希望这能解答你所有的疑惑。



干杯



I hope this answers all of your doubts.

Cheers


这篇关于编写一个代码需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆