.NET連接Oracle的方法
方式1:直接利用.NET的oracle驅動連接
引用System.data.oracleclient;
using System.data.oracleclient;
string oradb = "Data Source=water;User Id=modis;Password=modis;Integrated Security=no";
OracleConnection conn = new OracleConnection(oradb);
conn.Open();
方式2:安裝完ODP.net(Oracle Data Provider.NET),通過OLEDB連接。
引用System.Data.OleDb;
using System.Data.OleDb;
string oradb = "Provider=OraOLEDB.Oracle;Data Source=water;User Id=modis;Password=modis;";
OleDbConnection myConnection = new OleDbConnection(oradb);
myConnection.Open();
方式3:安裝完ODP.net,通過Oracle的連接器
引用動態鏈接庫Oracle.DataAccess.Client.
using Oracle.DataAccess.Client;
string oradb = "Data Source=water;User Id=modis;Password=modis;";
OracleConnection conn = new OracleConnection(oradb); // C#
conn.Open();
C#連接Oracle數據庫(查詢數據)
using System;
using System.Collections.Generic;
using System.ComponentModel;
//這行和下一行都要先在引用中填加system.data.oracleclient
using System.Data.OracleClient;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
#region 從region到endregion是手工寫的。別的都是系統自動生成的
//定義連接數據庫的字符串
string constring = "data source=wzd;user=wzd;password=wzd;";
//進行連接
OracleConnection conn = new OracleConnection(constring);
try
{
conn.Open();//打開指定的連接
OracleCommand com = conn.CreateCommand();
//寫好想執行的Sql語句
com.CommandText =
"select name from mytable where card_no='0000000002'";
OracleDataReader odr = com.ExecuteReader();
//讀取數據,如果返回為false的話,就說明到記錄集的尾部了
while (odr.Read())
{
//將讀取到的值顯示到定義的控件中。
this.lbl.Text = odr.GetOracleString(0).ToString();
}
odr.Close();//關閉reader.這是一定要寫的
}
catch
{
Messa......余下全文>>
C#連接Oracle的方法
方法1:
System.Data.OracleClient
oracleConnectionString : data source = orcl;user id= scott; password=tiger;persist security info=false;
方法2:
oracleConnectionString :
Provider=OraOLEDB.Oracle.1;User ID=scott;Password=tiger;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.125.250)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))
方法3:
oracleConnectionString:
User ID=scott;Password=tiger;Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST=10.10.125.250)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))
參考資料:百度