C# 连接 Oracle 数据库(三种方式:OracleClient、ODBC、OLEDB)

1、OracleClient
//基于.NET 2.0,只有2.0中包含OracleClient
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Security.Cryptography;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OracleClient;
public partial class orclService : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("Access-Control-Allow-Origin", "*");
List result = new List();
string callback = Request.QueryString["method"];
if (callback == "getTableInfo")
result = getTableInfo();
Response.Write(result);
Response.End();
}

//
private static string strConn = ConfigurationSettings.AppSettings["orclCon"];

[WebMethod(Description = "

Oracle连接测试

")]
public List getTableInfo()
{
List list = new List();

OracleConnection conn = new OracleConnection(strConn);
conn.Open();
string strComm = "select * from ATEST_POLYGON";
OracleCommand comm = new OracleCommand(strComm, conn);
OracleDataReader sdr = comm.ExecuteReader();
info strInfo = null;

while (sdr.Read())
{
strInfo = new info();
strInfo.ID = sdr["ID"].ToString();
strInfo.Content = sdr["CONTENT"].ToString();
list.Add(strInfo);
}
if (!sdr.HasRows)
{
strInfo.Content = "无匹配记录";
list.Add(strInfo);
}

sdr.Close();
return list;
}

public class info
{
private string id;
public string ID
{
get { return this.id; }
set { this.id = value; }
}
private string content;
public string Content
{
get { return this.content; }
set { this.content = value; }
}
}

}
2、ODBC(参见“Oracle 通过 ODBC 连接”)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Odbc;
public partial class orclService : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("Access-Control-Allow-Origin", "*");
int result = 0;
string callback = Request.QueryString["method"];
if (callback == "getTableInfo")
result = getTableInfo();
Response.Write(result);
Response.End();
}

//
//
private static string strConn = ConfigurationSettings.AppSettings["orclCon_linux"];

[WebMethod(Description = "

Oracle连接测试

")]
public int getTableInfo()
{
OdbcConnection odbcconn = new OdbcConnection(strConn);
odbcconn.Open();
string strComm = "select * from ATEST_POLYGON";
OdbcDataAdapter odbcda = new OdbcDataAdapter(strComm, odbcconn);
DataSet ds = new DataSet();
odbcda.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
odbcconn.Close();
odbcconn.Dispose();
return dt.Rows.Count;
}
}
3、OLEDB
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
public partial class orclService : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("Access-Control-Allow-Origin", "*");
List result = new List();
string callback = Request.QueryString["method"];
if (callback == "getTableInfo")
result = getTableInfo();
Response.Write(result);
Response.End();
}

//
//
private static string strConn = ConfigurationSettings.AppSettings["orclCon_windows"];

[WebMethod(Description = "

Oracle连接测试

")]
public List getTableInfo()
{
List list = new List();

OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strComm = "select * from ATEST_POLYGON";
OleDbCommand comm = new OleDbCommand(strComm, conn);
OleDbDataReader sdr = comm.ExecuteReader();
info strInfo = null;

while (sdr.Read())
{
strInfo = new info();
strInfo.ID = sdr["ID"].ToString();
strInfo.Content = sdr["CONTENT"].ToString();
list.Add(strInfo);
}

if (!sdr.HasRows)
{
strInfo.Content = "无匹配记录";
list.Add(strInfo);
}

sdr.Close();
return list;
conn.Close();
conn.Dispose();
return list;
}

public class info
{
private string id;
public string ID
{
get { return this.id; }
set { this.id = value; }
}
private string content;
public string Content
{
get { return this.content; }
set { this.content = value; }
}
}
}

你可能感兴趣的:(Oracle)