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

Dani AI

Generated

For @ICode: that error usually means SQL Server will not attach the MDF file. Typical causes are (a) the file is already attached under the same logical database name, (b) the connection-string keys or C# usage are incorrect, or (c) the SQL Server service account lacks access to the MDF/LDF. Fixes depend on which of those applies.

A concise, correct example (C# + web.config) showing proper class/property use and connection-string keys:

using System.Data.SqlClient;

var connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Initial Catalog=Database;Integrated Security=True;User Instance=True";
using (var con = new SqlConnection(connStr))
{
    con.Open();
    // database work
}
<connectionStrings>
  <add name="DefaultConnection"
       connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Initial Catalog=Database;Integrated Security=True;User Instance=True"
       providerName="System.Data.SqlClient" />
</connectionStrings>

Troubleshooting steps: check SQL Server Management Studio for an already-attached database named "Database" and detach it if you want AttachDbFilename to reattach; alternatively remove AttachDbFilename and point to the server database via Initial Catalog. Ensure the SQL Server service account has read/write rights to the file in App_Data. As noted, |DataDirectory| works for web apps (you normally do not need to call AppDomain.SetData), and as suggested, keep the connection string in web.config so each developer can set their local value without editing code.

Recommended Answers

All 3 Replies

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?

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.

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.