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?
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.
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?
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"
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.