程序类型:Windows Form 应用程序窗体上放了:Label1,TextBox1,TextBox2
还有一个TextBox3
又放了一个按钮在TextBox3中输入一个数值18做为年龄用类似:select 序号,姓名,年龄 from ta 年龄=???? 的串在按钮中写什么代码可以把返回的内容“序号,姓名,年龄”依次显示于Label1,TextBox1,TextBox2中?服务器:Server1
数据库: test
帐户:sa
密码:mypass

解决方案 »

  1.   

    ADO.NET查询数据库,返回一个阅读器SqlDataReader reader关键代码
    reader.Read();
    Label1.Text=reader["序号"].ToString();
    TextBox1.Text=reader["姓名"].ToString();
    TextBox2.Text=reader["年龄"].ToString();
      

  2.   

    写一个方法查找数据库中的内容然后,返回一个dataset,然后接收绑定到你的控件上面
      

  3.   

    using System.Data;
    using System.Data.SqlClient;string conStr = "server=Server1;database=test;uid=sa;pwd=mypass";
                SqlConnection con = new SqlConnection(conStr);
                SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄=19", con);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Label1.Text = reader["序号"].ToString();
                    TextBox1.Text = reader["姓名"].ToString();
                    TextBox2.Text = reader["年龄"].ToString();
                }
                reader.Close();
                con.Close();
      

  4.   

    哎,下面我给也一个 ado.net 操作数据库 MSSQL 的例子,你可以参改一下:
    //通过用户名取得 其详细info
    public void GetUsers(string UserName)
    {
    //window 本地实例
    string connectionString = "Data Source=.\\SqlExpress;DataBase=master;Integrated Security=SSPI;";
    using(SqlConnection connect = new SqlConnection(connectionString))
    {
    connect.Open();
    using(SqlCommand comm = connect.CreateCommand() )
    {
    try
    {
    comm.CommandText="select  [id] ,[UserName],[Password] from [master].[dbo].[T_User] where [UserName]=@UN";
    comm.Parameters.Add(new SqlParameter("UN", UserName));
    using (SqlDataReader reader = comm.ExecuteReader())
    {
    while (reader.Read())
    {
    long id =(long) reader.GetSqlInt64(reader.GetOrdinal("id"));
    string UName = reader.GetString(reader.GetOrdinal("UserName"));
    string PWord = reader.GetString(reader.GetOrdinal("Password"));
    MessageBox.Show(id.ToString()+"-->"+UName+":"+PWord); //你在这里改改到textBox里
    }
    }
    }
    catch (SqlException ex)
    {
    //异常信信显示
    string ErrorInfo="\n数据库执行出错了:\n";
    ErrorInfo+="\n信息"+ex.Errors;
    ErrorInfo+="\n类弄"+ex.Number;
    ErrorInfo+="\n名称"+ex.Source;
    ErrorInfo+="\n行号"+ex.LineNumber;
    ErrorInfo+="\n详细"+ex.Message;
    MessageBox.Show(ErrorInfo);
    }
    finally
    { //关闭数据库连接
    if (connect.State == ConnectionState.Open)
    {
    connect.Close();
    connect.Dispose();
    }
    }
    }
    }
    }
      

  5.   

    谢大手
    不过有一小点
    要是那个19不是具体数,是textBox3内的值呢?我原问也是这么问的
      

  6.   

    谁把7楼回的这行
     SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄=19", con);
    里的19改成引用textBox1里的数字,就圆满结帖了,我是刚学,什么也不会啊
      

  7.   

    SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄="+textBox1.Text+, con);
      

  8.   

    发错了
    SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄="+textBox1.Text, con);
      

  9.   

    用参数化SQL,避免注入
    SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄=@age", con);
    cmd.Parameters.AddWithValue("@age",19);