C# 連結資料庫

來源:互聯網
上載者:User

標籤:tar   .net   轉型   ado   query   int   database   initial   new   

一. ADO.NET (Access Data Object, Data Access Objects)

 .NET操作資料庫的一組類

  1. DataSet 資料集
  2. .NET資料提供者(Data Provider)核心對象:

    Connection

     Command

     DataReader

     DataAdapter

 

二. Connection 連線物件

1.建立:

SqlConnection conn = new SqlConnection();

Conn.ConnectionString=’連結字串’

//或:

SQLConnection conn =new SQLConnection(連結字串);

2.開啟串連:

Conn.Open(); 開啟資料庫;

3.連結字串:

(1)地址,資料庫,帳號,密碼

(2)Data Source = 伺服器位址;Initial Catalog=資料庫名;User Id=帳號 ;Password=密碼。或 “server= 伺服器位址;database=資料庫名;uid=帳號;pwd=密碼。”(簡寫)

三.Command 命令執行對象

1.文法

SqlCommand comm=new SqlCommand();

Comm.CommandText=”SQL語句”    //要執行什麼SQL;

Comm.Command=conn                   //在哪執行

//簡寫:

SqlCommand comm=new SqlCommand(sql,conn);

2.執行

2.1  ExecuteScalar()

2.2  ExecuteReader()

2.3  ExecuteNomQuery()

3.comm.ExecuteScalar()

  3.1 Command 開始執行目標的SQL語句,並擷取資料庫返回的結果

  3.2 只擷取結果中的首行首列

  3.3 試用情況:

    3.3.1 彙總函式 AVG(),MAX()……

    3.3.2 根據條件查詢某一列的值

           Convert (萬能轉型  例:Convert.String  Convert.Int32 .....)

 

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.