Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中以编程方式读取sql server mdf头以获取日志文件信息#_C#_Sql Server - Fatal编程技术网

C# 如何在c中以编程方式读取sql server mdf头以获取日志文件信息#

C# 如何在c中以编程方式读取sql server mdf头以获取日志文件信息#,c#,sql-server,C#,Sql Server,我需要以编程方式附加数据库,但日志文件命名约定似乎不一样 例如: database1.mdfhasdatabase1.ldf,database2.mdfhasdatabase2\u log.ldf 等等 因此,我猜测关于日志文件的信息将在mdf文件的头数据中,但我不确定如何读取它 我在谷歌上搜索了一下,得到了这段代码,但这是为了阅读版本信息 using (FileStream fs = File.OpenRead(@"C:\database.mdf")) { using (BinaryR

我需要以编程方式附加数据库,但日志文件命名约定似乎不一样

例如:
database1.mdf
has
database1.ldf
database2.mdf
has
database2\u log.ldf
等等

因此,我猜测关于日志文件的信息将在mdf文件的头数据中,但我不确定如何读取它

我在谷歌上搜索了一下,得到了这段代码,但这是为了阅读版本信息

using (FileStream fs = File.OpenRead(@"C:\database.mdf"))
{
    using (BinaryReader br = new BinaryReader(fs))
    {
        // Skip pages 0-8 (8 KB each) of the .mdf file,
        // plus the 96 byte header of page 9 and the
        // first 4 bytes of the body of page 9,
        // then read the next 2 bytes

        int position = 9 * 8192 + 96 + 4;

        br.ReadBytes(position);

        byte[] buffer = br.ReadBytes(2);

        dbiVersion = buffer[0] + 256 * buffer[1];
    }
}
Server sqlServer = new Server(textServer.Text);
sqlServer.AttachDatabase(databasename, databasefiles);
======================================================

问题更新:

  • 我的应用程序安装sql express
  • 在磁盘上有许多.mdf和.ldf文件
  • 应用程序将所有数据库和日志文件复制到sql数据目录
  • 应用程序尝试以编程方式附加数据库
  • FileInfo mdf=新的FileInfo(dbfile)

    //这就是我的问题所在。显然,我不能假设日志文件名与具有ldf扩展名的mdf文件名相同。当时我认为有一种方法可以从mdf文件中读取头信息,其中包含ldf信息

    using (FileStream fs = File.OpenRead(@"C:\database.mdf"))
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            // Skip pages 0-8 (8 KB each) of the .mdf file,
            // plus the 96 byte header of page 9 and the
            // first 4 bytes of the body of page 9,
            // then read the next 2 bytes
    
            int position = 9 * 8192 + 96 + 4;
    
            br.ReadBytes(position);
    
            byte[] buffer = br.ReadBytes(2);
    
            dbiVersion = buffer[0] + 256 * buffer[1];
        }
    }
    
    Server sqlServer = new Server(textServer.Text);
    sqlServer.AttachDatabase(databasename, databasefiles);
    

    要将数据库和日志文件附加到该数据库,不必知道日志文件名。这将意味着大量的数据硬编码。使用SMO对象:

     Microsoft.SqlServer.Management.Smo.Server server = new ServerConnection("enter server name");
     Microsoft.SqlServer.Management.Smo.Database db = server.Databases("enter db name");
     Console.WriteLine(db.FileGroups[0].Files[0].FileName); 'the mdf file
     Console.WriteLine(db.LogFiles[0].FileName); 'the log file
    
    通过使用SMO,您不仅可以拥有sql server实例的句柄,还可以拥有实例上的每个数据库。但是,好的方面是您对数据库实例的句柄,它包含指向mdf文件和日志文件的指针。它避免了硬编码文件名


    它们都以.ldf结尾。。。如何确定mdf文件名?使用
    SMO
    (SQL管理对象)获取引用日志文件和mdf的数据库句柄。它将比硬编码名称容易得多。请记住,dba或数据库开发人员可以避免使用默认日志名,并根据需要命名它们。它们还可以更改这些名称,这样您的代码就可以很容易地抛出异常。避免这种类型的编程,因为它会导致异常/错误。我使用smo连接数据库,但这就是问题所在。我更新了上面的问题,因为我不能在这里放置任何易读的代码。我使用smo连接数据库,但这就是问题所在。我更新了上面的问题,因为我不能在这里放置任何易读的代码。