How to get the current path to the database file?

I am developing a web application. I need to get the path to the database file. How can I get the path to a database file in C #? eg:

E:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/Data/MyDatabase.mdf 
+4
source share
2 answers
 select physical_name from sys.database_files where type = 0 

The above SQL query to execute. Below is the C # code that will retrieve and store this data in string :

 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(); } 
+12
source

Server.mappath will return the path

-2
source

Source: https://habr.com/ru/post/1381417/


All Articles