asp.net 版本一键升级,后台直接调用升级脚本

应客户需求,要求实现一个版本一键升级的功能,咨询过同事之后弄了个demo出来,后台代码如下:

  //DBConnModelInfo:连接字符串的对象 (包含数据库实例名,数据库名,登陆名,登陆密码)

  public bool DBVersionSaveData(DBConnModelInfo mdl)
        {
            bool result = false;

            try
            {
                int timeout = 60000; //设置执行超时时间,单位毫秒
                string prversion = WebConfig.Version;//程序版本
                string dbversion = new BSystemParameter().GetDBVersion();//数据库版本
                
                //升级脚本的物理路径
                string root = "DataBase\\Update";
                string folder = prversion;
                string rootpath = Path.Combine(root, folder);
                string dirpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, rootpath);

                string filepath = Path.Combine(dirpath, "Update.sql");//升级脚本
                string logpath = Path.Combine(dirpath, "Update.txt");//存放执行结果
                string destfilename = string.Format("Update_{0}.sql", mdl.InitialCatalog);//要执行升级的脚本(升级脚本的副本,不改变原升级脚本)
                string destfilepath = Path.Combine(dirpath, destfilename);


                if (System.IO.File.Exists(destfilepath))
                {
                    Tools.DeleteFile(@destfilepath);//删除文件
                }
                //将升级脚本的内容复制到副本
                if (!System.IO.File.Exists(destfilepath))
                {
                    using (StreamWriter sw = new StreamWriter(destfilepath, true, Encoding.Unicode))
                    {
                        sw.WriteLine(string.Format("Use [{0}]", mdl.InitialCatalog));//mdl.InitialCatalog 数据库名
                        sw.WriteLine("Go");

                        string[] rals = System.IO.File.ReadAllLines(@filepath);
                        foreach (string s in rals)
                        {
                            sw.WriteLine(s);
                        }
                    }
                }
                //执行升级脚本
                if (System.IO.File.Exists(destfilepath))
                {
                    Process p = new Process();
                    p.StartInfo.FileName = "osql.exe"; //执行脚本的方式
                    /* -S:数据库实例名  -U:登录名 -P:密码 -i:升级的脚本路径 -o:执行后结果存放路径  */
                    string args = string.Format(@"-S {0}  -U {1} -P {2} -i ""{3}"" -o ""{4}""", mdl.DataSource, mdl.UserID, mdl.Password, destfilepath, logpath);
                    p.StartInfo.Arguments = args;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();
                    p.WaitForExit(timeout);
                    p.Close();

                    result = true;
                }                
            }
            catch (Exception ex)
            {
                Tools.Log(this.GetType().Name, new StackFrame(1).GetMethod().Name, ex);//错误日志
                result = false;
            }
            
            return result;
        }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyzzfw.html