C#中如何執(zhí)行存儲過程方法
功能 : 根據(jù)調(diào)用的方法名稱 反射動態(tài)調(diào)用 sql Command 的方法
/// <summary>
/// 存儲過程的屬性
/// ProcName 存儲過程的名稱
/// MethodName 執(zhí)行SqlCommand 方法的名稱
/// PrmList 存儲過程的參數(shù)
/// </summary>
public class ExeProc
{
public string ProcName;
public string MethodName;
public object[] PrmValue;
}
根據(jù)制定的存儲過程的名稱
和參數(shù) 來執(zhí)行指定的存儲過程 和 調(diào)用 sqlCommand 的方法
public class DataHelper
{
private string connString = null;
public DataHelper(string conStr)
{
this.connString = conStr;
}
/// <summary>
/// 執(zhí)行存儲過程
/// </summary>
/// <param name="ep">執(zhí)行存儲過程的屬性
/// ProcName 存儲過程的名稱
/// MethodName 執(zhí)行SqlCommand 方法的名稱
/// PrmList 存儲過程的參數(shù)
/// </param>
/// <returns>返回執(zhí)行的結(jié)果</returns>
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();
//用反射根據(jù)輸入的方法名 執(zhí)行對應(yīng)的方法
object retObj = ty.InvokeMember(ep.MethodName, BindingFlags.InvokeMethod, null, cmd, null);
if (retObj.GetType().FullName == "System.Data.SqlClient.SqlDataReader")
{
//將返回的object 轉(zhuǎn)換成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("獲取數(shù)據(jù)發(fā)生錯誤\n" + ex.Message);
}
}
return null;
}
}
相關(guān)文章
C#中使用強(qiáng)制類型實(shí)現(xiàn)字符串和ASCII碼之間的轉(zhuǎn)換
這篇文章主要介紹了C#中使用強(qiáng)制類型實(shí)現(xiàn)字符串和ASCII碼之間的轉(zhuǎn)換,本文還給出了另一種方法,需要的朋友可以參考下2014-08-08
淺談c#.net中巧用ToString()將日期轉(zhuǎn)成想要的格式
有時候我們要對時間進(jìn)行轉(zhuǎn)換,達(dá)到不同的顯示效果,更多的該怎么辦呢?2013-03-03
C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#使用whisper.net實(shí)現(xiàn)語音識別功能
這篇文章主要為大家詳細(xì)介紹了C#如何使用whisper.net實(shí)現(xiàn)語音識別功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以學(xué)習(xí)一下2023-11-11

