Solved

VB.net Build connection string for SQL 2008

Posted on 2014-01-21
6
1,379 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

778 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