简体   繁体   English

如何获取当前连接的数据库文件路径?

[英]How to get current connected database file path?

I am developing a web application. 我正在开发一个Web应用程序。 I need to get the database file path. 我需要获取数据库文件路径。 What's the way to get the database file path in c#? 在c#中获取数据库文件路径的方法是什么? for example: 例如:

E:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/Data/MyDatabase.mdf
select physical_name
from sys.database_files
where type = 0

The above is the SQL query to execute. 以上是要执行的SQL查询。 Below is the C# code that will retrieve and store this data in a string : 下面是将以string检索和存储此数据的C#代码:

SqlConnection DbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["CStringName"].ConnectionString);
SqlCommand GetDataFile = new SqlCommand();
GetDataFile.Connection = DbConn;
GetDataFile.CommandText = "select physical_name from sys.database_files where type = 0";

try
{
    DbConn.Open();
    string YourDataFile = (string) GetDataFile.ExecuteScalar();
    DbConn.Close();
}
catch (Exception ex)
{
    DbConn.Dispose();
}

Server.mappath将返回路径

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

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