Connectionstring
A connection string is a string which is used to gain access to a database.
A provider is a part of the connection string. A provider is a driver which is a library that can access the datasource in binary form. With the provider, we can setup a connection from an application and gain access to the datasource. In the connection string, we specify the appropriate provider depending on the datasource we wish to access. Note that when we need to connect to sql server we need not mention the provider.
Let us examine each part of the connection string.
VB 2005
string connectionString = “server=SYS2;” + “integrated security=SSPI;” + “database=FinAccounting”;
In the above example, server is the machine name of the computer that is running sql server. Integrated security indicates that current users windows credentials (username and password) are being used to access sql server. Control strings have other properties like connection pooling, length of timeout period and security options. In the example above, database refers to the name of the specific database that we would like to access.
Connection string is a property of the connection object.
To connect to a Microsoft access database, use the following code.
Dim myConn As OleDbConnection = New_
OleDbConnection ()
MyConn.ConnectionString = _
“Provider = Microsoft.Jet.OLEDB.4.0; Data “ & _
“Source = C : \ data \ northwind.mdb ; User Id = guest; “ & _
“Password = isw3083”
We need to specify a provider name in the connection string when we are using a OLEDB .Net data provider. This is because, OLEDB .Net data provider is used to connect to several types of databases.
Giving away user name and password in the connection string code is vulnerable to phishing. A better option to overcome is using the Windows integrated security. Consider the following string.
string connectionString = “server=SYS2;” + “integrated security=SSPI;” + “database=FinAccounting”;
Below is an example of a connection string used to access a sql server database which on the remote server on the internet hosted on a web server, using C# code in aspx file.
string connectionString = "Data Source=201.73.323.39;database=tatabye;User
ID=sssabc123;Password=scss456;";
In the above code, we assign the IP address of the server to the datasource, assigned the name of the database, assigned the username, assigned the password. The above code is used to connect to a database hosted on a webserver.
CONNECTION OBJECT
The Connection object establishes a connection to the database. Two of the most common Connection objects used are OleDbConnection and SqlConnection. Both the SqlConnection and OleDbConnection namespaces inherit from the IdbConnection object. The commonly used property of the Connection object is the connection string property.
ConnectionString property
This property is used to provide information, such as the datasource and database name, that is used to establish the connection with the database.
C#2005
string connectionString = “server=SYS2;” + “integrated security=SSPI;” + “database=FinAccounting”;
The connectionString property is a long string with several attributes separated by semicolons. In the above connectionString, server is the name of the SQL Server, or ‘Local’ if you are running SQL Server on the same machine. We can replace the ‘server’ with the word ‘Data Source’ and ‘database’ with ‘Initial Catalog’. Integrated Security is to specify that the connection should be secure. The possible values for Integrated Security are ‘true’, ‘yes’, and ‘sspi’, which specify a secure connection.
Connection string using APP.config file
Configuration files are used to store information about application settings. These files are also referred to as Application Configuration Files. APP.config files.
Information is stored as Key-Value pairs. After we write the data source information in a program, it will be difficult to change the data source information. This will create problems when we deploy the programs from test server to production servers.
Till .Net was released this information was stored this information in .ini files or in system registry. It is not easy job to open the registry, locate entries and make appropriate changes.
.NET gives a simple and easy solution for this problem - the Application Configuration File. Each application can have a configuration file, which is actually an XML file. Any text editor can be used to open the configuration file and change values. The application will load the values from this configuration file. There is no need change your code every time there is change in data source or any other information stored in the configuration file.
Click here for More informatin on
Connection strings
app.config for Windows applications
Windows applications in VS.NET use the name app.config by default for the configuration file. This will not be automatically created when you create a Windows application. If you need a configuration file for your application, open your project in VS.NET, go to the 'Solution Explorer' and right click on the project name. Choose Add > Add new item from the menu and select 'Application Configuration file' from the list of choices. This will create an app.config file for you in the application root.
By default, the app.config file will have the following content:
< xml version="1.0" encoding="utf-8" ?>
< configuration>
< appSettings>
< add key="Northwind" value="data source=localhost;initial catalog=Northwind;password=;persist
security info=True;user id=sa;workstation id=CPU-TENOTEBOOK;packet size=4096" />
appSettings>
configuration>
And to read from this config file, just use the following code in your application:
Imports System.Configuration
Public Class Form1
Inherits System.Windows.Forms.Form
Dim dbpath As String = ConfigurationSettings.AppSettings("DatabasePath")
End Class
ConfigurationSettings is the class used to access the contents of the configuration file. Since this class is part of the namespace System.Configuration, we have to use the fully qualified name System.Configuration.ConfigurationSettings. As a shortcut, you can use the using directive on top of the file like below:
In VB 2005 Imports System.Configuration
Note: When you compile your application, VS.NET will automatically create a file called
.exe.config in your bin\debug folder. The contents of the app.config will be automatically copied to this new config file when you compile the application. When you deliver the application to the end user, you have to deliver the exe and this new config file called .exe.config and NOT the app.config. Users can modify the data in .exe.config file and application will read the data from the config file, when restarted.
web.config for Windows applications
The web applications use the same concept, but they use a config file with the name web.config. There are couple of things to note in this case.
web.config is created automatically by VS.NET when you create any web project.
When you compile the web application, web.config is NOT renamed or copied to the BIN folder.
web.config has several default entries in it to support web/IIS configuration & security.
You can add the section in the web.config and add your key/value pairs in that section.
You can have separate web.config files for each directory in your web application, in addition to the one in the root. For each web page, by default, system will look for a web.config in the same folder as the page and if not found, then looks in the parent folder.
Free Resources