Results 1 to 4 of 4

Thread: Common code for oledb and slqConnection

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    141

    Common code for oledb and slqConnection

    Imagine I've got two separate functions like this:-


    Code:
    void do_oledb(string strNewValue)
    {
       // Do something with a JET database
       OleDbConnection con = new OleDbConnection();
       OleDbCommand cmd = new OleDbCommand();
       con.ConnectionString = JetConStr; // The JET DB Connection string
       cmd.Connection = con;
    
    
       con.Open();
       cmd.CommandText = "UPDATE myTable SET myField = '" + strNewValue + "'";
       cmd.ExecuteNonQuery();
       con.Close();
    }

    Code:
    void do_sql(string strNewValue)
    {
       // Do something with an SQL Server database
       SqlConnection con = new SqlConnection();
       SqlCommand cmd = new SqlCommand();
       con.ConnectionString = SqlConStr; // The SQL Svr DB Connection string
       cmd.Connection = con;
    
    
       con.Open();
       cmd.CommandText = "UPDATE myTable SET myField = '" + strNewValue + "'";
       cmd.ExecuteNonQuery();
       con.CLose();
    }
    You can see that most of the code is identical.
    What I'd like to do is have this in one single function. Some conditional code that sets up the connection and command objects etc. so that when I get to using the common code it doesn't care what type of connection we have. Of course in the real world we'd get differences in syntax of th commandtext and things like that but please humour me and come up with the smallest amount of code for a common function that could replace both of the above.

    Just curious - humour me.

  2. #2
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    739

    Re: Common code for oledb and slqConnection

    Well this appears to work

    Code:
    void do_any(string strNewValue)
    {
       IDBConnection con;
       IDBCommand cmd;
       strCon = "";
    
       if(useOlDb == true)     // Some conditional stuff here
        {
            con = new OleDbConnection();
            cmd = new OleDbCommand();
            strCon = JetConStr;
        }
        else
        {
            con = new System.Data.SqlClient.SqlConnection();
            cmd = new System.Data.SqlClient.SqlCommand();
            strCon = SqlConStr;
        }
    // But below here it's all common
       con.ConnectionString = strCon;
       cmd.Connection = con;
    
       con.Open();
    
       cmd.CommandText = "UPDATE myTable SET myField = '" + strNewValue + "'";
       cmd.ExecuteNonQuery();
    
       con.Close();
    }
    But it's just using a simple nonquery. Not sure if it would work with a datareader.
    Last edited by IanS; Dec 6th, 2012 at 06:11 AM.

  3. #3
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    739

    Re: Common code for oledb and slqConnection

    Although switching the above code to use

    System.Data.Common.DbConnection con;
    System.Data.Common.DbCommand cmd;

    would allow the use of System.Data.Common.DbDataReader

    which solves the dataReader problem (IDBxxxxxxxx doesn't have a datareader)

  4. #4
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: Common code for oledb and slqConnection

    Look into the System.Data.Common namespace and the DbProviderFactory. These allow you to create db specific objects in a generic way. With this, you would use the Db[Object] classes to interact, eg: DbCommand, DbConnection, DbParameter, etc....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width