Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Create A Generic Base Class
Message
 
To
10/06/2008 00:23:13
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01322719
Message ID:
01322815
Views:
10
AHHH - the blind man sees!!

Thanks Bonnie.




>Kevin,
>
>Basically, you'd program to the IDb Interfaces. So your GetConnection() method would actually return an IDbConnection (rather than a SqlConnection):
>
>
>public virtual IDbConnection GetConnection()  // or abstract instead of virtual, either way is ok
>{
>  // ...
>}
>
>
>Likewise for getting the Command (IDbCommand), the DataAdapter (IDbDataAdapter), etc.etc.etc.
>
>Your sub-classes then plug in the proper objects:
>
>
>public override IDbConnection GetConnection()
>{
>    // Get the connection string
>    string sConnString = _GetConnString();
>
>    // Create a connection and assign the connection string to it
>    SqlConnection oConn = new SqlConnection();
>
>    // etc.etc.etc.
>
>    return oConn;
>}
>
>
>~~Bonnie
>
>
>
>>I have the following code which returns a SQL Connection object:
>>
>>
>>public SqlConnection GetConnection()
>>{
>>    // Get the connection string
>>    string sConnString = _GetConnString();
>>
>>    // Create a connection and assign the connection string to it
>>    SqlConnection oConn = new SqlConnection();
>>    oConn.ConnectionString = sConnString;
>>
>>    // Try to open the connection
>>    try
>>    {
>>        oConn.Open();
>>    }
>>    catch (SqlException e)
>>    {
>>    }
>>
>>
>>    // Test the connection and if the connection was opened, return the connection
>>    // otherwise return null
>>    if (oConn.State == ConnectionState.Open)
>>    {
>>        // Return the connection
>>        return oConn;
>>    }
>>    else
>>    {
>>        return null;
>>    }
>>
>>}
>>
>>
>>
>>I anticipate having to connect to other DB's, so I would like to create an abstract base
>>class that has the GetConnection method, as well as other data methods. It will also
>>have connection string and other DB related properties on it. Then, all I have to do is
>>subclass it and modify it where needed.
>>
>>I'm not sure how to write a base class for this. Can anyone help out a newby?
>>
>>Thanks
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform