//private DeserializeDockContent m_deserializeDockContent; //private bool m_bSaveLayout = true; #endregion #region 保存菜单 private void SetMenuAll(ToolStripItemCollection items) { KJ128NDataBase.DBAcess dba = new KJ128NDataBase.DBAcess(); foreach (ToolStripItem tsi in items) { if (tsi is ToolStripMenuItem) { //ToolStrip TS = tsi.Owner; string str = string.Format("insert into menus1(PMenuID,Title,name) values({2},'{0}','{1}')", ((ToolStripMenuItem)tsi).Text.Substring(0, ((ToolStripMenuItem)tsi).Text.Length - 4), ((ToolStripMenuItem)tsi).Name , ((ToolStripMenuItem)tsi).Owner.Name == "msMainMenu" ? "-1" : dba.ExecuteScalarSql("select id from menus1 where name ='" + ((ToolStripMenuItem)tsi).OwnerItem.Name + "'")); dba.ExecuteSql(str); System.IO.File.AppendAllText("c:/sql.txt", str + "\r\n", Encoding.Default); if (((ToolStripMenuItem)tsi).DropDownItems.Count > 0) { SetMenuAll(((ToolStripMenuItem)tsi).DropDownItems); } } } }
public Li_RealTimeInOutMine_BLL() { dbacc = new DBAcess(); }
/// <summary> /// 备份指定数据库文件 /// </summary> /// <param name="strDbName">数据库名称</param> /// <param name="strFileName">备份数据库的路径</param> /// <param name="strServerName">服务器名称</param> /// <param name="strUserName">用户名</param> /// <param name="strPassword">密码</param> /// <param name="prosBar">进度条</param> /// <returns></returns> public static bool BackUPDB(string strDbName, string strFileName, string strServerName, string strUserName, string strPassword, ProgressBar bar) { #region [ 判断磁盘剩余空间是否可进行备份 ] DBAcess dba = new DBAcess(); DataSet tmpds = dba.GetDataSet("exec sp_spaceused"); if (tmpds.Tables.Count > 0 && tmpds.Tables[0] != null) { // 得到数据库大小 string tmpStr = tmpds.Tables[0].Rows[0]["database_size"].ToString(); int dbSize = Convert.ToInt32(tmpStr.Substring(0, tmpStr.LastIndexOf("."))); // 获取磁盘剩余空间大小 try { DriveInfo d = new DriveInfo(strFileName.Substring(0, 1)); int diskSize = Convert.ToInt32(d.AvailableFreeSpace / 1024 / 1024); if (diskSize < dbSize) { MessageBox.Show("备份数据库所需空间不能小于" + dbSize + "M\r\n磁盘[" + d.Name + "]存储空间过小,无法备份"); return false; } } catch (Exception) { MessageBox.Show("路径不正确"); return false; //throw; } } #endregion pBar = bar; string strTmp = ""; string tmpPath = strFileName.Substring(0, strFileName.LastIndexOf("\\")).ToString(); int isEmpty = tmpPath.IndexOf(" "); SQLDMO.SQLServer svr = null; try { svr = new SQLDMO.SQLServerClass(); // 连接到数据库 svr.Connect(strServerName, strUserName, strPassword); SQLDMO.Backup bak = new SQLDMO.BackupClass(); bak.Action = 0; bak.Initialize = true; #region 进度条处理 if (pBar != null) { pBar.Visible = true; SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(Step); bak.PercentComplete += pceh; } #endregion #region [ 文件夹名称中有空格: 备份前的处理 ] // 文件夹不存在时自动创建 if (!Directory.Exists(tmpPath)) { Directory.CreateDirectory(tmpPath); } // 文件夹名称 中有空格 备份文件路径设置为根目录的临时文件夹tmpBackup中 if (isEmpty > 1 && strFileName.Substring(4).LastIndexOf("\\") > 1) { strTmp = strFileName.Substring(0, 1).ToString() + ":\\tmp_backup.kj"; } else { strTmp = strFileName; } #endregion // 数据库的备份的名称及文件存放位置 bak.Files = strTmp; bak.Database = strDbName; // 备份 bak.SQLBackup(svr); } catch (Exception err) { if (SqlErrRepair(err.Message.ToString())) { BackUPDB(strDbName, strFileName, strServerName, strUserName, strPassword, pBar); return true; } return false; //MessageBox.Show("备份数据库失败"); } finally { if (svr != null) { svr.DisConnect(); } #region [ 文件夹名称中有空格: 备份完成后的处理 ] // 文件夹名称 中有空格 将备份的文件移动到用户指定的文件夹并将临时目录删除 if (isEmpty > 1 && strFileName.Substring(4).LastIndexOf("\\") > 1) { // 文件存在则替换 if (File.Exists(strFileName.Substring(strFileName.LastIndexOf("\\") + 2))) { File.Delete(strFileName.Substring(strFileName.LastIndexOf("\\") + 2)); } File.Move(strTmp, strFileName); } #endregion } return true; }
public static void ShrinkDataBase() { DBAcess dba = new DBAcess(); try { // 数据库名从连接字符串中得到 string dbName = ConfigurationSettings.AppSettings["ConnectionString"];//dba.ConnectionStringKJ128N; dbName = dbName.Substring(dbName.IndexOf("database=") + 9); dbName = dbName.Substring(0, dbName.IndexOf(";")); dba.GetDataSet("DUMP TRANSACTION " + dbName + " WITH NO_LOG"); MessageBox.Show("清空日志成功"); } catch (Exception ex) { MessageBox.Show("清空日志失败"); } }
static void Main() { #region[程序启动代码] try { #region[系统启用应用程序样式] Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); #endregion #region [ 删除 .dw 后缀的文件] if (File.Exists(Application.StartupPath + "\\Conn.dw")) { File.Delete(Application.StartupPath + "\\Conn.dw"); } #endregion #region [ 防止程序被多次打开 ] int iCount = 0; foreach (Process process in Process.GetProcesses()) { if (process.ProcessName.Equals("KJ128NMainRun")) { iCount++; } } //Czlt-2011-01-25 删除打开多个程序控制 //qyz-2011-11-27 加入多开限制 if (iCount > 1) { MessageBox.Show("已经有一个KJ128A桌面程序在运行,请勿重复打开!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } #endregion #region [ 打开欢迎画面 ] FrmWelcome frm = new FrmWelcome(); frm.Show(); Thread.Sleep(100); #endregion #region 【实例化委托】 CurrentStateDelegate czltCurrentState = new CurrentStateDelegate(frm.CurrentState); #endregion #region[判断是否是客户端] bool isClient = false; try { string path = Application.StartupPath + @"\IsClient.xml"; if (File.Exists(path)) { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode node = doc.ChildNodes[1].SelectSingleNode("IsClient"); if (node != null) { if (node.InnerText.ToLower().Equals("true")) { isClient = true; } } } } catch { } #endregion #region [ 检测软件狗 ] DialogResult result; #if DEBUG #else if (!isClient) { //frm.CurrentState = "正在检测软件狗"; czltCurrentState("正在检测软件狗"); Thread.Sleep(200); SoftDogJudge sdj = new SoftDogJudge(); string strSoftDog = sdj.Judge(ShowType.WinForm, "KJ128", "128"); if (!strSoftDog.Equals("")) { result = MessageBox.Show(strSoftDog, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); if (result == DialogResult.OK) { //return; } } } #endif #endregion #region [ 打开热备通讯程序 ] #if DEBUG #else if (!isClient) { //frm.CurrentState = "正在打开热备通讯程序"; czltCurrentState("正在打开热备通讯程序"); Thread.Sleep(100); bool flag = false; foreach (Process process in Process.GetProcesses()) { if (process.ProcessName.Equals("KJ128A.Batman")) { flag = true; } } if (!flag) { if (File.Exists(Application.StartupPath + "\\KJ128A.Batman.exe")) { string strPath = @"KJ128A.Batman.exe"; Process TongXun = new Process(); TongXun.StartInfo.FileName = Application.StartupPath + @"\" + strPath; TongXun.Start(); } } } #endif #endregion #region [ 检测数据库 ] //frm.CurrentState = "正在检测连接数据库"; czltCurrentState("正在检测连接数据库"); Thread.Sleep(100); DBAcess dbacc = new DBAcess(); DbHelperSQL dbsql = new DbHelperSQL(); if (dbacc.CreateConnection() == -1) { result = MessageBox.Show("数据库未连接,可能是数据库没有安装或数据库没有打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); if (result == DialogResult.OK) { return; } } ////取消数据库收缩 //if (!isClient) //{ // new KJ128A.DataSave.DataBaseManage().ZipDataBase(); //} #endregion #region [ 打开主界面 ] //frm.CurrentState = "请稍等,正在打开主程序......"; czltCurrentState("请稍等,正在打开主程序......"); Application.Run(new A_FrmMain()); //A_FrmToolOptions KJ128NMainRun.A_FrmMain #endregion } catch (SqlException ex) { string strErr = "在向服务器发送请求时发生传输级错误。 (provider: TCP 提供程序, error: 0 - 远程主机强迫关闭了一个现有的连接。)"; if (ex.Message.Equals(strErr)) { MessageBox.Show("数据库连接失败,请重新打开应用程序!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); Process pr = Process.GetCurrentProcess(); if (pr.ProcessName.Equals("KJ128NMainRun")) { pr.Kill(); } } } #endregion }