Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB 2005 Connection to MS SQL 2008 Server

Status
Not open for further replies.

markhkram

IS-IT--Management
Dec 30, 2008
32
0
0
US
I have a newbie question, that I have been trying to find, but cannot find anything. Not sure if it too simple to find, but here it is...

I have a vb 2005 app, and a database with 1 table in MS SQL 2008. All I want to do is a simple SELECT query on the 1 table.

Can someone give me a quick example on how to connect to the MSSQL db, and do a simple select query? (connection string and any references i need to enable would be helpful)

Also, is there more than one way to connect? If so, which one is faster? (ADO connection?)

Thanks!
 
You first need to determine the security method of connecting. Do you require authentication with a user name as password (as associated through an ODBC connection) or not.


Here is some basic code to help:
Code:
Dim CONN As SqlConnection

CONN = New SqlClient.SqlConnection

Try
   With CONN
      'Trusted Connection            
      .ConnectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI"

      'Standard Connection
      .ConnectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=UserPassword")

      .Open()
   End With

Catch ex As Exception
   messagebox.show ex.message
End Try

I hope this helps.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Thanks! I appreciate it. Do I need a .Close() on that? Or does it automatically close?

Thanks again!
 
When your done with the connection, you can close it with a .close. It doesn't hurt. This saves the computer from doing it when the application is ended.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top