大家好,想請教一個新手問題,資料庫是SQL
試了好久實在是想不到該如何做= =特來請教各位大大...懇請賜教SQL內有一個table
個人資料表如下|姓名|興趣|星座|
|小明|游泳|獅子|
----------------
|小華|爬山|雙魚|
----------------
|阿花|跑步|巨蟹|textbox1↓
----------
輸入姓名(假設輸入小明)     |查詢扭| 
----------
textbox2↓
----------
游泳        <<<<<按下查詢後讀取資料庫資料顯示在texbox上
----------
textbox3↓
----------
獅子        <<<<<按下查詢後讀取資料庫資料顯示在texbox上
----------

解决方案 »

  1.   

     protected void Button1_Click(object sender, EventArgs e)
    {
    string strsql = "select 兴趣,星座 from 表 whhere 姓名='"+TextBox1.Text+"' ";
        
    DataTable dt=new DataTable();//创建一个数据表dt
    SqlConnection Conn = new SqlConnection(strConn);//定义新的数据连接控件并初始化
    Conn.Open();//打开连接
    SqlDataAdapter Cmd = new SqlDataAdapter(strSql, Conn);//定义并初始化数据适配器
    Cmd.Fill(dt); //将数据适配器中的数据填充到数据集dt中
    Conn.Close();//关闭连接
    TextBox2.Text=dt.Rows[0].ItemArray[1].ToString;//Row[0]表示dt表中的第一行(假设没有同名),ItemArray[1]表示行中的第二个数据单元
    TextBox3.Text=dt.Rows[0].ItemArray[1].ToString; //同上
    }如果答案满意记得多给点分哦
      

  2.   

     纠正:
    TextBox2.Text=dt.Rows[0].ItemArray[1].ToString();
    TextBox3.Text=dt.Rows[0].ItemArray[2].ToString();
      

  3.   

    protected void Button1_Click(object sender, EventArgs e)
    {
    string strsql = "select 兴趣,星座 from 表 whhere 姓名='"+TextBox1.Text+"' ";
       
    DataTable dt=new DataTable();//创建一个数据表dt
    SqlConnection Conn = new SqlConnection(strConn);//定义新的数据连接控件并初始化
    Conn.Open();//打开连接
    SqlDataAdapter Cmd = new SqlDataAdapter(strSql, Conn);//定义并初始化数据适配器
    Cmd.Fill(dt); //将数据适配器中的数据填充到数据集dt中
    if(dt.rows.count>0)//判斷資料庫沒有要查詢的資料
    {有查询出来
    TextBox2.Text=dt.Rows[0].ItemArray[1].ToString();//Row[0]表示dt表中的第一行(假设没有同名),ItemArray[1]表示行中的第二个数据单元
    TextBox3.Text=dt.Rows[0].ItemArray[1].ToString(); //同上
    }
    Conn.Close();//关闭连接}
      

  4.   

    if(dt.rows.count>0)//判斷資料庫沒有要查詢的資料
    {//有查询出来
    TextBox2.Text=dt.Rows[0].ItemArray[1].ToString();
    TextBox3.Text=dt.Rows[0].ItemArray[1].ToString();
    }
    else
    {
    TextBox2.Text="查無此資料";
    TextBox3.Text="查無此資料";
    }
      

  5.   

    应该改成
    TextBox2.Text=dt.Rows[0]["兴趣"].ToString();这种