Connecting VB.NET to MSSQL Server

A modular and practical approach for switching between local and remote SQL Server connections

Posted by Hüseyin Sekmenoğlu on December 11, 2012 Backend Development

🧠 Why This Matters

There are very few reliable Turkish resources about VB.NET and MSSQL integration. Everyone should contribute what they know so we can move forward as a community. Sites like connectionstrings.com are excellent but lack Turkish versions. Someone needs to step up and translate or recreate them. If I do not share this knowledge and you do not either, then who will?

Let us return to the topic at hand.


⚙️ The Setup

In my most recent VB.NET project, I needed to connect to two different SQL Server instances:

  1. Local development SQL Server

  2. Remote production SQL Server

To handle both setups efficiently, I created a module inside the project. This module holds my connection settings and allows me to switch between environments with a simple comment toggle.


💾 Local Connection Code

Public conn As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Veritabani;User Id=sa;Password=Sifremiz;")

This line connects to a local SQL Server Express instance installed on the same machine.


🌐 Remote Connection Code

Public conn As SqlConnection = New SqlConnection("Data Source=192.16.1.2\SQLEXPRESS;Network Library=DBMSSOCN;Initial Catalog=Veritabani;User Id=sa;Password=Sifremiz;")

This line connects to the production SQL Server using a fixed IP address.


🔄 Switching Between Environments

During development, I uncomment the local connection line and comment the remote one. Before publishing the project, I reverse it.

This way, I do not have to rewrite my connection settings every time. I just call conn.Open() wherever I need to open the database connection.


🧱 Lessons Learned

Writing the connection strings was easy. The hard part was configuring the server itself. Unfortunately, I do not even remember the exact steps now. That is why I wrote this down. Not for any special reason but just to avoid forgetting again.