C#能不能直接读取sqlserver备份的mdf文件的内容?

stefq 2012-07-24 10:53:53
我现在最想要实现的功能是,在没有装sqlserver的机器上面,用C#程序做的exe直接读取sqlserver数据库备份mdf文件的内容,将读取的内容转换成csv文件。

但是在网上查了很长时间,可能没有办法实现。

退而求其次的话,就是在至少装着sqlserver express的机器上面,能够运行我的程序,读取mdf文件。
昨天试了一下,直接在连接字符串里指定attachdbfilename的方法,貌似在有些机器上可以成功打开,有些机器上报各种各样的错误,类似下面这种

Could not open new database 'D:\新建文件夹\MANAGER.MDF'. CREATE DATABASE is abor
ted.
An attempt to attach an auto-named database for file D:\新建文件夹\Manager.mdf f
ailed. A database with the same name exists, or specified file cannot be opened,
or it is located on UNC share.
File activation failure. The physical file name "D:\新建文件夹\Manager_log.ldf"
may be incorrect.
The log cannot be rebuilt because there were open transactions/users when the da
tabase was shutdown, no checkpoint occurred to the database, or the database was
read-only. This error could occur if the transaction log file was manually dele
ted or lost due to a hardware or environment failure.

不同机器上出现的错误还不同,也有类似下面这种。
Failed System.Data.SqlClient.SqlException: Unable to open the physical file "C:\
Program Files\***\Data\MManager.ldf". Operating system erro
r 32: "32(另一个程序正在使用此文件,进程无法访问。)".
Could not open new database 'Test1'. CREATE DATABASE is aborted.
Cannot attach the file 'D:\新建文件夹\Manager.mdf' as database 'Test1'.
File activation failure. The physical file name "C:\Program Files\***\Data\MManager.ldf" may be incorrect.
The log cannot be rebuilt because there were open transactions/users when the da
tabase was shutdown, no checkpoint occurred to the database, or the database was
read-only. This error could occur if the transaction log file was manually dele
ted or lost due to a hardware or environment failure.

我用我成功过的机器上的mdf文件,放到运行失败的机器上面,也能运行成功。

这各种各样的失败,只能再退而求其次,把mdf文件直接真实地attach到数据库上面,然后直接读取数据库,读取完成后再detach。

想请问一下大家,一般要实现这样的功能,普通的做法应该是怎么样的?
通过attachdbfilename参数来连接的话,有什么要求吗?如果是本机上的数据库备份出来的mdf,直接这样连接不会和本机的同名数据库有冲突吗?通过attachdbfilename参数来连接,是不是需要detach?

其实想实现的功能,就是在用户机器没有安装sqlserver的情况下,就能运行我的程序直接读取mdf文件,通过任何方法实现都可以,比如在程序内部嵌入sqlserver的模块之类的方法,不知道能否解决。

谢谢大家,希望能帮一下小弟。。
...全文
604 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
续写经典 2012-07-24
  • 打赏
  • 举报
回复
数据库被占用
tiangong789 2012-07-24
  • 打赏
  • 举报
回复
无需ldf文件的数据库附加语句

USE [master]GOCREATE DATABASE [NGA_BMS] ON ( FILENAME = N'E:\XXX.mdf' )-这里单引号中添加mdf文件目录
stefq 2012-07-24
  • 打赏
  • 举报
回复
没人啊。。。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧