一、添加引用
提示:System.Data.OracleClient缺少程序集引用
再把对应路径(例:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.2\System.Data.OracleClient.dll)下的OracleClient.dll文件拷贝到项目的bin文件夹下
二、配置web.config文件
三、在项目根目录下创建Oracle文件夹,并在其中创建OracleHelper.cs文件
using System;using System.Configuration;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using System.Data.SqlClient;using System.Data.OracleClient;namespace MyWebService.Oracle{ public class OracleHelper { public static OracleConnection GetConn() { return new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConnString"].ToString()); } ////// 查询操作 /// /// /// ///public static DataTable QueryCommand(string querySQL, OracleParameter[] oraParams) { DataTable dt = new DataTable(); OracleConnection conn = OracleHelper.GetConn(); OracleCommand cmd = new OracleCommand(querySQL, conn); if (oraParams != null && oraParams.Length > 0) { foreach (OracleParameter oraParam in oraParams) { cmd.Parameters.Add(oraParam); } } OracleDataAdapter adapter = new OracleDataAdapter(cmd); adapter.Fill(dt); conn.Close(); return dt; } }}
四、引用OracleHelper
创建一个测试类Test.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using System.Data.OracleClient;using MyWebService.Oracle;namespace MyWebService.Test{ public class Test { public void TestOracle() { //代码块 string querySQL = "SQL语句"; DataTable dt = OracleHelper.QueryCommand(querySQL, null); } }}