简体   繁体   English

在Asp.net中为库创建数据库的相对路径

[英]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. 在学校里,我是一个由四个人组成的团队的一部分,该团队致力于创建GUI,以将伪造公司的书面记录及其功能转换为数字格式。 We're using an ASP.NET website for this purpose. 为此,我们正在使用ASP.NET网站。

Basically we use stored procedures and C# classes to represent the database. 基本上,我们使用存储过程和C#类来表示数据库。 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. 我想知道是否可以通过某种方式在我的库的Settings.Settings.cs文件中(或通过其他方式)设置数据库的相对路径,因此我不必不断更改数据库的位置每次移动项目时连接字符串值。

I suppose I should also mention that the database is in an App_Data folder. 我想我还应该提到该数据库位于App_Data文件夹中。

You want to use Server.MapPath(...) 您想使用Server.MapPath(...)

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server. MapPath方法将指定的相对或虚拟路径映射到服务器上的相应物理目录。

if your database is in App_Data folder, you should use a connection string like this in web.config: 如果数据库位于App_Data文件夹中,则应在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. 无论您在哪里安装Web应用程序,都将引用App_Data。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM