private void button1_Click(object sender, EventArgs e)
        {
            string str = "server=(local);database=book;User ID=sa;password=987654321";
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            try
            {
                SqlDataAdapter ada = new SqlDataAdapter();
                ada.SelectCommand = new SqlCommand("select * from Table2", conn);
                DataSet set = new DataSet();
                ada.Fill(set,"he");
               
                textBox1.Text = set.Tables[0].Rows[0].ToString();
                textBox2.Text = set.Tables[0].Rows[1].ToString();
            }
            catch
            { MessageBox.Show("读取失败!"); }
            finally
            { conn.Close(); }
编译没有错误,但运行显示  读取失败!textBox1.Text里只显示System.Data.DataRow【和表里数据不一样】 ,textBox2.Text里 什么都没显示。这是怎么回事?

解决方案 »

  1.   

    textBox1.Text = set.Tables[0].Rows[0]["列名"].ToString();
    textBox2.Text = set.Tables[0].Rows[1]["列名"].ToString();
      

  2.   

     谢谢! 表里有数据
    用这种方法才能把数据输出来,
    但是
    textBox1.Text = set.Tables[0].Rows[0]["name"].ToString();
    textBox2.Text = set.Tables[0].Rows[0]["pwd"].ToString(); 输出第一组数据;textBox1.Text = set.Tables[0].Rows[1]["name"].ToString();
    textBox2.Text = set.Tables[0].Rows[1]["pwd"].ToString(); 输出第二组数据;这是怎么回事?
      

  3.   

    textBox1.Text里只显示System.Data.DataRow是因为你没设置读取的列,set.Tables[0].Rows[0]返回的是一个DataRow的对象,ToString()之后返回类的名字。
    textBox2.Text里显示为空,则说明读取的数据只有一行。