C#--sql实例

 

sql用法总结:

1.查询语句adpter里写sql,执行后把结果赋值给新建的dataset 对象ds,即可在gridview显示。  

SqlConnection con = new SqlConnection("server=ZMQHBD2007;database=data;uid=sa;pwd=123;");
                  //
上面这句等价于:
                 //private static string 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();

注:利用SqlConnectionSqlDataAdapterDataSet实现表内容在GridView中显示。

  

2.sqlcommand执行sql,然后赋值给adaptercommand方法, adapter再填充数据给新建dataset对象的ds,然后显示

       SqlConnection con = new SqlConnection(…);
        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();

利用SqlConnectionSqlCommandSqlDataAdapterDataSet实现表内容在GridView中显示。

        ////我最经常用这一种,同时连接对象是整个程序的公共对象,所以我一般会把数据库连接封装到一个类中,这样就可以在程序的任何地方随时调用

 

 

3.sqlcommand执行sql,然后执行reader属性,赋值给datareader对象,reader对象作为ds.


    SqlConnection con = new SqlConnection("…..");            //
双引号中的最后一个分号可以去掉
        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();

利用SqlConnectionSqlCommandSqlDataReader实现表内容在GridView中显示。

 

注:加参数

SqlCommand sqlComm = new SqlCommand(sql, Sqlcon);

        sqlComm.Parameters.Add("@userid", SqlDbType.Int);//添加参数

        sqlComm.Parameters["@userid"].Value = Convert.ToInt32( tbUserId.Text) ;//为参数赋值

       

 

 

 

4.其他更新语句   
//
如果SQL语句是DeleteUpdateInsert  
SqlComman   cmd=new   SqlCommand();  
cmd.CommandText=SQL
语句;  
cmd.CommandType=System.Data.CommandType.Text;  
cmd.Connection=mycon;  
cmd.ExcuteNoQuery();  
   
mycon.Close();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2012-03-09 20:06 青蛙學堂 阅读(532) 评论(0)  编辑 收藏 引用 所属分类: 軟件布袋數據庫

只有注册用户登录后才能发表评论。
<2006年8月>
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜