连接sql

protected string connstr;
  protected SqlConnection conn;
  protected SqlCommand comm;
  protected SqlDataAdapter da;
protected DataSet ds;

  private void InitDataSource()
  {
   connstr = "Server=.;DataBase=pubs;uid=sa;pwd=sql";
   conn = new SqlConnection(connstr);
   comm = new SqlCommand();
   comm.Connection = conn;
   comm.CommandText = "select * from authors where au_id=@au_id";
   comm.Parameters.Add("@au_id",SqlDbType.VarChar,11,"au_id");     
   comm.Parameters[0].Value = this.Request.QueryString["au_id"];
   comm.CommandType = CommandType.Text;

   da = new SqlDataAdapter();
   da.SelectCommand = comm;

   ds = new DataSet();

   da.Fill(ds);
  }

void BindDataSource()
  {
   DataRow dr = ds.Tables[0].Rows[0];
   this.TextBox1.Text =dr[0] .ToString();
   this.TextBox2.Text =dr[1].ToString();
   this.TextBox3.Text = dr[2].ToString();
   this.TextBox4.Text =dr[3].ToString();
   this.TextBox5.Text = dr[5].ToString();

  }


private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }

原文地址:https://www.cnblogs.com/puke/p/756599.html