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

發佈了22 篇原創文章 · 獲贊 7 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章