标题:关于C#写数据库备份的覆盖还原问题
取消只看楼主
蝴蝶为花醉
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-15
结帖率:0
已结贴  问题点数:10 回复次数:1 
关于C#写数据库备份的覆盖还原问题

string rebackup = "use master RESTORE DATABASE db_SMS FROM DISK='F\db_SMS.bak' with replace";
                try
                {
                    SqlConnection.ClearAllPools();//清空连接池
                    
                    //杀掉数据库连接的进程
                    string connection = "server=.;database=master;uid=sa;pwd=123456;Trusted_Connection=False";
                    SqlConnection conn = new SqlConnection(connection);
                    conn.Open();
                    SqlCommand killCmd = new SqlCommand("use master; SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name='db_SMS' ", conn);
                    SqlDataReader myreader=killCmd .ExecuteReader ();
                    ArrayList list = new ArrayList();
                    while (myreader.Read())
                    {
                        list.Add(myreader.GetInt16(0));
                    }
                    myreader.Close();
                    for (int i = 0; i < list.Count; i++)
                    {
                        killCmd = new SqlCommand(string.Format("kill {0}", list), conn);
                        killCmd.ExecuteNonQuery();
                    }

                    if (dbll.DataBackupInfo(rebackup) != 0)
                    {
                        SqlConnection.ClearAllPools();//清空连接池
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('还原成功!');", true);
                        bind();
                    }
                }
                catch (Exception)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('还原失败!');", true);
                }

dbll.DataBackupInfo(rebackup)是我调用三层的方法
public int DataBackupInfo(string strWhere)
        {
            return DbHelperSQL.ExecuteSql(strWhere);
        }
问题是为什么用上述代码实现数据库备份的覆盖还原为什么要重启下sql才能还原成功,或者是我从未打开过sql server也能还原成功,只要我把sql server中的数据改动或直接把表打开,就会抛出错误:执行killCmd.ExecuteNonQuery()时 提示.附件有语法错误。我实在不明白这到底是为什么,问题该怎么解决,等待高手帮忙
搜索更多相关主题的帖子: master 数据库备份 数据库连接 database 
2013-05-15 09:13
蝴蝶为花醉
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-15
得分:0 
刷新可以理解,但是有解决办法么,若在程序中总是出现错误页或功能实现不了,这不太好
2013-05-18 20:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-407113-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.347002 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved