locked
portable database with .asp.net 4.0 RRS feed

  • Question

  • User331487592 posted

    I want to architect asp.net application is such a way that it could handle any database(mysql, sqlserver, access etc). Can some one explain me complete architecture of application with database layers so I only change database server in configuration and the Data Access work with any database.

    I want to use asp.net 4.0. please explain complete strategy.

    Thursday, July 14, 2011 3:58 AM

Answers

  • User-18289217 posted

    Please check out the following links:

    http://msdn.microsoft.com/en-us/library/system.data.common.dbproviderfactory.aspx

    http://msdn.microsoft.com/en-us/library/wda6c36e.aspx

    Basically you use DbProviderFactory class to achieve what you want: e.g.

    public static DbCommand CreateGenericCommand()
        {
            // get a database provider name
            string dataProviderName = ConfigurationManager.ConnectionStrings["YourConnStr"].ProviderName;
            // get a database connection string
            string connectionString = ConfigurationManager.ConnectionStrings["YourConnStr"].ConnectionString;
            // create a new data provider factory
            DbProviderFactory factory = DbProviderFactories.
            GetFactory(dataProviderName);
            // get a database specific connection object
            DbConnection conn = factory.CreateConnection();
            // set the connection string
            conn.ConnectionString = connectionString;
            // Create a database specific command object
            DbCommand comm = conn.CreateCommand();
            // set the command type to stored procedure
            comm.CommandType = CommandType.StoredProcedure;
            // return the initialized command object
            return comm;
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 14, 2011 4:20 AM

All replies

  • User-18289217 posted

    Please check out the following links:

    http://msdn.microsoft.com/en-us/library/system.data.common.dbproviderfactory.aspx

    http://msdn.microsoft.com/en-us/library/wda6c36e.aspx

    Basically you use DbProviderFactory class to achieve what you want: e.g.

    public static DbCommand CreateGenericCommand()
        {
            // get a database provider name
            string dataProviderName = ConfigurationManager.ConnectionStrings["YourConnStr"].ProviderName;
            // get a database connection string
            string connectionString = ConfigurationManager.ConnectionStrings["YourConnStr"].ConnectionString;
            // create a new data provider factory
            DbProviderFactory factory = DbProviderFactories.
            GetFactory(dataProviderName);
            // get a database specific connection object
            DbConnection conn = factory.CreateConnection();
            // set the connection string
            conn.ConnectionString = connectionString;
            // Create a database specific command object
            DbCommand comm = conn.CreateCommand();
            // set the command type to stored procedure
            comm.CommandType = CommandType.StoredProcedure;
            // return the initialized command object
            return comm;
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 14, 2011 4:20 AM
  • User331487592 posted

    I am using entity framework. Do I need to create different repository for each DB if yes then how I will load relavant respository.

    Monday, July 18, 2011 2:04 AM