Results 1 to 5 of 5

Thread: Connection string to MSSQL 2008 VB.NET

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    364

    Connection string to MSSQL 2008 VB.NET

    Hi Guys, I'm working on some app which have to connect to the database (MSSQL2008).
    My connection string looks as follow:

    Code:
    #Region "[--------First of all you need database connection be set up---------]>"
        Public Function GetConnection() As OleDbConnection
            '[-------------Below is for MS SQL Server 07 / 2000 Connection String------------------]>
                Dim strConn As String = "Server=JMPANOPIO;UID=sa;PWD=123456; Database=AHD"
    
            Try
                Dim Conn As OleDbConnection = New OleDbConnection(strConn)
    
                If Conn.State <> ConnectionState.Open Then
                    Conn.Open()
                End If
    
                Return Conn
            Catch
                Throw New Exception("The connection with the database can't be established.")
            End Try
        End Function
    #End Region
    Of course dosen't work [Exception Details: System.Exception: The connection with the database can't be established.]. I suppose the reason I cannot to get MSSQL is that the string is old. Anyway can You please tell me how the connection string should looks like?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Connection string to MSSQL 2008 VB.NET

    You cannot use an OleDbConnection, oledb is used for Microsoft Access. For SQL, you have to import System.Data.Sql and use a SqlConnection. For a list of connection strings, look here.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    364

    Re: Connection string to MSSQL 2008 VB.NET

    Yes:
    this should be like this?:
    Code:
    Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
    But can You please take a look at the rest of the code? Is there any that have to be changed?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Connection string to MSSQL 2008 VB.NET

    Other than the obvious OleDb being changed to SqlClient? Two things that I'd do differently:
    1) I wouldn't open the connection string in a function, if anything it should probably be a Sub
    2) Don't name your region that. Name it "SqlConnection"
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Connection string to MSSQL 2008 VB.NET

    If you google on connection string, the top choice should be a pretty excellent resource for building whatever connection string you need for whatever database you happen to be using. I don't bother memorizing the various options because it is all so readily available.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width