如何阅读C#编程的日志文件的信息SQL Server的MDF头 [英] how to read sql server mdf header for log file information programmatically in c#

查看:119
本文介绍了如何阅读C#编程的日志文件的信息SQL Server的MDF头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式连接数据库,但日志文件的命名约定似乎并不相同。



例如:结果
database1.mdf 数据库1。 LDF database2.mdf database2_log.ldf
等。 ..



所以,我的猜测是关于日志文件中的信息将在MDF文件的标题数据,但我不知道如何阅读



我做了谷歌搜索和得到这个代码,但它是阅读的版本信息。

 使用(的FileStream FS = File.OpenRead(@C:\database.mdf))
{
使用(BinaryReader BR =新BinaryReader(FS))
{
//跳转页面0-8(各8 KB)的.mdf文件,
//加9页的96字节的头和
//第4第9页的正文中,
字节//然后阅读接下来的2个字节

INT位置= 9×8192 + 96 + 4;

br.ReadBytes(位置);

字节[]缓冲= br.ReadBytes(2);

dbiVersion =缓冲器[0] + 256 *缓冲液[1];
}
}

=========== ===========================================



问题的更新:




  1. 我的应用程序安装的SQL Express

  2. 有许多.mdf和磁盘上的.ldf文件

  3. 应用程序会将所有数据库和日志文件到SQL数据目录

  4. 应用程序试图附加数据库编程



FileInfo的MDF =新的FileInfo(DBFILE);

 数据库名称= mdf.Name.ToLower()更换(@,@中密度纤维板)。 
StringCollection databasefiles =新StringCollection();
databasefiles.Add(mdf.FullName);
databasefiles.Add(mdf.FullName.ToLower()更换(@,@)。LDF);



//这是在那里我有问题。很显然,我不能假设日志文件名是相同与LDF扩展MDF文件名。那是当我以为会有读取MDF文件头信息的方法,这将有LDF信息。

 服务器SQLSERVER =新服务器(textServer.Text); 
sqlServer.AttachDatabase(数据库名称,databasefiles);


解决方案

您不应该知道的日志文件名为了附加数据库和一个日志文件到该数据库。这将意味着大量数据的硬编码。使用SMO对象:

  Microsoft.SqlServer.Management.Smo.Server服务器=新ServerConnection(中输入服务器名); 
Microsoft.SqlServer.Management.Smo.Database DB = server.Databases(输入数据库名称);
Console.WriteLine(db.FileGroups [0] .Files [0] .FileName); MDF文件
Console.WriteLine(db.LogFiles [0] .FileName); 日志文件



通过使用SMO,你不但拥有一个句柄到你的SQL Server实例,以及作为实例每一个数据库。但好处是你的数据库实例句柄,包含指向两个MDF文件和日志文件。它避免了硬编码的文件名。



下面是关于SMO的MSDN


I need to attach databases programmatically but the log file naming conventions don't appear to be same.

For example:
database1.mdf has database1.ldf, database2.mdf has database2_log.ldf and so on...

So, my guess was that the information about the log file would be in the header data of the mdf file, but I'm not sure how to read it.

I did a google search and got this code, but it's to read the version information.

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];
    }
}

======================================================

question updated:

  1. My app installs sql express
  2. Have many .mdf and .ldf files on a disk
  3. app copies all the databases and log files to sql data directory
  4. app attempts to attach the databases programmatically.

FileInfo mdf = new FileInfo(dbfile);

databasename = mdf.Name.ToLower().Replace(@".mdf", @"");
StringCollection databasefiles = new StringCollection();
databasefiles.Add(mdf.FullName);
databasefiles.Add(mdf.FullName.ToLower().Replace(@".ldf", @"")); 

//this is where I have issue. Obviously I can't assume that the log file name would be the same as mdf file name with ldf extension. Thats when I thought there would be a way to read the header information from mdf file, and that will have ldf information.

Server sqlServer = new Server(textServer.Text);
sqlServer.AttachDatabase(databasename, databasefiles);

解决方案

You shouldn't have to know the log file name in order to attach a database and a log file to that database. That would mean a lot of hard coding of data. Use SMO objects:

 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

By using SMO you will have not only a handle to your sql server instance as well as every single database on the instance. But the nice thing is the handle you have to the database instance, contains pointers to both the mdf file and the log file. It avoids having to hard code file names.

Here's the MSDN on SMO

这篇关于如何阅读C#编程的日志文件的信息SQL Server的MDF头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆