Connecting to a SQL Server Database from vb.net (I)

May 22nd, 2009

understanding Connection String

Introduction

If we want to connect to a SQL Server database type, the easiest method is to find the database with server explorer and connect, this let us to create Typed DataSets (on the fly), making drag and drop. This is a very common practice in the major internet tutorial that we can search (there are a lot), but if I am searching for major performance in the application I’m developing then I Hate. There is a well know myth that everything is made by a wizard (or helper) is made for improve the compatibility, not the performance, and this make performance penalizations.

To make a connection to a database, read from a table and can work with the data, ideally (without making array-like objects) is make our own access and connection functions.

The Connection String.

Usually, we need a connection string, that were indicated Connection host, instance ’s name, username, password, if it’s a trusted connection…

Structure for a connection string to connect to SQL Server.

Data Source=[machinename]\[instancename];Initial Catalog=[databaseName];Persist Security Info=[True/False];User ID=[loginInfo];Password=[loginPassword]

Defining:

Data Source= [machinename]\[instancename]

On [machinename] is the name or ip of the remote host in the lan which have installed SQL Server, and [Instance Name] is the name of the SQL Server which contains the database that we want to connect. In my case for example, the server who I connect have the ip 192.168.1.10 and the instance is named SQLEXPRESS.

In my case seems like this:
DataSource=192.168.1.10\SQLEXPRESS

Initial Catalog = [databaseName]

on [databaseName] is the name of the database which we want to work.

User ID=[loginInfo]; Pasword=[loginPassword]
Information of connection and credentials to connect to the database in that Server.

Persist Security Info=[True/False]
If the application that you are developing is for a lan, or in a local machine (application and the database are in the same machine), you can turn the property to [True], because this improve the connection, but make the same application is not making authentication for every query, like a credential’s cache. In the case that the information who travel through the application to the database is maked in non-reliable environments ( for example, remote connections through Internet, without any kind of VPN…)  could be a good advice to turn [False] this property

In the next articles, I explain how to make through Visual Basic .Net a connection to the database, using a little example database (with a table) and we make our own conection string step by step with that example database, and I will explain for a well practices making queries and the SQL Languages.

Hello world!

April 15th, 2009

Wellcome to Coding Horror,  This is my first post of a lot (I suppose). In a short time period I will add new contents to the web.