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里 什么都没显示。这是怎么回事?
{
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里 什么都没显示。这是怎么回事?
textBox2.Text = set.Tables[0].Rows[1]["列名"].ToString();
用这种方法才能把数据输出来,
但是
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(); 输出第二组数据;这是怎么回事?
textBox2.Text里显示为空,则说明读取的数据只有一行。