views:

405

answers:

5

I am setting up a .net project that is called to generate other webpages. Basically a true CODE BEHIND page. How do I go about making a connection on a Class Library File, when there is no webconfig file present/available?

A: 

System.Data namespace. If using SQL Server, a SQLConnection object.

I use the Application Data Blocks for data access b/c I think it is a time saver. That framework is great for SQL Server access.

Cody C
A: 

Will your library be called from an ASP.net site? If so, you can still use the same methods for accessing the application settings. Otherwise, you may need some kind of app.config for whatever front end you're putting on this.

Paddy
A: 

Also, you don't need a connecting in your configuration in most class libraries, unless you are using an ORM which needs to use it to pick up some info. The calling application's configuration will be used when you are running the thing, so it will pick up the configuration out of the web.config or whatever.

Wyatt Barnett
A: 

There are several ways to solve this:

1) If you're class library is really going to generate web pages, then it will probably be deployed on the server with the web.config and so can use properties of the web.config file. If you don't want to use the ConnectionString part, then you can just make an appSetting with the connection string.

The benefits of doing this are that it's easy to change or have different connection strings for different environments (testing, deployment, etc.). And if you decide to use the class library in another kind of app, you can use the app.config file with the same setting.

Depending on the type of database, you'll be using the System.Data and possibly some of the sub-namespaces to make this work.

2) You can hard code the connection string into the code. Ugly, but workable in a pinch.

3) You could use an entity framework to help you out and cut down on the work that you have to do.

Jared
+3  A: 

In this case, I would create a 'Base Page' that derives from System.Web.UI.Page. On this page, you would create a property called 'ConnectionString'. You will have all of your web pages you create inherit from this page.

Example:

public partial class BasePage : System.Web.UI.Page
{
    protected string ConnectionString
    {
        get
        {
            return System.Configuration.ConfigurationSettings.AppSettings["MyConnectionString"];
        }
    }

}

And in your web.config

<appSettings>
     <!-- Connection String -->
     <add key="ConnectionString" value="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"/>
</appSettings>

Now you can have a web.config file with a connection string that is easily readable. And you can store this 'Base Page' in your class library, no problem. As long as your web pages inheriting from it are using the web.config file with the connection string.

After that, it's a simple matter of connecting using that string. You can do this in a variety of ways, either in your web pages, or in separate classes (I recommend separate classes). In this case, if you had a separate class, you would need to pass in the ConnectionString property to your connecting functions:

public void ExecuteQuery(string connectionString, string sql)
        {
            using (System.Data.SqlClient.SqlConnection theConnection = new System.Data.SqlClient.SqlConnection(connectionString))
            using (System.Data.SqlClient.SqlCommand theCommand = new System.Data.SqlClient.SqlCommand(sql, theConnection))
            {
                theConnection.Open();
                theCommand.CommandType = CommandType.Text;
                theCommand.ExecuteNonQuery();
            }
        }

or you can create a function that does not take a connection string parameter, and just reads from the web.config file. It sounds like you may want to put your connecting and data access code in a class library for good separation of data and content. Hope this helps!

theJerm