ConnectionString for Databases using different Libraries


When creating a connection, you must specify several of pieces of required information. You should give type of authentication or user to authenticate, the location of database server, and the name of the database. OLEDB connection strings specify an OLEDB provider, and ODBC connection strings specify ODBC driver. So specify this information we use ConnectionString property.
In short, we can say that: ConnectionString holds all the ingredient to build a connection with database.
Now I’m going to tell how we can build different ConnectionString using different libraries such as SqlClient, OleDB, and Odbc in .NET FRAMEWORK environment.
Firstly, we should know what are the basic parameters of ConnectionString.
ParametersDescription
AttachDBFilename / Initial File NameWe can use it, if we want to connect to an attachable database file such as .mdf file. Normally, we use the Initial Catalog parameter instead.
Connect Timeout / Connection TimeoutThe length of time to build a connection with database, the default time to build a connection is 15 seconds, if connection does not build in 15 seconds, it generates timeout error and 0 second represents an infinite wait.
Data Source / Server / Address / Addr / Network AddressWe provide server name or network address of the database in this parameter. Normally, we use localhost for the current computer.
Initial Catalog / DatabaseWe provide database name in this parameter to use for all operation such insertions, deletions, updations, queries, and so on.
Integrated Security / Trusted_ConnectionDefault value of integrated security is false, when we set it to true or SSPI the .NET provide attempts to connect to the data source using Windows Integrated Security.
Persist Security InfoWhen we set to false, password is removed from the ConnectionString property as soon as the connection is opened. Thus, you can’t retrieve this information in your code.
User IDThe account of database user should provide in this parameter
Password/PwdWe give password in this parameter corresponding to the user id.
ConnectionString For SQL Server Database:
Connection builds within the Windows system.
string ConnectionString = @”server=localhost; database=your DB name;integrated security=true;”;
Connection builds to a remote location.
string ConnectionString = @”server=your located DB IP address;database=your DB name; user SQL Server account id; pwd=password corresponding to the id;”;
ConnectionString For OLEDB:
string ConnectionString = @”Data Source=localhost;”+”Initial catalog= Northwind; user id; password=your password;”+”Provider=SQLOLEDB”);
Here’s an example of MS Access file.
string ConnectionString = @”Data Source=localhost;” + “Initial catalog= c:\Nortwdind.mdb;” + “Provider=Microsoft.Jet.OLEDB.4.0″);
ConnectionString For ODBC:
Here’s an example of MS Access file and MS Excel file.
string ConnectionString = @”Driver={Microsoft Excel Driver (*.xls)};” + “DBQ=c:\book1.xls”);
string ConnectionString = @”Driver={MySQL ODBC 3.51 Driver};” +”Database= test;UID=root; PWD=secret;Option=3″);
I hope this article help you to make your desirable ConnectionString.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger