本文介绍了如何获取存储在组合框中显示的数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.NET和SQL SERVER 2008制作窗口应用程序.我希望将存储在SQL SERVER中的数据显示在组合框上.我尝试了以下代码,但未能成功.

I am making window application using C#.NET and SQL SERVER 2008.I want that thedata which is stored in SQL SERVER get displayed on combobox.I tried following code but couldnt make it.

private void cbomedia_MouseClick(object sender, MouseEventArgs e)
        {         
             SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=PSM");
            con.Open();
           SqlCommand com = new SqlCommand("select * from Target_Audience", con);
            SqlDataReader reader;
                       
               reader = com.ExecuteReader();
              cbomedia.DataSource = reader;
       cbomedia.DisplayMember = "Media_method_ID";
              reader.Close();
                con.Close();
       
        }


我有一个错误


i got an error

complex DataBining accepts as a data source either an IList or an IListSource


请告诉我
问候
shivani


Please tell me
regards
shivani

推荐答案

Dataset ds = new Dataset();
DataAdapter dt = new dataAdapter(Com);
dt.fill(ds);

comboBox2.DataSource = ds;
comboBox2.DisplayMember = "MemberName";
comboBox2.ValueMember = "MemID";







//with DataReader
try
{
// opening the connection
connection.Open();
OleDbDataReader dr = command.ExecuteReader();
if (dr.HasRows)
{
// Iterate through the table
while (dr.Read())
{
//adding item to the combobox. you can also use dr[1] to get the country name
comboBox1.Items.Add(dr["CountryName"]);
}
}
}
catch (Exception ex)
{
MessageBox.Show("The following error occured :rn" + ex.ToString());
}
finally
{
// close the connection and DataReader
connection.Close();
dr.Close();
}
}



这篇关于如何获取存储在组合框中显示的数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:47