Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Being more generic

Being more generic

Scheduled Pinned Locked Moved C#
algorithmstutorial
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ryno Burger
    wrote on last edited by
    #1

    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.

    M P P N 5 Replies Last reply
    0
    • R Ryno Burger

      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.

      M Offline
      M Offline
      MartyExodus
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • R Ryno Burger

        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.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Take a look at how I do that. http://www.codeproject.com/cs/database/DatabaseAccessor.asp[^]

        1 Reply Last reply
        0
        • R Ryno Burger

          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.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          And also http://www.codeproject.com/script/comments/forums.asp?msg=2202925&forumid=1649&XtraIDs=1649&searchkw=using+and+database&sd=1+apr+2007&ed=4+Oct+2007&author=PIEBALDconsult&stype=3#xx2202925xx[^]

          1 Reply Last reply
          0
          • R Ryno Burger

            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.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • R Ryno Burger

              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.

              N Offline
              N Offline
              Nissim Salomon
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups