Hellow.

I have 3 variables
1. string query;
2. public static OracleCommand oracmd;
3. public static OracleDataReader reader;

I have used these variables in a function

.
.
.
public void PreviousValue()
{
  query = " select * from....... ";
  oracmd = new OracleCommand();
  ....
  ....
  reader = oracmd.ExecuteReader();   
}
.
.

But I want to put these three variables in C# Generics and then access those in PreviousValue().
Can any one help me how to do that?

Recommended Answers

All 4 Replies

That doesn't seem like it would be very efficient.
What are you going to do with them after that.

That doesn't seem like it would be very efficient.
What are you going to do with them after that.

I just want to learn generics by applying it here. Generic is a Custom Data type. SO I thought to learn it by applying it here. Though I already managed these three instances by applying public static property.

Using generics will not help you with that function.
As a matter of fact, if you study how the IDataReader is created, you have some syntax that has to be re-written to be of real use.

I played around with the concept and came up with SOMETHING that is purely syntax and not functional in a manner anyone should use.

PROOF OF CONCEPT ONLY:

using System.Data;
using System.Data.OracleClient;

namespace DW_407917_CS_CON
{
   class CGTest<T, U, V>
      where U : IDbCommand
      where V : IDataReader
   {
      private string _strSQL { get; set; }
      private OracleCommand _cmd { get; set; }
      private OracleDataReader _rdr { get; set; }

      public CGTest(T strSql, U cmd, V rdr)
      {
         _strSQL = strSql.ToString();
         _cmd = cmd as OracleCommand;
         _rdr = rdr as OracleDataReader;
      }
 
      public void PreviousValue()
      {
         OracleConnectionStringBuilder csb = new OracleConnectionStringBuilder();
         // fix parameters of builder here

         using (OracleConnection conn = new OracleConnection(csb.ToString()))
         {
            conn.Open();

            _cmd = new OracleCommand(_strSQL, conn);

            using (_rdr = _cmd.ExecuteReader())
            {
               while (_rdr.Read())
               {
                  //copy from rdr to variables
               }
               //
               _rdr.Close();
            }
            //
            conn.Close();
         }
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         OracleCommand cmd = new OracleCommand();
         OracleDataReader rdr = null;

         CGTest<string, OracleCommand, OracleDataReader> test =
            new CGTest<string, OracleCommand, OracleDataReader>
               ("select * from db", cmd, rdr);

         test.PreviousValue();
      }
   }
}

Using generics will not help you with that function.
As a matter of fact, if you study how the IDataReader is created, you have some syntax that has to be re-written to be of real use.

I played around with the concept and came up with SOMETHING that is purely syntax and not functional in a manner anyone should use.

PROOF OF CONCEPT ONLY:

using System.Data;
using System.Data.OracleClient;

namespace DW_407917_CS_CON
{
   class CGTest<T, U, V>
      where U : IDbCommand
      where V : IDataReader
   {
      private string _strSQL { get; set; }
      private OracleCommand _cmd { get; set; }
      private OracleDataReader _rdr { get; set; }

      public CGTest(T strSql, U cmd, V rdr)
      {
         _strSQL = strSql.ToString();
         _cmd = cmd as OracleCommand;
         _rdr = rdr as OracleDataReader;
      }
 
      public void PreviousValue()
      {
         OracleConnectionStringBuilder csb = new OracleConnectionStringBuilder();
         // fix parameters of builder here

         using (OracleConnection conn = new OracleConnection(csb.ToString()))
         {
            conn.Open();

            _cmd = new OracleCommand(_strSQL, conn);

            using (_rdr = _cmd.ExecuteReader())
            {
               while (_rdr.Read())
               {
                  //copy from rdr to variables
               }
               //
               _rdr.Close();
            }
            //
            conn.Close();
         }
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         OracleCommand cmd = new OracleCommand();
         OracleDataReader rdr = null;

         CGTest<string, OracleCommand, OracleDataReader> test =
            new CGTest<string, OracleCommand, OracleDataReader>
               ("select * from db", cmd, rdr);

         test.PreviousValue();
      }
   }
}

Thank you. Your post helped me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.