Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem with my database project.
I have developed a database software in visual C#. I have used sql server database ( .mdf ).
The problem is _
when i shift my whole software to another directory connection to database is not possible.There occurs an error.Similarly if i move my software package to another pc the same problem occurs. The program only runs without error in the directory in which i developed it.
I know the problem is due to connection string. i have coded a fixed connection string but when directory changes the string remains unchanged. I need a way that the program automatic locate the database. What can i do? Please help.
Posted

If you have your database as SQL Server, then the connection string should be independent of the database location - it should refer to the SQL server instance, which is not accessed by a file system structure. Without seeing the code, and the connection string in particular, it is difficult to be sure, but I would guess that your SQL connection string is attaching the database each time, rather than letting SQL server handle the database itself, and referring to the specific database via the "Initial Catalog" specification in the connection string. This has a number of problems: the DB disk location become critical - if you move the DB or the app relative to the DB then it may well fail as you have seen. It also means that there is a good chance that each user will be using his own copy of the database instead of sharing the DB between them.

Let SQL server handle the database - and access it via the SQL instance instead of directly.
 
Share this answer
 
add like this in app.Config
XML
<add name="abc" connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\abc.mdf;Integrated Security=True;User Instance=True" 
        providerName="System.Data.SqlClient" > 
</add>
 
Share this answer
 
v3

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