コード例 #1
0
        public bool loginSeccess(Login loginForm)
        {
            DialogResult result = DialogResult.OK;

            try
            {
                result = loginForm.ShowDialog(this);
            }
            catch (Exception)
            {
            }
            if (result == DialogResult.OK)
            {
                UserInfo userinfo = new UserInfo {
                    UserName = loginForm.UserName, PassWorld = loginForm.PassWorld, LoginType = loginForm.LoginType
                };
                string connectionString;
                string shili = ConfigHelper.GetAppConfig("shiLiName");
                if (loginForm.LoginType == LoginType.FromIdAndPsw)
                {
                    connectionString = @"Data Source=" + shili + ";Initial Catalog=master;User ID=" + loginForm.UserName + ";Password="******"Data Source=" + shili + ";Initial Catalog=master;Integrated Security=True";
                }
                if (ConnectionTestInfo.ConnectionTest(connectionString))
                {
                    userinfo.ConnectionString = connectionString;
                    userinfo.BaseName         = ConfigHelper.GetAppConfig("baseName");
                    userinfo.AppPath          = Application.StartupPath;
                    SQLHelper.strConn         = connectionString;
                    this.UserInfo             = userinfo;
                    ConfigHelper.UpdateAppConfig("idAndPsw", UserInfo.UserName + "," + UserInfo.PassWorld + "," + UserInfo.LoginType);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                System.Environment.Exit(0);
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// 初始化代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Shown(object sender, EventArgs e)
        {
            listLog2.AddToButtom("开始加文件夹载备份和还原快照...");
            int fileGhostType = Common.getFileGhost(listLog2, Application.StartupPath);

            if (fileGhostType == 0)
            {
                listLog2.AddToButtom("备份和还原快照加载完成...OK");
            }
            else if (fileGhostType == 1)
            {
                listLog2.AddToButtom("找不到备份快照文件,请先进行首次备份...");
            }
            else if (fileGhostType == 2 || fileGhostType == 3)
            {
                //todo:禁用还原按钮
                listLog2.AddToButtom("找不到还原快照文件,不可进行还原操作,请先进行备份...");
            }
            //Login loginForm = new Login();
            //string idAndPsw = ConfigHelper.GetAppConfig("idAndPsw");
            //if (hasLoginConfig(idAndPsw))
            //{
            //    //配置有效
            //    string[] idpsw = idAndPsw.Split(',');
            //    loginForm.UserName = idpsw[0];
            //    loginForm.PassWorld = idpsw[1];
            //    loginForm.LoginType = idpsw[2] == "FromSysTem" ? LoginType.FromSysTem : LoginType.FromIdAndPsw;
            //}
            //else
            //{
            //    //配置无效
            //    if (this.UserInfo == null)
            //    {
            //        //无Info
            //        loginForm.LoginType = LoginType.FromSysTem;
            //    }
            //    else
            //    {
            //        //有Info
            //        loginForm.UserName = UserInfo.UserName;
            //        loginForm.PassWorld = UserInfo.PassWorld;
            //        loginForm.LoginType = UserInfo.LoginType;
            //    }
            //}

            while (!loginSeccess(createNewLoginForm()))
            {
                listLog2.AddToButtom("数据库登陆失败...");
                MessageBox.Show("数据库登陆信息验证失败!请重新输入!");
            }
            listLog2.AddToButtom("数据库登陆成功...OK");
            listLog2.AddToButtom("正在验证数据库完整性");
            if (ConnectionTestInfo.CheckExistsDataBase(UserInfo))
            {
                listLog2.AddToButtom("数据库完整性验证成功...OK");
                listLog2.AddToButtom("开始验证表单数据...");
                if (ConnectionTestInfo.checkTables(listLog2, UserInfo))
                {
                    listLog2.AddToButtom("表单校验成功...OK");
                }
                else
                {
                    listLog2.AddToButtom("表单校验失败...缺少核心表单数据");
                    listLog2.AddToButtom("开始执行初始还原操作...");
                    skBtnRestored_Click("", new EventArgs());
                }
            }
            else
            {
                listLog2.AddToButtom("数据库完整性验证失败...不存在");
                listLog2.AddToButtom("开始执行初始还原操作...");
                skBtnRestored_Click("", new EventArgs());
            }
        }
コード例 #3
0
        //以下是一键还原数据库(通过最后一个参数指定是完整还原还是差异还原,完整还原,第一个参数只要一个文件名,差异还原则要两个)------------------------------------
        /// <summary>
        /// 一键还原数据库
        /// </summary>
        /// <param name="backfile">一个或两个文件,完全备份和差异备份</param>
        /// <param name="userinfo"></param>
        /// <param name="listbox"></param>
        /// <param name="type">是完整还原还是差异还原</param>
        /// <returns></returns>
        public static bool FullRestoreDatabase(string[] backfile, UserInfo userinfo, SkinListBox listbox, BacOrResType type)
        {
            ///杀死原来所有的数据库连接进程
            ArrayList    list = new ArrayList();
            string       sql  = "SELECT spid FROM sysprocesses ,sysdatabases WHERE sysprocesses.dbid=sysdatabases.dbid AND sysdatabases.Name=@baseName";
            SqlParameter spm  = new SqlParameter("@baseName", userinfo.BaseName);

            using (SqlDataReader reader = SQLHelper.ExcuteDataReader(sql, CommandType.Text, spm))
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        list.Add(reader.GetInt16(0));
                    }
                }
            }
            for (int i = 0; i < list.Count; i++)
            {
                sql = string.Format("KILL {0}", list[i].ToString());
                SQLHelper.ExecutNonQuery(sql, CommandType.Text, null);
                //MessageBox.Show("系统已经清除的数据库线程: " + list[i].ToString() + "\r\n正在还原数据库!");
                listbox.AddToButtom("清除数据库线程: " + list[i].ToString());
            }
            listbox.AddToButtom("所有线程清理完成...OK");
            //listbox.AddToButtom("开始执行完整还原操作...");

            listbox.AddToButtom(type == BacOrResType.Full ? "开始完整还原数据库..." : "开始差异还原数据库...");
            WaitThreadControl wtc = new WaitThreadControl {
                RecordedDatabase = false
            };

            #region 通过斜线旋转执行等待
            //Thread waitThread = new Thread((canStop) =>
            //    {
            //        string[] strArray = new string[] { "/", "—", "\\" };
            //        int i = 0;
            //        WaitDialog wd = new WaitDialog();
            //        wd.StartPosition = FormStartPosition.CenterParent;
            //        listbox.Invoke(new Action<WaitDialog>(wd1 =>
            //        {
            //            ((WaitDialog)wd1).ShowDialog();
            //        }), wd);
            //        while (!((WaitThreadControl)canStop).RecordedDatabase)
            //        {
            //            int b;
            //            Math.DivRem(i, 3, out b);
            //            listbox.Invoke(new Action(() =>
            //            {
            //                listbox.Items.RemoveAt(listbox.Items.Count - 1);
            //                listbox.AddToButtom("正在还原数据库,禁止所有操作..." + strArray[b]);
            //            }));
            //            i++;
            //            Thread.Sleep(300);
            //        }

            //        listbox.Invoke(new Action<string>(u =>
            //        {
            //            listbox.Items.RemoveAt(listbox.Items.Count - 1);
            //            listbox.AddToButtom(u);
            //        }), "还原数据库执行完成...OK");
            //    });
            #endregion
            Thread thread = new Thread((q) =>
            {
                bool hasBase = ConnectionTestInfo.CheckExistsDataBase(userinfo);
                //数据库已经存在时,不需要加move关键字,但需要覆盖.
                //数据库不存在时,需要加move,但不需要覆盖
                string movPath     = hasBase ? "" : @"MOVE @mdfFileName TO @mdfFilePath ,  MOVE @logFileName TO @logFilePath ,";//数据库是否存在
                string defaultPath = string.Empty;
                if (!hasBase)
                {
                    #region 获取数据库默认路径代码
                    listbox.Invoke(new Action <string>(n => { listbox.AddToButtom(n); }), "获取系统默认路径...");//.AddToButtom("获取系统默认路径...");
                    sql = "select filename from master..sysfiles";
                    using (SqlDataReader reader = SQLHelper.ExcuteDataReader(sql, CommandType.Text))
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                defaultPath = Path.GetDirectoryName(reader.GetString(0));
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(defaultPath))
                    {
                        listbox.Invoke(new Action <string>(n => { listbox.AddToButtom(n); }), "获取系统默认路径失败,需指定数据库存放路径...");
                        //listbox.AddToButtom("获取系统默认路径失败,需指定数据库存放路径");
                        //todo:弹出对话框
                    }
                    else
                    {
                        listbox.Invoke(new Action <string, string>((n, m) => { listbox.AddToButtom(n); listbox.AddToButtom(m); }), "获取默认路径成功...OK", defaultPath);
                        //listbox.AddToButtom("获取默认路径成功");
                        //listbox.Invoke(new Action<string>(n => { listbox.AddToButtom(n); }), defaultPath);
                        //listbox.AddToButtom(defaultPath);
                    }
                    #endregion
                }
                if (type == BacOrResType.Full)
                {
                    sql = @"RESTORE DATABASE @baseName FROM  DISK = @backFileName WITH  FILE = 1, " + movPath + " NOUNLOAD, REPLACE, STATS = 10";//NORECOVERY,
                }
                else
                {
                    sql = "RESTORE DATABASE @baseName FROM  DISK = @backFileName WITH  FILE = 1, " + movPath + "  NORECOVERY, REPLACE,  NOUNLOAD,  STATS = 10";
                }
                SqlParameter[] spms = new SqlParameter[]
                {
                    new SqlParameter("@baseName", userinfo.BaseName),
                    new SqlParameter("@backFileName", backfile[0]),
                    new SqlParameter("@mdfFileName", userinfo.BaseName),
                    new SqlParameter("@mdfFilePath", defaultPath + "\\" + userinfo.BaseName + ".mdf"),
                    new SqlParameter("@logFileName", userinfo.BaseName + "_log"),
                    new SqlParameter("@logFilePath", defaultPath + "\\" + userinfo.BaseName + ".ldf")
                };
                SQLHelper.ExecutNonQuery(sql, CommandType.Text, true, spms);
                //listbox.Invoke(new Action<string> ( u => listbox.AddToButtom(u) ),"执行完成");
                if (type == BacOrResType.Def)
                {
                    spms[1] = new SqlParameter("@backFileName", backfile[1]);
                    sql     = @"RESTORE DATABASE @baseName FROM  DISK = @backFileName WITH  FILE = 1,  NOUNLOAD,  STATS = 10";
                    SQLHelper.ExecutNonQuery(sql, CommandType.Text, true, spms);
                }
                ((WaitThreadControl)q).RecordedDatabase = true;
                listbox.Invoke(new Action <string>(n => listbox.AddToButtom(n)), "还原完成...OK");
            });
            thread.IsBackground = true;
            thread.Start(wtc);//要先执行线程再谈对话框,不然会阻塞.
            WaitDialog wd = new WaitDialog(wtc);
            wd.chengeLoadingText("正在还原数据库");
            wd.StartPosition = FormStartPosition.CenterParent;
            wd.ShowDialog();
            //waitThread.IsBackground = true;
            //waitThread.Start(wtc);

            return(true);

            #region 原来的代码
            //这里一定要是master数据库,而不能是要还原的数据库,因为这样便变成了有其它进程
            //占用了数据库。

            //string constr = userinfo.ConnectionString;
            //string database = userinfo.BaseName;//MyDataBase;
            //string path = backfile;
            //string BACKUP = String.Format("RESTORE DATABASE {0} FROM DISK = '{1}'", database, path);
            //SqlConnection con = new SqlConnection(constr);
            //SqlCommand cmd = new SqlCommand(BACKUP, con);
            //con.Open();
            //try
            //{
            //    cmd.ExecuteNonQuery();
            //    MessageBox.Show("还原成功,点击退出系统!");
            //    Application.Exit();
            //}
            //catch (SqlException ee)
            //{
            //    //throw(ee);

            //    //MessageBox.Show("还原失败");

            //    MessageBox.Show(ee.ToString());

            //}
            //finally
            //{
            //    con.Close();
            //}
            //return "成功与否字符串";
            #endregion
        }