有一种方法可以通过对我有用的代码来获取数据库的 .mdf 和 .ldf ,为此,使用连接字符串打开与数据库的连接,准备 SQL 命令,使用查询
select physical_name from sys.database_files where type = 0
然后执行命令,顺便说一下,,type = 0
是针对mdf,type = 1
是针对ldf,也可以直接在SSMS中使用这个查询
这是示例代码,请记住您需要获取连接字符串
string _mdfCommand = "select physical_name from sys.database_files where type = 0";
string _ldfCommand = "select physical_name from sys.database_files where type = 1";
SqlCommand GetSQLData = new SqlCommand();
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
GetSQLData.CommandText = _mdfCommand;
GetSQLData.Connection = connection;
string mdf_Path= (string)GetSQLData.ExecuteScalar();
GetSQLData.CommandText = _ldfCommand;
GetSQLData.Connection = connection;
string ldf_Path= (string)GetSQLData.ExecuteScalar();
connection.Close();