hi all,
---put in mind that im doing an ASP.NET website....

i made a DB using VS2010 (installed SQL server 2008 with the VS 2010 installation).

the DB is in the App_Data folder.
in the default.aspx.cs file i wrote this

Sqlconnection con = new sqlconnection();
con.connection = "server=.\\SQLEXPRESS;AttachdbFile=|DataDirectory|\\Database.mdf;Database=Database;Trusted=true;"

this results in an error:::

"Cannot attach file "C:\bbbb\bbbb\bbb\Database.mdf" to Database "Database"".

whats the problem here?
by the way, the code is in the page_load function and i have included the SQL classes

Maybe its the problem in using the substitution to your database -> |DataDirectory|.


|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.

For example, instead of having the following connection string:
"Data Source= c:\program files\MyApp\Mydb.sdf"

Using DataDirectory, you can have the following connection string:
“Data Source = |DataDirectory|\Mydb.sdf”

To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
• For applications that are put in a folder on the user's computer, the database folder uses the application folder.
• For applications that are running under ClickOnce, the database folder uses the specific data folder that is created.

So try changing the string to the full path.

Mitja

the problem is, i want it to be a relative path, because im in a team of 5
i cant keep on changing the code!

now i ddnt call the AppDomain.SetData method ...
is it a must? and if so .. how do u use it?

Edited 6 Years Ago by "ICode": n/a

Don't put the connection string in the code, put it in the Application settings. That way you don't have to worry about it, the people using it (the other developers) will have to set it.

This article has been dead for over six months. Start a new discussion instead.