我在程序中定义一个string   我定义的string 怎样来得到数据库中的数据哦...
 
如:string  str;
 table A
   id  name 
    1   a
    2   b
sql:  select name  from A  where id = 1  我怎样来得到str=a???

解决方案 »

  1.   

    string strSql ="select name from A where id = 1 ";
    SqlDataAdapter myCommand = new SqlDataAdapter(strSql, myConnection);
    DataSet ds = new DataSet();
    myCommand.Fill(ds, "t_dict");
    DataView dv = new DataView();
    str=ds.Tables["A"].Columns[0].Caption;
      

  2.   

    其中myConnection为你的数据库联接,myCommand.Fill(ds, "t_dict");----〉myCommand.Fill(ds, "A");
      

  3.   

    - -!为什么那么复杂string strSql = "select name from A where id = 1 ";
                SqlCommand comm = new SqlCommand(strSql, myConnection);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    str = dr.GetString(0);
                }
      

  4.   

    1楼的好像不对吧,那capation不是干这个用的啊.二楼的可以
      

  5.   

    顶MicroSoftor(http://blog.sina.com.cn/zhouzimimi)
      

  6.   

    string strSql = "select name from A where id = 1 ";
                SqlCommand comm = new SqlCommand(strSql, myConnection);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    str = dr.GetString(0);
                }
    是個好方法,用SqlDataReader比較快,
    可是這段代碼存在著隱憂,
    問題出在dr.GetString(0);這句,當數據庫中的name字段空時
    vs會報DBNull錯的,
    應該用dr[0].ToString();就不會有錯了