【发布时间】:2012-04-03 23:23:19
【问题描述】:
我创建了一个名为ExecuteProcedure<T>(T command, string parameters) 的通用函数,现在在ExecuteProcedure 函数中我想转换T into SqlCommand,这样我就可以使用SqlCommand's 属性,如Parameters.Add() 这是我的代码。
T could be SqlCommand or SqlDataAdapter
这是我的代码:
public void ExecuteProcedure<T>(T command, string parameters)
{
using (connection)
{
if (typeof(T) == typeof(SqlCommand))
{
//how to convert into SqlCommand Here to use command.CommandType Property below.
}
command.CommandType = CommandType.StoredProcedure;
foreach (string param in parameters.Split(','))
{
SqlParameter par = new SqlParameter(param, param.Substring(1, param.Length - 1));
command.Parameters.Add(par);
}
connection.Open();
command.ExecuteNonQuery();
}
}
【问题讨论】: