Click here to Skip to main content
15,883,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a data base in a c# project in my hard disk, but when i move my project to other computer and it's address change , it doesn't work , I think it's for this code:
C#
SqlConnection sqlconnection=new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\c#\LAS\LAS\mm.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");


can anyone help me to solve this problem?
Posted
Updated 27-Feb-11 6:55am
v2

You have hardcoded the URL for the database.
That is not the best approach. You need to place your database on a central server and allow all client machines to point to that server.

If it is a local database, make sure the database is always in the location you have given here.
 
Share this answer
 
Comments
CS2011 27-Feb-11 7:26am    
Good suggestion...But This it's kind of learning project. my 5 anyway for the suggestion.it's always a good practice
Look if you have your files in the drive with different name. As per now The connection string says it's in drive "G:\". if so change is to appropriate one and it should work fine
 
Share this answer
 
Comments
mehdi_k 27-Feb-11 9:32am    
can you give a sample code?
CS2011 27-Feb-11 20:49pm    
I can give you an example

See As per now your your connection string is "(@"Data Source=.\SQLEXPRESS;AttachDbFilename=G:\c#\LAS\LAS\mm.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");" and as per this your database file would in drive G:\

now Assume you have moved your to C:\ Drive and the folders remain same then your connection string would be changed to

"(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\c#\LAS\LAS\mm.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");"
mehdi_k 28-Feb-11 1:33am    
my problem is how it should change!
when my app is exe i can't change it's code!!
Don't hardcode the URL for the database, instead of that use it as
SqlConnection sqlconnection=new SqlConnection(@"Data Source=.\\SQLEXPRESS;database="Training";Integrated Security=True;Connect Timeout=30;User Instance=True");


Hope this will solve your problem.
 
Share this answer
 
Since problem resides in your connection string because you have hardcoded the database URL so you we need to specify a path that should be automatically detected by the system while connecting on other system also.

Substitute your connection string with the following and make sure to attach your database with databases option in SqlServer 2005 before executing your code:

string constr;
constr = "Data Source=.\\SQLEXPRESS;database="Library";Persist Security Info=False;Integrated Security=True;";


Substitute 'Library' database in above code with your database name only without specifying any path.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900