private void 数据恢复ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "请选择目录";
fbd.SelectedPath = "C:";
if (fbd.ShowDialog() == DialogResult.OK)
{
string SqlStr1 = "server=.;database=Northwind;uid=sa;pwd=123";
string fileName = fbd.SelectedPath;
string SqlStr2 = "restore database Northwind from disk='" + fileName + "备份数据.bak'";
SqlConnection conn = new SqlConnection(SqlStr1);
conn.Open();
SqlCommand cmd = new SqlCommand(SqlStr2, conn);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("数据文件备份成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception er)
{
MessageBox.Show(er.Message.ToString());
}
未能获得对数据库的排它访问权 该怎么处理哦 还想要个功能如果路径内有这个文件则备份成功 否则弹出该路径下无文件
帮我改下代码吧!~~顺便有个疑问做的软件给别人安装完 对方没有安装SQL 那软件里需要的数据该保存到哪里去
{
try
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "请选择目录";
fbd.SelectedPath = "C:";
if (fbd.ShowDialog() == DialogResult.OK)
{
string SqlStr1 = "server=.;database=Northwind;uid=sa;pwd=123";
string fileName = fbd.SelectedPath;
string SqlStr2 = "restore database Northwind from disk='" + fileName + "备份数据.bak'";
SqlConnection conn = new SqlConnection(SqlStr1);
conn.Open();
SqlCommand cmd = new SqlCommand(SqlStr2, conn);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("数据文件备份成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception er)
{
MessageBox.Show(er.Message.ToString());
}
未能获得对数据库的排它访问权 该怎么处理哦 还想要个功能如果路径内有这个文件则备份成功 否则弹出该路径下无文件
帮我改下代码吧!~~顺便有个疑问做的软件给别人安装完 对方没有安装SQL 那软件里需要的数据该保存到哪里去
解决方案 »
- 求教。net2.0 运行在64位系统时候如何访问32位注册表路径
- 取字符串子串问题
- C# 写入和读取地址到注册表
- Reporting Services 安装时为何检测不到已经安装的IIS?(WIN 2003操作系统)
- ContextMenu click 事件无效???
- 大家好!好久没来提问了,又得来麻烦大家,关于怎么在工程里添加已注册好的全局程序集的问题!很奇怪
- 高分求解---------电脑上安装了多个打印机,每一个有没有打印任务,状态如何获取?
- Microsoft Agent Control 2.0 桌面精灵
- c#做的程序在win98下怎样才能运行,20分相送
- 关于Windows Service的问题...
- 再问:如何在c#中调用API-WaitForMultipleObjects?
- 向高手请教:关于结构的问题
--查询分析器中,用下面的代码来处理use master
goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_killspid]
GOcreate proc p_killspid
@dbname sysname--要关闭进程的数据库名
as
declare @s nvarchar(1000)
declare tb cursor local for
select s='kill '+cast(spid as varchar)
from master..sysprocesses
where dbid=db_id(@dbname)open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
go--用法
exec p_killspid 'MSLearning' -- 注,MSLearning 为数据库名
*************************************************************
以上我是在数据查询分析器里建立了存储过程p_killspid,使用p_killspid。你可以在程序里执行。
无法找到数据类型 Northwind。
参数 '@dbname' 的数据类型无效。
我对SQL一点都不会
试试SQLDMO
用代码判断文件是否存在,
FileInfo TheFile = new FileInfo("路径");
if (TheFile.Exists)
{......}
总要有数据库才能存数据取数据
@dbname 'Northwind' 这里有问题
@dbname sysname--要关闭进程的数据库名
@dbname 'Northwind' 这里有问题你干吗要把 sysname改成“'Northwind'”?
{
string path = "e:\test";//自己选个要备份的路径
string SqlStr1 = "server=.;uid=sa;pwd=123";
string sqlstr3 = "backup database northwind to disk= ' " + path + " '";
SqlConnection conn = new SqlConnection(SqlStr1);
conn.Open();
sqlcommand cmd = new sqlcommand(sqkstr3,conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("备份成功! ");
}
catch
{
MessageBox.Show("备份失败! ");
}
finally
{
conn.Close();
} }
觉得数据库的备份就选个具体路径比较好.如果要用SaveFileDialog那就把数据表备份成EXCEL更好吧
只是个人看法.
string fileName = fbd.SelectedPath;
string SqlStr2 = "restore database Northwind from disk='" + fileName + "备份数据.bak'"; 还原数据库时,应该连到master上。还要保证没有其他进程正在使用 要还原的数据库。