Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass in a parameter for a class set property (not class declaration)
    primarykey
    data
    text
    <p><strong>Background:</strong></p> <p>I am making a c# 4.0 class for re-use so I can send one parameter to most methods of my program and pass a ‘Connections’ object that can hold things like:</p> <ul> <li>ConnectionStrings </li> <li>DbConnection (i.e. OleDbConnection or SQLConnection) </li> <li>DataReader (i.e. OleDbDataReader or SQLDataReader)</li> </ul> <p>The reasoning is that I will eventually upgrade to SQL Server and don’t want to change my method parameters or code so much. Methods often have to call a quick query here and there from the same database. </p> <p><strong>Question:</strong></p> <p>How do I properly setup (and call) the right-hand side of the DRMDbConnection ‘set’ parameter for DbType (now hard-coded as integer 1) in the method below at line: </p> <pre><code>set { drmDbConnection = SetDRMDbConnection(1); } </code></pre> <p><strong>Sample Code:</strong></p> <pre><code>public class Connections { public string DRMDbConnectionString { get; set; } private object drmDbConnection; public object DRMDbConnection { get { return drmDbConnection; } set { drmDbConnection = SetDRMDbConnection(1); } } private object SetDRMDbConnection(int DbType) { if (DbType == 1) return new System.Data.OleDb.OleDbConnection(); else return new SqlConnection(); } </code></pre> <p>}</p> <p><strong>EDITED Oct 31</strong></p> <p>I updated the class code to use the recommended IDbConnection, IDbCommand and IDataReader. Note: without a change to the SQL Server connection string you will recieve an error: "There is already an open DataReader associated with this Command which must be closed first." Thus add this to connection string: "MultipleActiveResultSets=True;"</p> <p><strong>Updated Class code</strong></p> <pre><code>public class Connections // reusuable object to hold all database Connections and a convenient DataReader and Command object. // Its purpose is to also be able to handle your choice of database connection, i.e. OleDb or SQL Server. { public string DRMConnectionString { get; set; } public IDbConnection DRMConnection; public IDbCommand DRMCommand { get { return DRMConnection.CreateCommand(); } set { } } public int DRMConnectionType // must be called to setup database connection and command. The connectionstring must be previously set. { set { if (value == 1) DRMConnection = new System.Data.OleDb.OleDbConnection(DRMConnectionString); else DRMConnection = new SqlConnection(DRMConnectionString); } } public void CloseConnections() { if (DRMCommand != null) DRMCommand.Dispose(); if ((DRMConnection != null) &amp;&amp; (DRMConnection.State != ConnectionState.Closed)) DRMConnection.Close(); } } </code></pre> <p><strong>CALLING CODE</strong></p> <pre><code>var conn = new Connections(); conn.DRMConnectionString = "my connection string"; conn.DRMConnectionType = 2; conn.DRMConnection.Open(); try { using (var cmd = conn.DRMCommand) { cmd.CommandText = "SELECT * FROM MyTable"; using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { CallSubMethodThatAlsoOpensDataReader(conn); } } } } finally { conn.CloseConnections(); } CallSubMethodThatAlsoOpensDataReader(Connections Conn) { using (var cmd = Conn.DRMCommand) { cmd.CommandText = "SELECT * FROM MyOtherTable"; using (var rdr = cmd.ExecuteReader()) // Error: There is already an open DataReader associated with this Command which must be closed first. { ; // do stuff } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload