Greener
Greener

Reputation: 1387

Creating a relative path to a Database in Asp.net for a library

In school I am part of a team of four working to create a GUI to translate the paper records of a made-up company and their functionality to a digital format. We're using an ASP.NET website for this purpose.

Basically we use stored procedures and C# classes to represent the database. The folder we're using for the project contains the site and the libraries in separate folders. If I try to open the site from the folder containing both these elements the site will not run. I want to know if there is some way I can set up a relative path to the database in the Settings.Settings.cs file (or by some other means) of my libraries so I don't have to constantly change the database location for the connection string value every time we move the project.

I suppose I should also mention that the database is in an App_Data folder.

Upvotes: 0

Views: 1854

Answers (2)

JotaBe
JotaBe

Reputation: 39065

if your database is in App_Data folder, you should use a connection string like this in web.config:

<add name="Database1ConnectionString1" 
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\Database1.mdf;
Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />

|DataDirectory| will refer to App_Data, wherever you install your web app.

Upvotes: 0

asawyer
asawyer

Reputation: 17808

You want to use Server.MapPath(...)

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

Upvotes: 1

Related Questions