SqlConnection,SqlCommand,SqlReader,SqlDataAdaper的用法總結!!

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.