How do I release sql express database?
Questions
I have a simple app that
uses an SQL Express 2005 database. When the user closes the app, I want
to give the option to back up the database by making a copy in another
directory. However, when I try to do it, I get "The process cannot
access the file '...\Pricing.MDF' because it is being used by another
process." I closed the connection, disposed the connection, set it to
nothing, and GC.Collect(), but it makes no difference. My connection
string is "Data
Source=.\SQLEXPRESS2005;AttachDbFilename=|DataDirectory|\Pricing.mdf;Integrated
Security=True; User Instance=True" and I just keep using the same
connection throughout. I didn't see where I could detach the database
to counter the attach in the connection string.
1 - How do I RELEASE the thing? 2 - Is there a better way than just
copying the database? The app is for my husband only, so I will be able
to handle it if he actually does need to restore from backup.
Thanks!
--------------------------------------------------------------------------------------------------
Answer
You don't want to copy the mdf directly because SQL keeps most of
the changes in the transaction log, take a look at the modified time
after running some queries, it doesn't write directly to the file. I
noticed this while setting up an rsync job.
Having SQL generate the backup is much safer and more desirable,
single-user or multi-user. You can provide a link to a function calling
the T-SQL which you can completely automate as far as source db and
destination folder:
BACKUP DATABASE [mydatabasename]
TO DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Scheduled Task Backups\mydatabasename-backup' WITH NOFORMAT, NOINIT, NAME = N'mydatabasename-Full Data
SQL 2005 had introduced another T-SQL syntax to do this, for the life
of me I can't find it. But there are ways to do it through M$$SQL
without having the full blown database server.
相关文档:
SQL Server Out Put Excel File
在 SQL Server中, 导出EXCEL 文件, 用到 bcp.exe
bcp 导出的本质是纯文本文档,
若数据含有中文,请导出到中文版EXCEL,或TXT文档等, 否则乱码....
用TXT 打开英文版EXCEL,也可以,
导出 数据到C:\authors.xls, 若文件存在则重写文件, 不存在则创建文件
  ......
--完整备份
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Full_20070908.bak'
--差异备份
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Df_20070908.bak'
With Dferential
--日志备份默认截断日志
Backup Log NorthwindCS
To disk='G:\Backup\NorthwindCS_Log_20070908. ......
今天有个学生问我调用包里面具有游标类型的输出参数,可是在用PL/SQL程序调用的时候居然报出参数个数或类型错误的异常信息,于是自己也试了一下,果然是出错了。然后就自己琢磨了一下、查查资料和问问同事,原来问题是很简单的,先看一下程序的结构代码:
CREATE OR REPLACE PACKAGE Pkg_S ......
SQL Server 2005数据库镜像简介
自从SQL Server 2000以来,你已经能够通过使用复制来创建一个备用的服务器、传输日志,以及备份和重新存储了——但是现在微软又引入了一个内建的工具,它可以实现自动的错误恢复。数据库镜像是SQL Server 2005的一个新特性,它允许你将一个SQL Server中的数据库内容镜像到另一个S ......
调整的目的就是为了消耗最小的资源来完成功能,通过查看执行计划和各种统计信息来分辨调整后的sql对资源的耗费情况,来找出一个成本最小的sql语句
检查系统的I/O问题
vmstate能检查整个系统的iostat(IO statistics)
查看该SQL的response time(db block gets/consistent gets/physical reads/sorts (disk)) ......