Being more generic
-
Hi guys I wanna know how I could make the following function more generic and stop writing tedious code... My current example function:
public SqlCommand SetupCommand() { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM Test"; cmd.CommandTimeout = 180; return cmd; }
The above snippet of code will return a SqlCommand object. However there is cases where I would like this very same method but this time around returning a OleDbCommand object. Is there a way to make this method more generic to be able to return any Command types e.g SqlCommand, OleDbCommand without having to re-write code all the time. I guess I'm searching for something similar to IDataReader that can return either SqlDataReader or OleDbDataReader. Thanks in advance. R. -
Hi guys I wanna know how I could make the following function more generic and stop writing tedious code... My current example function:
public SqlCommand SetupCommand() { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM Test"; cmd.CommandTimeout = 180; return cmd; }
The above snippet of code will return a SqlCommand object. However there is cases where I would like this very same method but this time around returning a OleDbCommand object. Is there a way to make this method more generic to be able to return any Command types e.g SqlCommand, OleDbCommand without having to re-write code all the time. I guess I'm searching for something similar to IDataReader that can return either SqlDataReader or OleDbDataReader. Thanks in advance. R.Wouldn't the easiest way be:
public DbCommand SetupCommand(bool isSql) { if(isSql) { //Do SQL setup } else { //Do OleDb setup } return cmd; }
There is no knowledge that is not power. - Mortal Kombat
-
Hi guys I wanna know how I could make the following function more generic and stop writing tedious code... My current example function:
public SqlCommand SetupCommand() { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM Test"; cmd.CommandTimeout = 180; return cmd; }
The above snippet of code will return a SqlCommand object. However there is cases where I would like this very same method but this time around returning a OleDbCommand object. Is there a way to make this method more generic to be able to return any Command types e.g SqlCommand, OleDbCommand without having to re-write code all the time. I guess I'm searching for something similar to IDataReader that can return either SqlDataReader or OleDbDataReader. Thanks in advance. R.Take a look at how I do that. http://www.codeproject.com/cs/database/DatabaseAccessor.asp[^]
-
Hi guys I wanna know how I could make the following function more generic and stop writing tedious code... My current example function:
public SqlCommand SetupCommand() { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM Test"; cmd.CommandTimeout = 180; return cmd; }
The above snippet of code will return a SqlCommand object. However there is cases where I would like this very same method but this time around returning a OleDbCommand object. Is there a way to make this method more generic to be able to return any Command types e.g SqlCommand, OleDbCommand without having to re-write code all the time. I guess I'm searching for something similar to IDataReader that can return either SqlDataReader or OleDbDataReader. Thanks in advance. R. -
Hi guys I wanna know how I could make the following function more generic and stop writing tedious code... My current example function:
public SqlCommand SetupCommand() { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM Test"; cmd.CommandTimeout = 180; return cmd; }
The above snippet of code will return a SqlCommand object. However there is cases where I would like this very same method but this time around returning a OleDbCommand object. Is there a way to make this method more generic to be able to return any Command types e.g SqlCommand, OleDbCommand without having to re-write code all the time. I guess I'm searching for something similar to IDataReader that can return either SqlDataReader or OleDbDataReader. Thanks in advance. R.If you are using .NET 2, take a look at the DB Provider classes that were introduced. These classes, such as DbCommand[^] are intended to provide you with a relatively database agnostic way of working with your data. You can get more detailed information here[^].
Deja View - the feeling that you've seen this post before.
-
Hi guys I wanna know how I could make the following function more generic and stop writing tedious code... My current example function:
public SqlCommand SetupCommand() { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT * FROM Test"; cmd.CommandTimeout = 180; return cmd; }
The above snippet of code will return a SqlCommand object. However there is cases where I would like this very same method but this time around returning a OleDbCommand object. Is there a way to make this method more generic to be able to return any Command types e.g SqlCommand, OleDbCommand without having to re-write code all the time. I guess I'm searching for something similar to IDataReader that can return either SqlDataReader or OleDbDataReader. Thanks in advance. R.Hi Try using the following code that use generics in order to construct the desired type sqlcommand
class Program
{
public static T SetupCommand<T>() where T : IDbCommand, new()
{
T cmd = new T();
cmd.CommandText = "SELECT * FROM Test";
cmd.CommandTimeout = 180;
return cmd;
}static void Main(string\[\] args) { SqlCommand sqlClientCmd = SetupCommand<SqlCommand>(); OleDbCommand oleDBCmd = SetupCommand<OleDbCommand>(); } }
-- modified at 6:21 Friday 5th October, 2007