c#执行oracle存储过程,C#中如何执行存储过程方法

功能 :  根据调用的方法名称  反射动态调用  sql Command 的方法

代码如下:

///

/// 存储过程的属性

/// ProcName 存储过程的名称

/// MethodName 执行SqlCommand 方法的名称

/// PrmList 存储过程的参数

///

public class ExeProc

{

public string ProcName;

public string MethodName;

public object[] PrmValue;

}

根据制定的存储过程的名称

和参数  来执行指定的存储过程 和 调用 sqlCommand 的方法

代码如下:

public class DataHelper

{

private string connString = null;

public DataHelper(string conStr)

{

this.connString = conStr;

}

///

///  执行存储过程

///

/// 执行存储过程的属性

/// ProcName 存储过程的名称

/// MethodName 执行SqlCommand 方法的名称

/// PrmList 存储过程的参数

///

/// 返回执行的结果

public object ExecProcRetObj(ExeProc ep)

{

if (this.connString != null && this.connString != string.Empty)

{

try

{

SqlConnection con = new SqlConnection(this.connString);

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText = "Exec " + ep.ProcName + " ";

foreach (object obj in ep.PrmValue)

{

cmd.CommandText += obj + ",";

}

cmd.CommandText = cmd.CommandText.Remove(cmd.CommandText.Length - 1, 1);

Type ty = cmd.GetType();

con.Open();

//用反射根据输入的方法名 执行对应的方法

object retObj = ty.InvokeMember(ep.MethodName, BindingFlags.InvokeMethod, null, cmd, null);

if (retObj.GetType().FullName == "System.Data.SqlClient.SqlDataReader")

{

//将返回的object 转换成DataTable

DataTable retDt = new DataTable();

retDt.Load(retObj as SqlDataReader);

con.Close();

con.Dispose();

return retDt;

}

return retObj;

}

catch (Exception ex)

{

System.Windows.Forms.MessageBox.Show("获取数据发生错误n" + ex.Message);

}

}

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值