I'm Having this problem since yesterday.
My professor wants us to create a VB.net Program connected with MS SQL.
The problem is when i put my VB.net Project to other pc, i need to change the
current connection String.

Currently im using this code for my Connection String.
Data Source=.\SQLEXPRESS;AttachDbFilename=D:\SQL\sqlNew.mdf;Integrated Security=True

5 Years
Discussion Span
Last Post by conio.charles

Prompt the user for the name of the database on first use and save the result in a settings variable for subsequent use. I'd also recommend changing your connection string so that is does not depend ona particular file name. For SqlDb you can use


for OleDb you can use


and for ADO.net you can use

"Driver={SQL Server};Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=yes;"

In all cases you shgould replace PUBS with txtSource.Text. However, if the database name is the same as the source machine, and the server instance is ".\SQLEXPRESS" then you do not need to modify the connection string via user input.

Edited by Reverend Jim

Votes + Comments
this has been useful to me too.

how can i do this?
Prompt the user for the name of the database on first use and save the result in a settings variable for subsequent use

i used the SqlDB but its shows cannot open database


If you go to the Settings page of your project proprties you can add an Application scope string variable with any name you want. Do not enter a value. You can access the value through My.Settings. For example, if your variable is named Database then you can access it like any other variable by


except that when you save a value into this variable it is saved so it is available the next time the app runs. When the form loads you can do

If My.Settings.Database = "" Then
    'prompt the user for a database name
End If

Alternatively, if the dataabase is the same name on all systems you can do

Dim constr As String = ""Server=.\SQLEXPRESS;Database=" _
                     & My.Settings.Database _
                     & ";Trusted_Connection=yes;"

and if the connection fails you can then prompt for an alternate database name or possible even a server name.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.