Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Creating a Connection to the Data Source Using Sql Server

Author: Faraz

This article will help you to get connected to the Sql Server database. Here I’ll discuss the basic Sql connection string and the properties involve.

Using Sql server you can perform many kind of operations but before this you need a connection. To create a connection, you need to specify a value for its ConnectionString property. This ConnectionString defines all the information the computer needs to find the data source, log in, and choose an initial database. Consider the following two examples, the first one shows the connection with Sql Server database using OleDb:


Dim SQLConn As New OleDbConnection

SQLConn.ConnectionString = "Provider=SQLOLEDB.1;" _ & "Password=YourPassword;Persist Security Info=True;" _  & "User ID=YourUserID;Initial Catalog=YourDatabaseName;" _ & "Data Source=NameOfSQLServer"


The second example shows the connection with Sql server database using SqlClient. Here we use the SqlConnection object:


Dim strConn As String = "Server=localhost;UID=YourUID; " _ & "Password=YourPassword;Database=YourDatabaseName"

Dim SQLConn As New SqlClient.SqlConnection(strConn)


In above two examples you have seen a series of distinct pieces of information, separated by semicolon (;). The description of these pieces of information is as follows: 


Provider: This is the name of the OleDb provider, which allows communication between ADO.Net and your database. This must be specified because it tells the OleDb the type of database with which you want to connect.


Data Source: This indicates the name of the server where the data source is located. Notice if the server is on the same computer hosting the ASP.Net site the writing “localhost” is sufficient.


Initial Catalog: This is the name of the database that this connection will be accessing. It’s only the “initial” database because you can change it later, by running a Sql command or by modifying the database property.


User ID (UID): This is used to access the database. The “UID” is alternative to User ID.


Password (Pwd): This applies to the Sql Server Authentication only. “Pwd” is an alternative to “Password”.


Connection Timeout: This determines how long your code will wait, in seconds, before generating an error if it cannot establish a database connection.


Initial Catalog (Database): This indicates the name of the database with which we want to connect. “Database” is an alternative to the “initial catalog”


Trusted_Connection: Contains two values yes or no. Setting to “Yes” enables Windows NT Authentication.

Article Comments
Thanks, your article was just the perfect match for me.
I hope to get more lessons on connecting to daabase and and binding controls to a dataset , etc and all neccessary codes to connect to a database in VB.NET

Posted on 1/24/2007 4:14:38 AM by Tola

how to lists the databases (all) which is available in localhost system ? it wil be shown in the combobox in the windows form. how we wl get all tables in particular database. how table is convert into XML file?

Posted on 3/11/2009 4:20:37 AM by GEETHA

Add Article Comment:
Name :
Email Address :
Comments :
<< How to define and use Cursors in SQL Server 2000.

Disclaimer - Privacy
© 2002-2018