C#使用Oracle.ManagedDataAccess.dll
时间:2021-07-01 10:21:17
帮助过:16人阅读
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System.IO;
using System.Collections;
using System.Diagnostics;
using Oracle.ManagedDataAccess.Types;
namespace OracleDemo
{
public class OracleHelper
{
private static string connStr =
"User Id=admin;Password=123;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=test)))";
#region 执行SQL语句,返回受影响行数
public static int ExecuteNonQuery(
string sql,
params OracleParameter[] parameters)
{
using (OracleConnection conn =
new OracleConnection(connStr))
{
conn.Open();
using (OracleCommand cmd =
conn.CreateCommand())
{
cmd.CommandText =
sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
}
#endregion
#region 执行SQL语句,返回DataTable;只用来执行查询结果比较少的情况
public static DataTable ExecuteDataTable(
string sql,
params OracleParameter[] parameters)
{
using (OracleConnection conn =
new OracleConnection(connStr))
{
conn.Open();
using (OracleCommand cmd =
conn.CreateCommand())
{
cmd.CommandText =
sql;
cmd.Parameters.AddRange(parameters);
OracleDataAdapter adapter =
new OracleDataAdapter(cmd);
DataTable datatable =
new DataTable();
adapter.Fill(datatable);
return datatable;
}
}
}
#endregion
}
}
以下代码是调用部分
string sqlString = "Select a.col1,a.col2 From test a Where a.id=:id";
DataTable dt = OracleHelper.ExecuteDataTable(sqlString,new OracleParameter(":id",1));
程序编写完成,生成完成后,目录大概如下:
这时候拷贝Debug文件夹到目标客户电脑上即可直接运行了(前提是目标客户电脑上有.NET Framework),完全摆脱安装Oracle客户端。
C#使用Oracle.ManagedDataAccess.dll
标签:使用 变化 上下 host manage 好用 资料 系统 types