韦德体育9pt备份名称和位置/span

  • 栏目:数据 时间:2020-04-14 23:02
<返回列表

前台代码:

%@ Page Language=C# AutoEventWireup=true CodeBehind=SqlDbMgmt.asp教程x.cs Inherits=SysSourceMgmt.SqlDbMgmt %

!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN

韦德体育 ,html xmlns=

head runat=server

title/title

/head

body

form id=form1 runat=server

div

table

tr

td style=width: 100px

span style=font-size: 9pt操 作 数 据 库/span

/td

td

asp:DropDownList ID=DropDownList1 runat=server Font-Size=9pt Width=124px

/asp:DropDownList

asp:TextBox ID=txtDbName runat=server/asp:TextBox

/td

td style=width: 100px

/td

/tr

tr

td style=width: 100px

span style=font-size: 9pt备份名称和位置/span

/td

td style=width: 100px

asp:TextBox ID=TextBox1 runat=server Font-Size=9pt Width=117px/asp:TextBox

/td

td style=width: 100px

span style=font-size: 9pt; color: #ff3300/span

/td

/tr

tr

td colspan=3

asp:Button ID=Button1 runat=server Font-Size=9pt OnClick=Button1_Click Text=备份数据库教程 /

/td

/tr

/table

/div

div style=width: 100%; height: 100px

table

tr

td style=width: 100px; height: 21px

span style=font-size: 9pt操 作 数 据 库/span

/td

td

asp:DropDownList ID=DropDownList2 runat=server Font-Size=9pt Width=124px

/asp:DropDownList

/td

td style=width: 100px; height: 21px

/td

/tr

tr

td style=width: 100px

span style=font-size: 9pt操 作 数 据 库/span

/td

td style=width: 100px

asp:FileUpload ID=FileUpload1 runat=server Font-Size=9pt Width=190px /

/td

td style=width: 100px

/td

/tr

tr

td colspan=3

asp:Button ID=Button2 runat=server Font-Size=9pt OnClick=Button2_Click Text=还原数据库 /

asp:Button ID=Button3 runat=server Font-Size=9pt OnClick=Button3_Click Text=强制还原数据库 /

/td

/tr

/table

/div

/form

/body

/html 后台:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.IO;

using System.Data;

using System.Diagnostics;

namespace SysSourceMgmt

{

public partial class SqlDbMgmt : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

try

{

string SqlStr1 = Server=(local);DataBase=master;Uid=sa;Pwd=;

string SqlStr2 = Exec sp_helpdb;

SqlConnection con = new SqlConnection(SqlStr1);

con.Open();

SqlCommand com = new SqlCommand(SqlStr2, con);

SqlDataReader dr = com.ExecuteReader();

this.DropDownList1.DataSource = dr;

this.DropDownList1.DataTextField = name;

this.DropDownList1.DataBind();

dr.Close();

con.Close();

SqlStr1 = Server=(local);DataBase=master;Uid=sa;Pwd=;

SqlStr2 = Exec sp_helpdb;

con = new SqlConnection(SqlStr1);

con.Open();

com = new SqlCommand(SqlStr2, con);

dr = com.ExecuteReader();

this.DropDownList1.DataSource = dr;

this.DropDownList1.DataTextField = name;

this.DropDownList1.DataBind();

dr.Close();

con.Close();

}

catch (Exception)

{

}

}

}

protected void Button1_Click(object sender, EventArgs e)

{

string dbName = string.Empty;

if (DropDownList1.Items.Count != 0)

{

dbName = DropDownList1.SelectedValue.Trim();

}

else

{

dbName = txtDbName.Text.Trim();

}

string SqlStr1 = Data Source=./sqlexpress;Initial Catalog=' + dbName + ';Integrated Security=True;

string SqlStr2 = backup database + dbName + to disk=' + this.TextBox1.Text.Trim() + .bak';

SqlConnection con = new SqlConnection(SqlStr1);

con.Open();

try

{

if (File.Exists(this.TextBox1.Text.Trim()))

{

Response.Write(script language=javascript教程alert('此文件已存在,请从新输入!');location='Default.aspx'/script);

return;

}

SqlCommand com = new SqlCommand(SqlStr2, con);

com.ExecuteNonQuery();

Response.Write(script language=javascriptalert('备份数据成功!');'/script);

}

catch (Exception error)

{

Response.Write(error.Message);

Response.Write(script language=javascriptalert('备份数据失败!')/script);

}

finally

{

con.Close();

}

}

protected void Button2_Click(object sender, EventArgs e)

{

string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称

string dbName = string.Empty;

if (DropDownList1.Items.Count != 0)

{

dbName = DropDownList1.SelectedValue.Trim();

}

else

{

dbName = txtDbName.Text.Trim();

} string SqlStr1 = Data Source=./sqlexpress;Initial Catalog=' + dbName + ';Integrated Security=True;

string SqlStr2 = @use master restore database + dbName + from disk=' + path + ';

SqlConnection con = new SqlConnection(SqlStr1);

con.Open();

try

{

SqlCommand com = new SqlCommand(SqlStr2, con);

com.ExecuteNonQuery();

Response.Write(script language=javascriptalert('还原数据成功!');'/script);

}

catch (Exception error)

{

Response.Write(error.Message);

Response.Write(script language=javascriptalert('还原数据失败!')/script);

txtDbName.Text = SqlStr2;

}

finally

{

con.Close();

}

}

/// summary

/// 恢复数据库,可选择是否可以强制还原

/// /summary

/// param name=databasename待还原的数据库名称/param

/// param name=databasefile带还原的备份文件的完全路径/param

/// param name=errormessage恢复数据库失败的信息/param

/// param name=forceRestore是否强制还原,如果为TRUE,则exec killspid '数据库名' 结束此数据库的进程,这样才能还原数据库/param

/// returns/returns

public bool RestoreDataBase(string databasename, string databasefile, ref string returnMessage, bool forceRestore, SqlConnection conn)

{

bool success = true;

string path = databasefile;

string dbname = databasename;

string restoreSql = use master;;

if (forceRestore)//如果强制回复

restoreSql += string.Format(use master exec killspid '{0}';, databasename);

restoreSql += restore database @dbname from disk = @path;;

SqlCommand myCommand = new SqlCommand(restoreSql, conn);

myCommand.Parameters.Add(@dbname, SqlDbType.Char);

myCommand.Parameters[@dbname].Value = dbname;

myCommand.Parameters.Add(@path, SqlDbType.Char);

myCommand.Parameters[@path].Value = path;

Response.Write(restoreSql);

try

{

myCommand.Connection.Open();

myCommand.ExecuteNonQuery();

returnMessage = 还原成功;

}

catch (Exception ex)

{

returnMessage = ex.Message;

success = false;

}

finally

{

myCommand.Connection.Close();

}

return success;

}

protected void Button3_Click(object sender, EventArgs e)

{

string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称

string dbName = string.Empty;

if (DropDownList1.Items.Count != 0)

{

dbName = DropDownList1.SelectedValue.Trim();

}

else

{

dbName = txtDbName.Text.Trim();

}

string returnMessage = string.Empty;

string SqlStr1 = Data Source=./sqlexpress;Initial Catalog=' + dbName + ';Integrated Security=True;

SqlConnection con = new SqlConnection(SqlStr1);

RestoreDataBase(txtDbName.Text, path, ref returnMessage, true,con);

Response.Write(returnMessage);

}

}

}

上一篇:良好的规则是只选所需 下一篇:数据库名.dbo.表名),因为此组件已作为此服务器安全配置的一部分而被关闭

更多阅读

代码如下复制代码,听说效率比较高的一

数据 2020-04-14
文章有二个实例一个是sql2000的分页存储过程一个是sql2005的分页存储过程,有需要的同学可以拿...
查看全文

数据库名.dbo.表名),因为此组件已作为此

数据 2020-04-14
数据库教程1:AAA数据库2:BBB 如果出现错误提示: SQL Server 阻止了对组件 'Ad Hoc DistributedQuer...
查看全文

韦德体育9pt备份名称和位置/span

数据 2020-04-14
前台代码: %@ Page Language=C# AutoEventWireup=trueCodeBehind=SqlDbMgmt.asp教程x.cs Inherits=SysSourceMgmt.SqlDbMg...
查看全文

友情链接: 网站地图

Copyright © 2015-2019 http://www.koi-bumi.com. 韦德体育有限公司 版权所有