用ASP.NET还原与恢复SQL Server数据库

一个基于.NET实现的SQLServer数据库备份与恢复程序代码 立即下载

 需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:

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

在还原之前先执行这个存储过程,需要传递dbname,就是你的数据库的名字。下边是类的原代码:(web.config里的数据库连接字符串是constr)

using System;

using System.Configuration;

using System.Data.SqlClient;

using System.Data;

namespace web.base_class

{

/// <summary>

/// DbOper类,主要应用SQLDMO实现对Microsoft SQL Server数据库的备份和恢复

/// </summary>

public class DbOper

{

private string server;

private string uid;

private string pwd;

private string database;

private string conn;

/// <summary>

/// DbOper类的构造函数

/// </summary>

public DbOper()

{

conn=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();

server=cut(conn,"server=",";");

uid=cut(conn,"uid=",";");

pwd=cut(conn,"pwd=",";");

database=cut(conn,"database=",";");

}

public string cut(string str,string bg,string ed)

{

string sub;

sub=str.Substring(str.IndexOf(bg)+bg.Length);

sub=sub.Substring(0,sub.IndexOf(";"));

return sub;

}



/// <summary>

/// 数据库备份

/// </summary>

public bool DbBackup(string url)

{

SQLDMO.Backup oBackup = new SQLDMO.BackupClass();

SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();

try

{

oSQLServer.LoginSecure = false;

oSQLServer.Connect(server,uid, pwd);

oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;

oBackup.Database = database;

oBackup.Files = url;//"d:/Northwind.bak";

oBackup.BackupSetName = database;

oBackup.BackupSetDescription = "数据库备份";

oBackup.Initialize = true;

oBackup.SQLBackup(oSQLServer);

return true;

}

catch

{

return false;

throw;

}

finally

{

oSQLServer.DisConnect();

}

}

/// <summary>

/// 数据库恢复

/// </summary>

public string DbRestore(string url)

{

if(exepro()!=true)//执行存储过程

{

return "操作失败";

}

else

{

SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();

SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();

try

{

oSQLServer.LoginSecure = false;

oSQLServer.Connect(server, uid, pwd);

oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;

oRestore.Database = database;

oRestore.Files = url;//@"d:/Northwind.bak";

oRestore.FileNumber = 1;

oRestore.ReplaceDatabase = true;

oRestore.SQLRestore(oSQLServer);

return "ok";

}

catch(Exception e)

{

return "恢复数据库失败";

throw;

}

finally

{

oSQLServer.DisConnect();

}

}

}

private bool exepro()

{

SqlConnection conn1 = new SqlConnection("server="+server+";uid="+uid+";pwd="+pwd+";database=master");

SqlCommand cmd = new SqlCommand("killspid",conn1);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@dbname","port");

try

{

conn1.Open();

cmd.ExecuteNonQuery();

return true;

}

catch(Exception ex)

{

return false;

}

finally

{

conn1.Close();

}

}

}

}

asp.net备份和还原数据库(C#) ASP.NET备份还原数据库,以及sqlDMO的注册 立即下载

相关推荐

C#实现数据库备份与还原(两种方法)

用C#写的实现数据库备份与还原的工程(VS2008下可直接运行,低于08的版本可以拷贝代码实现,记得要在COM中添加SQLSMO引用,本例数据库为SuperMarket,请更改为你所需备份的数据库),个人特意将两种方法放在同一工程内,Form1是直接用SQL语句实现的,Form2是使用SQLSMO实现的,还有进度条控制,界面也挺漂亮的,因为为了个人存档。

用asp.net还原与恢复sqlserver数据库(转)

   上次做了个项目,涉及到数据库的还原和恢复,到网上找了一下,是利用SQLDMO实现的,只要添加SQLDMO引用就好了,然后利用下边的类的方法就可以实现了。我把原作者的类扩充了一下,可以自动识别web.config里 的数据库连接字符串,可以通过变量设置还原恢复的信息。需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:

要饭's Blog 1205

C# ASP.NET 模块 - 数据库备份与恢复模块

☆ 功能如下:☆ 备份、还原 SQL Server 数据表; 分离、附加 SQL Server 数据库; 备份、还原 Access 数据库;

用asp.net还原与恢复sqlserver数据库

上次做了个项目,涉及到数据库的还原和恢复,到网上找了一下,是利用SQLDMO实现的,只要添加SQLDMO引用就好了,然后利用下边的类的方法就可以实现了。我把原作者的类扩充了一下,可以自动识别web.config里 的数据库连接字符串,可以通过变量设置还原恢复的信息。需要注意的时还原,还原的时候问题最大了,有别的用户使用数据库的时候无法还原,解决办法就是在MASTER数据库中添加一个存储过程:cre...

weixin_33860553的博客 182

VisionPro开发实战:CogRecordDisplay控件显示与保存图片的完整流程(附C#代码示例)

本文详细解析了VisionPro开发中CogRecordDisplay控件的图像处理全流程,包括图像加载、显示优化、动态清屏及结果保存等关键操作。通过C#代码示例,展示了如何高效使用该控件进行工业视觉应用开发,特别适合自动化检测系统的开发者参考。

weixin_42523792的博客 276

Xtract or Ucfyber的最条理的整理入门级教程

XTRACT可以对任意截面进行全过程截面分析。工程中常用来分析各种截面(如异形柱、组合截面等)的非线性特征点及承载力校核。 xtract主要思路也是根据各种材料本构关系,基于平截面假定,用fiber划分截面进行全过程非线性分析。......

→★佐佑思维★← 8399

数据库还原与备份的代码(1)

使用如下代码 ASP.NET 备份和恢复 Sql Server 数据库,只需要拷贝下面源代码即可使用。备份Sql Server数据库的代码:string SqlStr1 = "Server=(local);database=" + this.DropDownList1.SelectedValue + ";Uid=sa;Pwd="; string SqlS

随风逝去.痕 1721

Ubuntu22.04 上ROS1 Noetic的安装与常见问题解决

本文详细介绍了在Ubuntu 22.04系统上安装ROS1 Noetic的两种主要方法:推荐使用社区维护的适配仓库进行便捷安装,并提供了从源码编译的备选方案。文章重点解决了安装后编译ROS包时极易遇到的“shared_mutex”等C++标准库冲突问题,并给出了其他常见错误的排查与解决指南,帮助开发者顺利搭建稳定的ROS开发环境。

weixin_29295541的博客 364

asp.net(C#)实现SQL2000数据库备份和还原

asp.net(C#)实现SQL2000数据库备份和还原using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.

大鸾翔宇,遨游苍穹! 561

华为硬件逻辑岗笔试题(一)

积少成多,集腋成裘,坚持!!! 目录 1. 进制转换 2. 状态机和编码方式 3. 存储器的分类 4. Verilog语法中的操作符 5. 对组合逻辑的认识 6. 对时序逻辑的认识 7. 竞争冒险的认识 8. 基本时序逻辑电路 9. 建立时间和保持时间 10. 同步时序电路 11. 组合逻辑和时序逻辑判断 13. 基本总线的理解 14.加法器 15.FPGA开发工具 1. 进制转换 1、十进制46.25对应的二进制表达式为( )。 A 101110.11...

qq_42235129的博客 1万+

mmpretrain学习笔记

(4)数据集路径 data_root=‘data/imagenet’–》data_root=‘data/Fruits Dataset/images’(7)适当的修改num_workers、epochs、batch_size、lr等。3、训练优化 :梯度下降算法、学习率参数、训练总轮次、学习率变化策略等。my_resnet18_8xb32_in1k.py 完整的配置文件。(6)split=‘train’、split='val’注释掉。2、数据:数据集划分、数据文件路径、批大小、数据增强策略等。

吃葡萄不吐葡萄皮 937

浏览器在线烧录ESP32:Web Serial固件刷写方案全解析

在嵌入式开发中,固件烧录通常依赖本地驱动、Python环境和命令行工具,这给非专业用户和现场交付带来了极高的门槛。Web Serial API作为现代浏览器提供的标准硬件访问接口,允许网页在用户授权下直接与串口设备通信,配合乐鑫官方维护的esptool-js库,将原本复杂的esptool.py烧录流程完整迁移到浏览器中。该方案利用操作系统通用串口驱动,绕开了CH340、CP210x等驱动安装难题,只需一根数据线和一个网页,即可完成擦除、写入、校验和重启等全部烧录步骤。无论是快速原型验证、教学演示、设备交付,

weixin_33834910的博客 415

【AI应用实战-OpenCode】OpenCode安装部署(一)

OpenCode是一款2026年流行的开源AI编程工具,兼具轻量代码编辑器和智能编码代理功能。其核心优势包括:完全免费开源(MIT协议)、全平台兼容、支持75+大模型(含本地部署)、内置完整编程功能链(代码补全/Git集成/AI调试等),且所有数据本地存储确保隐私安全。相比Copilot等商业工具,OpenCode提供更自由的模型选择和零门槛使用体验。安装要求Windows10+/macOS12+/Linux主流发行版,仅需200MB空间和4G内存。该工具完美兼容VSCode生态,支持终端/桌面/IDE三种

2430

mysql 到 oracle数据库服务器_mysql导数据到oracle说明

一、还原mysql数据库1、在mysql创建一个与备份同名的数据库。2、还原数据表结构。找到data文件夹的以数据库名命名的数据库,把已备份的相关的*.Frm和opt文件复制到该文件夹中。查找my.ini文件的“#Path to the database root”可得到路径。如下:#Path to the database rootdatadir="C:/Documents and Settin...

weixin_35869628的博客 116

[PCIe5.0] 5.3.1.4.2 D3Cold State

主电源(Vcc)完全断开,仅保留极少量辅助电源(Vaux)用于维持基本唤醒功能设备功能完全关闭,所有寄存器状态丢失(除非有特殊存储)需要完全重新初始化才能恢复工作比D3Hot状态功耗更低,但唤醒延迟更长。

系统固件工程师 452

半车(前后、左右)、整车悬架详细推导建模和simulink仿真分析

用达朗贝尔定理依据悬架数学模型,得到悬架模型微分方程,分别用simulink建立前后半车、左右半车悬架;结合转向2自由度模型与半车侧倾模型,对模型考虑相干性、延时性的C级随机路面下,仿真分析给定角阶跃输入下的侧倾稳定性。

Mr.邹的博客 1万+

动态备份SQL-SERVER数据库——SQLDMO

上周要写一个SQL-SERVER数据库备份还原的程序,很没有思路,只是记得上学的时候用过SQL语句来完成数据库的备份还原,其余的方式还没有尝试过。经过资料查找终于找到了解决方案,效果还不错,于是写下来一起分享。大家最常见的是使用sql命令来进行数据库备份,最常用的sql语句如下:backup  [dbname]   to   disk=[路径+文件名] restore  [db

3条鱼 的命名空间 1949

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

asp.net下实现数据库的备份与恢复

WebDataBak.aspx.cs bakServer.cs为自定义类代码核心代码:1.private void BtnDataBackup_Click(object sender, System.EventArgs e)  {   if ( lstDb.Items.Count == 0 )   {    pub.Alert("数据库列表不能为空!", this.Page);  

lee576的专栏 1659

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

asp.net在线恢复数据库

用于asp.net还原与恢复SqlServer数据库的KillSpid存储过程 CREATE PROCEDURE KillSpid(@dbName varchar(20)) AS BEGIN DECLARE @sql nvarchar(500) DECLARE @spid int SET @sql='DECLARE getspid CURSOR ...

weixin_34009794的博客 220

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

ASP.NET备份恢复SQL Server数据库

ASP.NET备份恢复SQL Server数据库本文关键词: ASP.NET 备份 恢复 SQL Server 数据库 一、备份SQL Server数据库 string SqlStr1 = "Server=(local);database=" + this.DropD

dengyiyu的专栏 434

ASP.NET如何存取SQL Server数据库图片

<!--BODY { FONT-FAMILY: Verdana,Arial,宋体; FONT-SIZE: 12px}td{FONT-FAMILY: Verdana,Arial,宋体; FONT-SIZE: 12px;color:#000000;}A:link {text-decoration: none; color:#000000}

1690

在asp.net中备份还原SQL Server数据库

在web中,安全性主要体现在两个方面:一个是程序安全性,即防止网页在插入恶意代码;另一个是数据库安全性,这个我们可以经常备份数据库来实现。在文中,我将演示如果在网页中备份和恢复数据库。其实备份和恢复数据库都是利用SQL Server提供的SQL语句来备份的。备份:use master;backup database @name to disk=@path;恢复:use master;restore

周公(周金桥)的专栏 1万+

Asp.Net 备份和恢复SQL SERVER 数据库

Asp.Net 备份和恢复SQL SERVER 数据库    我们通常备份数据库时,需要登录数据库服务器去备份和恢复,这样很不方便,其实SQL SERVER自带的命令可能让我们很简单地实现远程通过Asp.Net备份和恢复数据库。 BACKUP DATABASE 被备份的数据库名 TO DISK = 备份文件路径;ALTER DATABASE 被恢复的数据库名 SET OFFLI

sqlove的专栏 3378
上一篇: ASP.NET常用代码
下一篇: 弹出图像大小的窗口
web718
博客等级 码龄22年 34粉丝 280原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值