private void LoadData() { string DefNo = SystemInfo.ini.ReadIni("Public", "OprtNo", ""); DataTableReader dr = null; TOprtObject objOprt; string DBPath = ""; string DBName = SystemInfo.NameSpace; string sql = ""; cbbOpter.Items.Clear(); try { if (SystemInfo.DBType == 0) { if (!File.Exists(SystemInfo.AccessDB)) { Pub.ShowMessageForm(Pub.GetResText("", "MsgCreating", "")); SystemInfo.objAC.CreateDatabase(); SystemInfo.db.Open(SystemInfo.ConnStr); SystemInfo.db.UpdateDatabase(SystemInfo.AppTitle, new DateTime()); } } else if (SystemInfo.DBType == 1 || SystemInfo.DBType == 2) { if (SystemInfo.db.IsOpen) { SystemInfo.db.Close(); } SystemInfo.db.Open(Pub.GetMSSQLConnStr(DBServerInfo.ServerName, DBServerInfo.WindowsNT, DBServerInfo.UserName, DBServerInfo.UserPass, "master")); //打开数据库 dr = SystemInfo.db.GetDataReader("SELECT * FROM sysdatabases WHERE name='" + SystemInfo.NameSpace + "'"); //查询数据库信息 if (!dr.Read()) { Pub.ShowMessageForm(Pub.GetResText("", "MsgCreating", "")); DBPath = SystemInfo.db.GetDatabasePath().ToString(); string mdfFile = DBPath + DBName + ".mdf"; string ldfFile = DBPath + DBName + ".ldf"; if (File.Exists(mdfFile) && File.Exists(ldfFile)) { sql = "EXEC sp_attach_db '" + DBName + "','" + mdfFile + "','" + ldfFile + "'"; //将数据库附加到服务器 } else if (File.Exists(mdfFile)) { sql = "EXEC sp_attach_single_file_db '" + DBName + "','" + mdfFile + "'"; //恢复数据库 } else { sql = "CREATE DATABASE " + DBName + " ON(NAME='" + DBName + "_Data', FILENAME='" + mdfFile + "') LOG ON(NAME='" + DBName + "_Log',FILENAME='" + ldfFile + "')"; //创建数据库 } SystemInfo.db.ExecSQL(sql); SystemInfo.db.Close(); SystemInfo.db.Open(SystemInfo.ConnStr); SystemInfo.db.UpdateDatabase(SystemInfo.AppTitle, new DateTime()); } dr.Close(); SystemInfo.db.Close(); } if (!SystemInfo.db.IsOpen) { SystemInfo.db.Open(SystemInfo.ConnStr); } CheckDBUpdate(); dr = SystemInfo.db.GetDataReader(Pub.GetSQL(DBCode.DB_000001, new string[] { "1" })); while (dr.Read()) { objOprt = new TOprtObject(); objOprt.Name = dr["OprtName"].ToString(); objOprt.Value = dr["OprtNo"].ToString(); objOprt.Text = objOprt.Value + "[" + objOprt.Name + "]"; cbbOpter.Items.Add(objOprt); if ((DefNo != "") && (objOprt.Value == DefNo)) { cbbOpter.SelectedIndex = cbbOpter.Items.Count - 1; } } dr.Close(); } catch (Exception E) { Pub.ShowErrorMsg(E); } finally { Pub.FreeMessageForm(); if (dr != null) { dr.Close(); } dr = null; } if ((cbbOpter.SelectedIndex == -1) && (cbbOpter.Items.Count > 0)) { cbbOpter.SelectedIndex = 0; } btnOk.Enabled = (cbbOpter.SelectedIndex >= 0); }
private void btnOk_Click(object sender, EventArgs e) { #region 检测是否安装Access驱动 //try //{ // if (SystemInfo.DBType == 0 || SystemInfo.DBType == 255) // { // DAO.DBEngine dbEngine = new DAO.DBEngine(); // } //} //catch (Exception ex) //{ // Pub.ShowErrorMsg(ex, Pub.GetResText("", "AccessEorror", "")); // Process process = new Process(); // process.StartInfo.FileName = SystemInfo.AppPath + "AccessDatabaseEngine.exe"; // process.StartInfo.UseShellExecute = false; // process.StartInfo.RedirectStandardOutput = false; // process.StartInfo.RedirectStandardInput = true; // process.StartInfo.CreateNoWindow = false; // process.Start(); // process.WaitForExit(); // process.Close(); //} #endregion if (!SystemInfo.db.CheckAppIsNewVer(true, DBAppVer)) { Pub.MessageBoxShow(string.Format(Pub.GetResText(formCode, "Error001", ""), DBAppVer)); return; } TOprtObject obj = (TOprtObject)cbbOpter.Items[cbbOpter.SelectedIndex]; string OprtNo = obj.Value; string OprtPWD = Pub.GetOprtEncrypt(txtPass.Text.Trim()); string PWD = OprtPWD; if (PWD == "") { PWD = Pub.GetOprtEncrypt("0"); } DataTableReader dr = null; bool IsOk = false; string Pass = ""; try { dr = SystemInfo.db.GetDataReader(Pub.GetSQL(DBCode.DB_000001, new string[] { "2", OprtNo })); dr.Read(); Pass = dr["OprtPWD"].ToString(); if (Pass != OprtPWD && Pass != PWD) { txtPass.Focus(); Pub.MessageBoxShow(Pub.GetResText(formCode, "Error003", "")); } else { IsOk = true; } if (IsOk) { OprtInfo.OprtNo = OprtNo; OprtInfo.OprtIsSys = (dr["OprtIsSys"].ToString() == "1"); OprtInfo.OprtNoAndName = cbbOpter.Text; SystemInfo.ini.WriteIni("Public", "OprtNo", OprtInfo.OprtNo); if (chkPass.Checked) { SystemInfo.ini.WriteIni("Public", "IsRePassword", chkPass.Checked); SystemInfo.ini.WriteIni("Public", "RePassword", OprtPWD); } else { SystemInfo.ini.WriteIni("Public", "IsRePassword", ""); SystemInfo.ini.WriteIni("Public", "RePassword", ""); } dr.Close(); SystemInfo.db.ExecSQL(Pub.GetSQL(DBCode.DB_000001, new string[] { "3", OprtNo })); dr = SystemInfo.db.GetDataReader(Pub.GetSQL(DBCode.DB_000001, new string[] { "4" })); if (dr.Read()) { SystemInfo.CommanyName = dr["DepartName"].ToString(); SystemInfo.CommanyID = dr["DepartID"].ToString(); } } } catch (Exception E) { IsOk = false; Pub.ShowErrorMsg(E); } finally { if (dr != null) { dr.Close(); } dr = null; } if (IsOk) { this.Close(); this.DialogResult = DialogResult.OK; } }