Hello.
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

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

"Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=yes;"

for OleDb you can use

"Provider=SQLNCLI10;Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=Yes;"

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.

commented: this has been useful to me too. +1

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

My.Settings.Database

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.

Thanks guys.
I'll try your suggestions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.