SqlConnection conn = new SqlConnection("server=localhost;Initial catalog =LIS;uid=sa;password=;");
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT XM FROM BRXX WHERE ID=" + n, conn);
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
            
            DataSet ds = new DataSet();
            
            string xm = Convert.ToString(ds.Tables["BRXX"].Rows[6]["XM"]);我想获得数据库中第6行的XM值,这样写错在那,老是包错“未将对象引用设置到对象事例”,有没有别的方法能获得一行中的一个值,或者我这个怎么改?

解决方案 »

  1.   

    SqlConnection conn = new SqlConnection("server=localhost;Initial catalog =LIS;uid=sa;password=;");            
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT XM FROM BRXX WHERE ID=" + n, conn);
        DataSet ds = new DataSet();
        adapter.Fill(ds,"BRXX");
        string xm = ds.Tables["BRXX"].Rows[5]["XM"].ToString();
        
      

  2.   

    这样写,ds里面应该没有数据吧,
    加上adapter.Fill(ds,"BRXX");试试
      

  3.   

    你没有把数据填充到DataSet里,当然会出现错误,Fill的时候,就会表结构和数据一起填充到了DS里面了!那样就不会错了!
      

  4.   

    未填充数据集:
    try..DataSet ds = new DataSet();
    adapter.Fill(ds,"BRXX");