示例#1
0
        public int Restore_DataBase(string parstrFileName)
        {
            int intReturnCode = 1;

            try
            {
                StringBuilder strQry = new StringBuilder();

                string strDataBaseName = "InteractPayrollClient";
#if (DEBUG)
                strDataBaseName = "InteractPayrollClient_Debug";
#endif
                DataSet DataSet = new DataSet();

                strQry.Clear();

                strQry.AppendLine(" SELECT ");
                strQry.AppendLine(" SPID");

                strQry.AppendLine(" FROM MASTER.dbo.SYSPROCESSES");

                strQry.AppendLine(" WHERE DBID = DB_ID('" + strDataBaseName + "')");

                strQry.AppendLine(" ORDER BY ");
                strQry.AppendLine(" SPID DESC");

                clsDBConnectionObjects.Create_DataTable_Client(strQry.ToString(), DataSet, "Spid", 60);

                for (int intRow = 0; intRow < DataSet.Tables["Spid"].Rows.Count; intRow++)
                {
                    string strKill = "USE MASTER KILL " + DataSet.Tables["Spid"].Rows[intRow]["spid"].ToString();

                    try
                    {
                        clsDBConnectionObjects.Execute_SQLCommand_Client(strKill, 60);
                    }
                    catch
                    {
                    }
                }

                strQry.Clear();

                strQry.AppendLine("RESTORE DATABASE " + strDataBaseName + " FROM DISK = '" + parstrFileName + "' WITH REPLACE");

                clsDBConnectionObjects.Execute_SQLCommand_Restore_Client(strQry.ToString(), 60);

                intReturnCode = 0;
            }
            catch (Exception ex)
            {
                WriteExceptionLog("Restore_DataBase " + parstrFileName, ex);
            }

            return(intReturnCode);
        }