SqlConnection,SqlCommand,SqlReader,SqlDataAdaper的用法总结!!

 

 

     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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值