?
Solved

VB.net Build connection string for SQL 2008

Posted on 2014-01-21
6
Medium Priority
?
1,433 Views
Last Modified: 2014-01-21
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
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 39796629
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

0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39796633
<connectionStrings>
    <add name="ConnectionInfo" connectionString="SERVER=Servername;Database=databasename;User ID=YourUserID;password=YourPass;initial catalog=dbname;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
0
 

Author Comment

by:Murray Brown
ID: 39796651
Maybe I didn't explain myself well. I want code that builds the text of the connection string given the four parameters
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 39796971
In my code above where i declare the connection string just
Replace each value with your texbox.text
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39797044
Give a look at the SqlConnectionStringBuilder class in the documentation. It's been created exactly for this purpose.

Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder
builder("Data Source") = "(local)"
builder("Integrated Security") = True
builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"
Console.WriteLine(builder.ConnectionString)

Simply use the content of your TextBox to set the different properties.
0
 

Author Closing Comment

by:Murray Brown
ID: 39797075
Thanks very much
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question