Friday, October 12, 2012

connection sting VISUAL BASIC .NET and SQL SERVER 2005

there are many types on how to connect VISUAL BASIC .NET and SQL SERVER 2005. I definitely suggest this type of connection string. whenever I'm connecting vb.net and sql server 2005 i create a class inside the class i create a public method, public method because you can access it anywhere in your project

Imports System.Data.SqlClient


Public Function ExecuteQuery(ByVal Query As String) As DataTable
        Dim sqlcon As New SqlConnection("Data Source=COMPUTER -NAME\SQLEXPRESS;Initial Catalog=DATABASE NAME;Trusted_Connection=True;")
        Dim sqlda As New SqlDataAdapter(Query, sqlcon)
        Dim sqlcb As New SqlCommandBuilder(sqlda)
        Dim dt As New DataTable
        sqlda.Fill(dt)
        Return dt
    End Function

in Data Source change the computer name to you computer name
initial catalog is your database name
trusted connection is optional you can use your username "sa" by default and password "what password did you enter" and done you have now your connection string.

let say that you want to gather some data on your database. first create a variable dt as new datatable

Dim dt As New Datatable
dt = executequery("SELECT * FROM tablename")


0 comments:

Post a Comment