1

I have multiple forms in my project. Sometimes I work on my laptop, sometimes I work on my desktop. The problem is, when transferring my project from laptop to desktop and vice versa, I must change my connection string on all forms in the project.

This is my connection string. I declared it globally.

Dim CN As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Documents and Settings\Adriane05\Desktop\ThesisDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

How can I change a single file without changing it on all forms?

1
  • 1
    If I understand the question correctly, Ronnie's problem is that the string has got a local directory path. An obvious solution would be to use a path that exists on both computers? Commented Nov 11, 2012 at 8:07

3 Answers 3

4

There are a few ways you could accomplish this. First is you could map your project folder to the same drive letter on all your computers so your connection string wouldn't change. I do this by default because my projects are on a drive that I encrypt with TrueCrypt

Another way is a simple code pattern. Store your connection string in a setting or global variable, and run something like this when your application loads:

if Environment.MachineName.Equals("laptop") then
    connectionString = ' Connection string 1
ElseIf Environment.MachineName.Equals("desktop") then
    connectionString = ' Connection string 2 
else
    connectionString = My.Settings.ConnectionString
End If

databaseProvider.Open(connectionString)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! sorry for the dumb question. Thx for this! now i can transfer my project from any computer :D
1

Declare a global variable (or constant) somewhere and replace the statement mentioned in your post by

Dim CN As New SqlConnection(NameOfGlobalVariable)

?

Comments

1

I would suggest to move connection string in config file. See these tutorials:

Store connection string in config

Connection Strings and Configuration Files

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.