Solved

VB.net Build connection string for SQL 2008

Posted on 2014-01-21
6
1,373 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:murbro
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:murbro
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 500 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:murbro
ID: 39797075
Thanks very much
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now