Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net Build connection string for SQL 2008

Hi

I have a simple form with four boxes:
1. Server
2. Database name
3. Username
4. Password

What VB.net code would I use to build a connection string for a SQL 2008 database.
Now I know you can look at http://www.connectionstrings.com/
but has anyone written VB.net code to do this
Avatar of Kamal Khaleefa
Kamal Khaleefa
Flag of Kuwait image

create this class(DBConnection)

Imports System.Data
Imports System.Data.SqlClient

Public Class DBConnection
 
    Public Function StartConnection() As SqlConnection

           Dim con As SqlClient.SqlConnection = Nothing
        Dim ConnectionString As String = "Data Source=myserver;Initial Catalog=myDataBase;user id=MyUser;password=MyPassword"
     
        'End Try


        Try
            con = New SqlClient.SqlConnection(ConnectionString)
         
        Catch ex As Exception

            Return con
        End Try
        Return con




    End Function


End Class

Open in new window


then in your code call your data base as


  Dim dt As New DataTable


        Dim oSQLConnection As System.Data.SqlClient.SqlConnection = (New DBConnection).StartConnection
        Try

            oSQLConnection.Open()

            Dim adapt As New SqlClient.SqlDataAdapter(" Select * from my table ", oSQLConnection)
            adapt.Fill(dt)


        Catch ex As Exception

        Finally
            Try
                oSQLConnection.Close()
            Catch ex As Exception

            End Try
        End Try

Open in new window

<connectionStrings>
    <add name="ConnectionInfo" connectionString="SERVER=Servername;Database=databasename;User ID=YourUserID;password=YourPass;initial catalog=dbname;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
Avatar of Murray Brown

ASKER

Maybe I didn't explain myself well. I want code that builds the text of the connection string given the four parameters
In my code above where i declare the connection string just
Replace each value with your texbox.text
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much