ado.net 操作oracle 数据库 跟操作mssql 的原来基本一样。只是使用不同的命名空间而已。下面举几个例子:
一。 C#读取oracle数据库的表格
///ado.net 读取table 列子
///需要添加命名空间 : using System.Data.OracleClient //链接字符串
string connString = @"User ID=system;Password=123456;Data Source=(DESCRIPTION = (ADDRESS_LIST=
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.118)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = ORCL)))";
//创建链接
OracleConnection conn = new OracleConnection(connString);
conn.Open();
string sql = "select * from help";
OracleDataAdapter sda_tab = new OracleDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda_tab.Fill(ds); DataTable tb = ds.Tables[];
二 。 C# 执行 oracle 语句 返回 受影响的行数
string connString = @"User ID=system;Password=123456;Data Source=(DESCRIPTION = (ADDRESS_LIST=
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.118)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = ORCL)))";
//创建链接
OracleConnection conn = new OracleConnection(connString);
conn.Open();
string sql = "select * from help";
OracleCommand cmd = new OracleCommand(sql, conn);
int ret = cmd.ExecuteNonQuery();
三。C#执行oracle游标的存储过程
//智友的业务端
public static string oconn = "User ID=" + sys.Sa + ";Password=" + sys.Pwd + ";Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = " + sys.Ip + ")(PORT = " + sys.Dk + "))) (CONNECT_DATA = (SERVICE_NAME = " + sys.Data + ")))"; /// <summary>
/// 执行存储过程返回搜影响的行数
/// </summary>
/// <param name="sql"></param>
/// <param name="opar"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string cmdtxt, string fw, OracleParameter[] sqlParme)
{ using (OracleConnection cona = new OracleConnection (oconn))
{
cona.Open();
using ( OracleCommand cmd = new OracleCommand("Proc_PXUnit_Pay", cona))
{
cmd.CommandType = CommandType.StoredProcedure;//说明执行的存储过程
cmd.CommandText = cmdtxt;//指定执行的名称 //cmd.Parameters.Clear();//先清空
cmd.Parameters.AddRange(sqlParme);//添加参数
//添加参数
return cmd.ExecuteNonQuery();
}
} //OracleConnection conA = null;
//OracleCommand cmd = null;
//try
//{ // conA = new OracleConnection(oconn);
// conA.Open();
// cmd = new OracleCommand("Proc_PXUnit_Pay", conA);
// //cmd = conA.CreateCommand();
// cmd.CommandType = CommandType.StoredProcedure;//说明执行的存储过程
// cmd.CommandText = cmdtxt;//指定执行的名称 // //cmd.Parameters.Clear();//先清空
// cmd.Parameters.AddRange(sqlParme);//添加参数
// //添加参数
// return cmd.ExecuteNonQuery(); //}
//catch (Exception ex)
//{
// throw;
//}
//finally
//{
// conA.Close(); //}
}
四 执行游标的存储过程 返回结果集
/// <summary>
/// 调用通过游标调用oracle的存储过程
/// </summary>
/// <param name="cmdtxt">存储过程的名字</param>
/// <param name="fw"></param>
/// <param name="sqlParme">存储过程的参数</param>
/// <returns></returns>
public static DataSet GetDsByPrec(string cmdtxt, string fw, OracleParameter[] sqlParme)
{
OracleConnection conA = null;
OracleCommand cmd = null; try
{ conA = new OracleConnection(oconn);
conA.Open();
cmd = new OracleCommand("Proc_PXUnit_Pay", conA); cmd.CommandText = cmdtxt;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();//先清空
//添加参数
if (sqlParme != null)
{
foreach (OracleParameter parme in sqlParme)
{
cmd.Parameters.Add(parme);
}
}
DataSet ds1 = new DataSet();
OracleDataAdapter da1 = new OracleDataAdapter(cmd);//取出数据
da1.Fill(ds1);
return ds1;
}
catch (Exception ex)
{
throw;
}
finally
{
conA.Close(); } }