oldb连接oracle,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", "*");

Listresult = 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 ListgetTableInfo()

{

Listlist = 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", "*");

Listresult = 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 ListgetTableInfo()

{

Listlist = 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; }

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值