i have created a generic function named ExecuteProcedure<T>(T command, string parameters)
, now inside the ExecuteProcedure
function i want to cast the T into SqlCommand
, so that i can use SqlCommand's
properties like Parameters.Add()
here is my code.
T could be SqlCommand or SqlDataAdapter
here's my code:
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();
}
}
You can use this code to CAST T to SqlCommand
SqlCommand cmd = (SqlCommand)Convert.ChangeType(command, typeof(SqlCommand));
anyways thanks @Chris
I believe all you have to do is:
SqlCommand cmd = (SqlCommand)(object)command;