Solved

VB.net Build connection string for SQL 2008

Posted on 2014-01-21
6
1,362 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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

20 Experts available now in Live!

Get 1:1 Help Now