Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# DB Connection Code Question
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01311457
Message ID:
01311592
Views:
18
Ahh I see. Yes you are correct the SqlConnectionBuilder is only for MS SQL.
I don't know how generic you can make it. You will for sure have to pass along information about what kind of database you are connecting to. Most database servers have their own connection string format. A good reference is : http://www.connectionstrings.com/


Here's what I have now. This works. I want to modify it so that all I have to do is specify the database type (SQL, MySql, Accces...) and it builds the connection string dynamically using the properties on the base class. It will also determine which provider to use (SQL, OleDe or ODBC).

By this is pretty good for a first attempt. What do you think?
namespace DSFactory
{
    public class DSFactory : DSFactoryBase
    {


        public DataSet GetDataSet(string sQuery)
        {
            DataSet oDataSet = new DataSet();
            IDbConnection oConnection = _GetConnection();
            IDbDataAdapter oAdapter = _GetDataAdapter(sQuery);

            oAdapter.Fill(oDataSet);

            return oDataSet;

        }

    }


    public class DSFactoryBase
    {
        // Define exposed members
        public bool bTrustedConnection = false;
        public string sDatabase = "";
        public string sPassword = "";
        public string sServer = "";
        public string sUserName = "";
        public string sConnString = "";

        int iConnectionType = 1;

        protected IDbConnection _GetConnection()
        {
            IDbConnection oConnection = null;

            switch (iConnectionType)
            {
                // Sql Data Provider
                case 1:
                    oConnection = new SqlConnection(sConnString);
                    break;

                // OleDb Data Provider
                case 2:
                    oConnection = new OleDbConnection(sConnString);
                    break;

                // ODBC Data Provider
                case 3:
                    oConnection = new OdbcConnection(sConnString);
                    break;

                default:
                    break;

            }
            return oConnection;
        }


        protected IDbDataAdapter _GetDataAdapter(string sQuery)
        {
            IDbDataAdapter oAdapter = null;

            switch (iConnectionType)
            {

                // Sql Data Provider
                case 1:
                    oAdapter = new SqlDataAdapter(sQuery, sConnString);
                    break;

                // OleDb Data Provider
                case 2:
                    oAdapter = new OleDbDataAdapter(sQuery, sConnString);
                    break;

                // ODBC Data Provider
                case 3:
                    oAdapter = new OdbcDataAdapter(sQuery, sConnString);
                    break;

                default:
                    break;

            }
            return oAdapter;
        }

    }
}
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