SqlConnection con = new SqlConnection("server=ZMQHBD2007;database=data;uid=sa;pwd=123;");
            //上面这句等价于:
            //private static string constring="server=ZMQHBD2007;database=data;uid=sa;pwd=123;";
            //SqlConnection con = new SqlConnection();
            //con.ConnectionString = constring;
       con.Open();
       SqlDataAdapter sda = new SqlDataAdapter("select * from category", con);
           //上面这句等价于:
           //string cmd= "select * from category";
           // SqlDataAdapter sda = new SqlDataAdapter(cmd,con);
       DataSet ds = new DataSet();
       sda.Fill(ds, "category");
           //下面这5个语句等价
           //this.GridView1.DataSource = ds;
           //this.GridView1.DataSource = ds.Tables[0];
           //this.GridView1.DataSource = ds.Tables["customers"];
           //this.GridView1.DataSource = ds.Tables[0].DefaultView;
        this.GridView1.DataSource = ds.Tables["category"].DefaultView;
        this.GridView1.DataBind();
        con.Close();

利用SqlConnection、SqlDataAdapter和DataSet实现表内容在GridView中显示。

        SqlConnection con = new SqlConnection("server=ZMQHBD2007;database=data;uid=sa;pwd=123;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from category", con);
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;
           //第三句话和第五句话可写为:
           //sda.SelectCommand = new SqlCommand("select * from category", con);
        DataSet ds = new DataSet();
        sda.Fill(ds, "category");
        this.GridView1.DataSource = ds.Tables["category"].DefaultView;
        this.GridView1.DataBind();
        con.Close();

 利用SqlConnection、SqlCommand、SqlDataAdapter和DataSet实现表内容在GridView中显示。

        ////我最经常用这一种,同时连接对象是整个程序的公共对象,所以我一般会把数据库连接封装到一个类中,这样就可以在程序的任何地方随时调用
    SqlConnection con = new SqlConnection("server=ZMQHBD2007;database=data;uid=sa;pwd=123;");            //双引号中的最后一个分号可以去掉
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from category", con);
           //上面这句可写为:
           //SqlCommand cmd = new SqlCommand();
           //cmd.CommandText = "select * from category";
           //cmd.Connection = con;
           //cmd.CommandType = CommandType.Text;  //这条语句是多余的,因为默认就是Text
    
        SqlDataReader sdr = cmd.ExecuteReader();
        this.GridView1.DataSource = sdr;
        this.GridView1.DataBind();
        sdr.Close();
        con.Close();

 利用SqlConnection、SqlCommand、SqlDataReader实现表内容在GridView中显示。

 


  SqlConnection   mycon=new   SqlConnection("server=服务器名;uid=用户名;pwd=密码;database=");  
  mycon.Open();  
   
  //如果SQL语句是Select   ...  
  SqlDataAdapter   myda=new   SqlDataAdapter(SQL语句,mycon);  
  myda.Fill(myset);  
  this.DataGrid1.DataSource=myset.Tables[0].DefaultView;  
   
  //如果SQL语句是Delete、Update、Insert  
  SqlComman   cmd=new   SqlCommand();  
  cmd.CommandText=SQL语句;  
  cmd.CommandType=System.Data.CommandType.Text;  
  cmd.Connection=mycon;  
  cmd.ExcuteNoQuery();  
   
  mycon.Close();

相关文章:

  • 2021-11-08
  • 2021-05-06
  • 2021-06-24
  • 2021-07-23
  • 2022-12-23
  • 2021-08-17
  • 2021-12-30
  • 2021-09-06
猜你喜欢
  • 2021-09-20
  • 2022-01-28
  • 2022-12-23
  • 2021-06-22
  • 2021-04-08
  • 2021-07-24
  • 2021-07-17
相关资源
相似解决方案