C#中對資料庫的操作

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   資料   div   art   

C#中對資料庫的操作:

1.首先要建立資料庫通道SqlConnection並open,

2.建立對資料庫操作的對象sqlcommand,

3.指定對哪個資料庫操作sqlcommand對象.connection=資料庫物件,

4.指定待操作的sql語句sqlcommand對象.commandtext,

5.建立執行對象SqlDataAdapter,建立DataAdapter資料配接器執行個體

6.sqldataadapter對象中fill方法進行查詢,插入等操作。

7.最後關閉資料庫,.Dispose()方法。

//建立資料庫連結字串string s="Data Source=WJ-20130626PJKO;Initial Catalog=StudentManagement;User ID=jiaxuekai;password=jiaxuekai";//建立資料庫物件Sqlconnection sql=new Sqlconnection(s);//開啟資料庫sql.Open();//建立資料庫執行對象Sqlcommand cmd=new Sqlcommand();//指定對哪個資料庫操作cmd.Connction=sql;//要操作的命令cmd.CommandText="select * from T_Department";//命令對象SqlDataAdapter dat=new SqlDataAdapter(cmd);//建立接收資料的表DataTable dt=new DataTable();//執行命令dat.fill(dt);//關閉所有佔用記憶體的資源 sql.Dispose(); cmd.Dispose();

此處需要說明,對資料庫的開啟.Open()方法可以不寫,因為在fill方法中帶有Open方法,不寫則fill中的open方法執行,如果寫了open方法,則fill中的open方法不再執行。 

C#中對資料庫的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.