备份还原数据库

chaye12 2008-07-10 07:58:29
最近开发了一个C/S的客户计件工资软件系统,最后一个菜单是备份还原数据库功能,想点击button后对数据备份,点击领一个button后对数据库进行还原,请问该如何去做呢?
...全文
100 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bikerboy 2008-07-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Guyschaos 的回复:]
浪费我表情!
[/Quote]

怎么会浪费呢?

Thanks for sharing!
chaye12 2008-07-10
  • 打赏
  • 举报
回复
问题是客户在点击备份按钮后,我该去写什么代码,才能让客户备份呢????
sxmonsy 2008-07-10
  • 打赏
  • 举报
回复
直接使用存储过程来备份.
chaye12 2008-07-10
  • 打赏
  • 举报
回复
3楼的能不能说的详细点,,,我没用过,很急帮帮忙!
贫僧又回来了 2008-07-10
  • 打赏
  • 举报
回复
浪费我表情!
贫僧又回来了 2008-07-10
  • 打赏
  • 举报
回复
access就是一个文件嘛!关闭数据库连接后直接用file.copy()拷贝可以不?
wuyi8808 2008-07-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chaye12 的回复:]
我的是ACCESS数据库,该如何去做呢??
[/Quote]
那就直接对mdb文件进行备份和还原(文件复制)就可以了。
chaye12 2008-07-10
  • 打赏
  • 举报
回复
我的是ACCESS数据库,该如何去做呢??
很急,麻烦各位帮帮忙!
Adechen 2008-07-10
  • 打赏
  • 举报
回复
直接使用SQL语句备份
贫僧又回来了 2008-07-10
  • 打赏
  • 举报
回复
这样做的话需要添加SQL的SQLDMO组件!也可以直接使用SQL语句备份!
备份数据库
USE master
GO
BACKUP DATABASE master
TO disk = 'e:\jj'
WITH FORMAT,
NAME = 'jj'
GO


还原数据库
USE master
GO
restore DATABASE master
from disk = 'e:\jj'
WITH FORMAT,
NAME = 'jj'
GO


public void DbBackup(string databasename, string filedir, string servername)
{
SQLDMO.Backup beifen = new SQLDMO.BackupClass();
SQLDMO.SQLServer servers = new SQLDMO.SQLServerClass();
try
{
servers.LoginSecure = false;
servers.Connect(servername, "sa", "");
beifen.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
beifen.Database = databasename;
beifen.Files = filedir;
beifen.BackupSetName = filedir;
beifen.BackupSetDescription = "数据库备份";
beifen.Initialize = true;
beifen.SQLBackup(servers);
}
catch (Exception ex)
{
throw ex;
}
finally
{
servers.DisConnect();
}
}



public void DbRestore(string database, string file, string servername)
{
if (exepro(database, servername) != true)
{
MessageBox.Show("操作失败");
}
else
{
SQLDMO.Restore res = new SQLDMO.RestoreClass();
SQLDMO.SQLServer Servers = new SQLDMO.SQLServerClass();
exepro(database, servername);
Servers.LoginSecure = false;
Servers.Connect(servername, "sa", "");
res.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
res.Database = database;
res.Files = file;
res.FileNumber = 1;
res.ReplaceDatabase = true;
res.SQLRestore(Servers);
Servers.DisConnect();
}
}

public bool exepro(string database, string server)
{
SqlConnection conn = new SqlConnection("server=" + server + ";uid=sa;pwd=;database=master");
SqlCommand cmd = new SqlCommand("killspid", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dbname", database);
try
{
conn.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
conn.Close();
}
}

create proc killspid (@dbname varchar(20))
as
begin
declare @sql nvarchar(500)
declare @spid int
set @sql='declare getspid cursor for
select spid from sysprocesses where dbid=db_id('''+@dbname+''')'
exec (@sql)
open getspid
fetch next from getspid into @spid
while @@fetch_status<>-1
begin
exec('kill '+@spid)
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end
GO
JustLovePro 2008-07-10
  • 打赏
  • 举报
回复
不知道你的这个操作是不是针对数据库的backup and restore?
没有这样做过,帮顶~

111,073

社区成员

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

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

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