请问!怎么把查询到的值传递给变量啊??比如说:SqlConnection cmd = new SqlConnection("select name from abc where id = max(id)",con);我要把取出来的name值传给textbox1 要怎么样写??

解决方案 »

  1.   

    本身你写的这个就是错误的,应该是:
    SqlConnection con = new SqlConnection(“连接字符串”);
    con.open();
    SqlCommand cmd = new SqlCommand("select name from abc where id = max(id)",con);
    textbox1.text=cmd.ExecuteScalar();
    con.close();
      

  2.   

    呵呵!一不小心写错了!呵呵!是SqlCommand打错!谢拉
      

  3.   

    System.Data.SqlClient.SqlDataAdapter的方法Fill(DataSet ds)
      

  4.   

    不用控件,直接用代码
    ----------------------------------
    刚才哪个错误是:cmd.ExecuteScalar();无法将类型OBJECT隐式转换为STRING,我用TOSTRING()也不行///
      

  5.   

    SqlConnection con = new SqlConnection(“连接字符串”);
    con.open();
    SqlCommand cmd = new SqlCommand("select name from abc where id = max(id)",con);
    SqlDataReader dr=cmd.ExecuteReader();
    textbox1.text=dr[0].tostring();
    con.close();
      

  6.   

    高手,还是不行啊!
    ----------------------------------------
    未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 系统错误。
      

  7.   

    SqlConnection con = new SqlConnection(“连接字符串”);
    con.open();
    string sql= "select name from abc where id = max(id)";
    datatable ds=new datatable();
    SqlDataAdapter dr=new sqldataAdapter(con,sql);
    dr.fill(ds);
    textbox1.text=ds.rows[0][0].tostring();
    con.close();
      

  8.   

    谢谢了,我搞定了!强制转换到OBJECT类型后,再转换到STRING类型就可以了!谢谢了!