/// <summary> /// 新增数据 /// </summary> /// <param name="dt"></param> /// <returns></returns> public bool InsertDASoftversionList(DASoftversion softversion) { int nflag = 0; try { service.insert("InsertDASoftversion", softversion); nflag = 1; } catch (Exception ex) { nflag = 0; throw new Exception(ex.Message); } return(nflag > 0 ? true : false); }
private void CheckVersion() { double oldVersion; double newVersion; //是否不存在表LastSoftVersion bool isExist = true; try { //读取数据库获取旧版本号,防止并发,在获取最新版本时锁定Soft_Version表 DASoftversion oObj = new DASoftversion(); try { oObj = dasoftversionbll.GetLastSoftVersion(); } catch { isExist = false; if (ShowMessageHelper.ShowBoxMsg("当前连接的数据库表不健全,是否初始化数据库?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } } if (oObj != null) { oldVersion = Convert.ToDouble(oObj.Versioncode); } else { oldVersion = 0.0; } //读取list.txt获取最新版本号 string filePath = Application.StartupPath + @"\edition.txt"; if (!File.Exists(filePath)) { ShowMessageHelper.ShowBoxMsg("检测数据库版本时发生错误,找不到版本文件!"); return; } FileStream fs = new FileStream(Application.StartupPath + @"\edition.txt", FileMode.Open, FileAccess.Read); StreamReader m_streamReader = new StreamReader(fs); //使用StreamReader类来读取文件 newVersion = Convert.ToDouble(m_streamReader.ReadLine()); if (newVersion > oldVersion) { if (isExist && ShowMessageHelper.ShowBoxMsg("检查到新的数据库版本,是否执行更新?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } //读取数据库配置XML文件 DataBaseinfo baseinfo = XMLHelper.ReadDataBase(DefaultConfig.DATABASENAME); //版本更新,更新数据库脚本 common.ScriptUpdateByBat(baseinfo, oldVersion, newVersion); } else if (newVersion < oldVersion) { //版本回溯,删除最新版本信息 DeleteLastVersion(newVersion.ToString()); } else { //无版本操作 } } catch { ShowMessageHelper.ShowBoxMsg("检测数据库版本时发生错误,请检查相关配置!"); } }
//初始化表结构 private void btnTableStruc_Click(object sender, EventArgs e) { if (DialogResult.No == ShowMessageHelper.ShowBoxMsg("是否要初始化当前连接下的表结构?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { return; } //验证 if (!CheckInput()) { return; } //保存连接 if (!SaveConfig()) { ShowMessageHelper.ShowBoxMsg("当前连接保存失败,请检查当前输入连接!"); return; } //读取当前版本 double oldVersion; DASoftversionBLL dasoftversionbll = new DASoftversionBLL(); DASoftversion oObj = new DASoftversion(); try { oObj = dasoftversionbll.GetLastSoftVersion(); } catch { } if (oObj != null) { oldVersion = Convert.ToDouble(oObj.Versioncode); } else { oldVersion = 0.0; } if (oldVersion >= 1.0) { ShowMessageHelper.ShowBoxMsg("当前连接下的数据表已经健全!"); return; } DataBaseinfo info = new DataBaseinfo(); if (radOracle.Checked == true) { info.databasetype = "0"; info.dataname = tbxSID.Text.ToString(); info.userid = tbxOracleUserName.Text.ToString(); info.password = tbxOraclePwd.Text.ToString(); info.datasource = tbxOracleService.Text.ToString(); } else { info.databasetype = "1"; info.dataname = tbxDatabase.Text.ToString(); info.userid = tbxSQLUserName.Text.ToString(); info.password = tbxSQLPwd.Text.ToString(); info.datasource = tbxSQLService.Text.ToString(); } if (common.ScriptUpdateByBat(info, 0, 1.0))//只读取1.0的脚本 { ShowMessageHelper.ShowBoxMsg("初始化表结构成功!"); } else { ShowMessageHelper.ShowBoxMsg("初始化表结构失败!"); } }
private ArrayList fileList = new ArrayList(); //批处理文件集合 /// <summary> /// 更新数据库脚本(通过执行bat文件) /// </summary> /// <param name="version">版本号</param> public bool ScriptUpdateByBat(DataBaseinfo baseinfo, double version, double newVersion) { bool istrue = false; string fileName = string.Empty; try { if (baseinfo.databasetype == "1")//SQL { fileName = "\\VersionSql\\SQL"; } else//ORACEL { fileName = "\\VersionSql\\ORA"; } string[] fileNames = Directory.GetFiles(Application.StartupPath + fileName); ArrayList versionList = new ArrayList(); foreach (string fName in fileNames) { string f = fName.Substring(0, fName.Length - 4); f = f.Substring(f.LastIndexOf("\\") + 1, f.Length - f.LastIndexOf("\\") - 1); if (version < Convert.ToDouble(f) && Convert.ToDouble(f) <= newVersion) { versionList.Add(Convert.ToDouble(f)); } } //排序 versionList.Sort(); for (int i = 0; i < versionList.Count; i++) { //生成Bat文件(格式) //OSQL /U sa /P sa -S 192.168.0.153 -d NorthWind -r -i b.sql -o a.txt //Pause if (!File.Exists(Application.StartupPath + "\\ExeScriptLog.txt")) { //创建Log文件 FileStream s = File.Create(Application.StartupPath + "\\ExeScriptLog.txt"); s.Close(); } FileInfo fBat = new FileInfo(Application.StartupPath + "\\ExeScript" + i + ".bat"); //创建Bat文件 using (StreamWriter sw = fBat.CreateText()) { if (baseinfo.databasetype == "1")//SQL { sw.WriteLine(string.Format("OSQL /U {2} /P {3} -S {0} -d {1} -r -i VersionSql\\SQL\\{4}.sql -o ExeScriptLog.txt", baseinfo.datasource, baseinfo.dataname, baseinfo.userid, baseinfo.password, versionList[i].ToString().Length < 2 ? versionList[i].ToString() + ".0" : versionList[i].ToString())); sw.WriteLine("Pause"); } else//ORLCLE { sw.WriteLine(string.Format("sqlplus {0}/{1}@{2} @VersionSql\\ORA\\{3}.sql >> ExeScriptLog.txt", baseinfo.userid, baseinfo.password, baseinfo.dataname, versionList[i].ToString().Length < 2 ? versionList[i].ToString() + ".0" : versionList[i].ToString())); sw.WriteLine("Pause"); } } //启动bat Process process = new Process(); process.StartInfo.WorkingDirectory = Application.StartupPath; process.StartInfo.FileName = "ExeScript" + i + ".bat"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardError = true; //开启出错返回信息 process.StartInfo.RedirectStandardOutput = true; //开启输出返回信息 process.Start(); string strOUT = process.StandardOutput.ReadToEnd(); //用于捕捉返回信息。 string strERR = process.StandardError.ReadToEnd(); //用于捕捉异常信息。 fileList.Add(process.StartInfo.FileName); //更新版本信息 DASoftversion sv = new DASoftversion(); sv.Versioncode = Convert.ToDecimal(versionList[i]); sv.Exceptionlog = strERR == "" ? "执行成功" : strERR; sv.Updatedate = DateTime.Now; new DASoftversionBLL().InsertDASoftversionList(sv); } if (versionList.Count == 0)//缺失脚本文件 { ShowMessageHelper.ShowBoxMsg("缺失脚本文件,请联系管理员!", MessageBoxButtons.OK); } else//执行脚本成功 { istrue = true; } //启动Bat文件删除线程 Thread t = new Thread(new ThreadStart(DeleteBat)); t.Start(); } catch { istrue = false; } return(istrue); }