I am developing a web application. I need to get the database file path. What's the way to get the database file path in c#? for example:
E:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/Data/MyDatabase.mdf
I am developing a web application. I need to get the database file path. What's the way to get the database file path in 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. Below is the C# code that will retrieve and store this data in a 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();
}
Server.mappath
will return the path