Oracle - C#串連資料庫

來源:互聯網
上載者:User

標籤:ada   framework   use   read   區分   select   network   table   資料庫訪問   

一.System.Data.OracleClient 微軟提供的串連庫,不過已經過時,不建議使用

二.Oracle.DataAccess.Client (ODP.Net) Oracle提供的資料庫訪問類庫.

三.Oracle.ManagedDataAccess.dll (ODP.NET Managed) 也是Oracle提供的。

推薦使用方式情節3,但是使用ManagedDataAccess的話,必須添加對程式集“netstandard”的引用,而這個程式集又是FrameWork 4.6及以上才有的,

所以筆者這用的.net架構FrameWork 4.5的項目並不能使用, 採用方案二來處理,方案二是需要區分x86/x64版本的.

1.去官網下載需要的dll檔案。

http://www.oracle.com/technetwork/topics/dotnet/utilsoft-086879.html 

下載對應的vs版本的dll檔案,如果vs版本太低,就下載最老的dll檔案,筆者下載的最老的: ODAC 11.2 Release 5

 

將這些dll檔案放到項目啟動並執行bin目錄下,和運行檔案同一目錄。

資料庫連接語句:

  <connectionStrings>    <add name="OracleConnString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.161)(PORT=1521))(CONNECT_DATA=(SID=MyOracle)));Persist Security Info=True;User ID=system;Password=Sky312;"/>  </connectionStrings>

C#調用

private readonly string connstr = ConfigurationManager.ConnectionStrings["OracleConnString"].ConnectionString;string sql = "SELECT * FROM \"UserInfo\"   ";OracleConnection connection = new OracleConnection(connstr);connection.Open();OracleCommand command = new OracleCommand(sql, connection);DataTable table = null;try{    OracleDataAdapter adapter = new OracleDataAdapter();    adapter.SelectCommand = command;    table = new DataTable();    adapter.Fill(table);}catch{    throw;}finally{    command.Dispose();    connection.Close();    connection.Dispose();}
dataGridView1.DataSource = table;

查詢

 

Oracle - 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.