您好,欢迎访问一九零五行业门户网

web数据库备份恢复

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.data; using system.configuration; using system.collections
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.data;
using system.configuration;
using system.collections;
namespace webapplication1
{
    public partial class webform1 : system.web.ui.page
    {
        protected void page_load(object sender, eventargs e)
        {
        }
        protected void button1_click(object sender, eventargs e)
        {
            //引用web.config的数据库连接字符串 优点:数据库移植的时候可直接对web.config的数据库连接字符串中的ip进行更换.
            string con_str = configurationmanager.connectionstrings[testconnectionstring].tostring();
            sqlconnection conn = new sqlconnection(con_str);
            sqlcommand cmdbk = new sqlcommand();
            cmdbk.commandtype = commandtype.text;
            cmdbk.connection = conn;
            cmdbk.commandtext = @backup database +tbdb.text.trim()+@ to disk='..\backup\+tbback.text.trim()+@.bak' + with init;
            try
            {
                conn.open();
                cmdbk.executenonquery();
                system.web.httpcontext.current.response.write();
            }
            catch (exception)
            {
                system.web.httpcontext.current.response.write();
            }
            finally
            {
                conn.close();
            }
        }
        protected void button2_click(object sender, eventargs e)
        {
            //sqlconnection conn = new sqlconnection(server=.;database=master;user id=sa;password=1;trusted_connection=false);
            //conn.open();
            //引用web.config的数据库连接字符串 优点:数据库移植的时候可直接对web.config的数据库连接字符串中的ip进行更换.
            string con_str = configurationmanager.connectionstrings[testconnectionstring].tostring();
            sqlconnection conn = new sqlconnection(con_str);
            try
            {
                conn.open();
                //杀死所有正在使用要还原数据的数据库进程
                sqlcommand cmd = new sqlcommand(@use master select spid from sysprocesses,sysdatabases where sysprocesses.dbid=sysdatabases.dbid and sysdatabases.name=' + tbhfdb0.text.trim() + ', conn);
                sqldatareader dr;
                dr = cmd.executereader();
                arraylist list = new arraylist();
                while (dr.read())
                {
                    list.add(dr.getint16(0));
                }
                dr.close();
                for (int k = 0; k                 {
                    cmd = new sqlcommand(string.format(kill {0}, list[k]), conn);
                    cmd.executenonquery();
                }
                sqlcommand cmdrt = new sqlcommand();
                cmdrt.commandtype = commandtype.text;
                cmdrt.connection = conn;
                cmdrt.commandtext = @use master restore database + tbhfdb0.text.trim() + @ from disk= '..\backup\ + tbhffile.text.trim() + @.bak' + with replace;
                cmdrt.executenonquery();
                system.web.httpcontext.current.response.write();
            }
            catch (exception)
            {
                system.web.httpcontext.current.response.write();
            }
            finally
            {
                conn.close();
            }
        }
    }
}
其它类似信息

推荐信息