日期:2014-05-18  浏览次数:20895 次

C#连接数据库问题
我想从SQL数据库的一个表中 读取一列值到 listbox控件 ,应该怎么写呢

------解决方案--------------------
private void button1_Click_1(object sender, EventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["字符串名"].ConnectionString;
SqlConnection myConn = new SqlConnection(strConn);
string strSQL = "select address from shuju ";
myConn.Open();//此处需要先打开数据库连接
SqlCommand myCommand = new SqlCommand(strSQL, myConn);
SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
adapter.Fill(ds,"NewDataSet");
//DataSet ds = new DataSet("NewDataSet");
//SqlDataReader reader = myCommand.ExecuteReader();
//myConn.Open();
listBox1.DataSource = ds.Tables("shuju");
listBox1.DisplayMember = "address";
myConn.Close();
  
}
------解决方案--------------------
额,你自己看那错了你的:listBox1.DataSource = ds.Tables("shuju");

 listBox1.DataSource = ds.Tables["shuju"];