// *********************************************************************** // Assembly : Construction // Author : flythink // Created : 06-28-2020 // // Last Modified By : flythink // Last Modified On : 09-01-2020 // *********************************************************************** // // Copyright (c) jindongfang. All rights reserved. // // // *********************************************************************** namespace WellWorkDataUI.CustomControls { using System; using System.Windows.Forms; using WorkData; /// /// Class FrmDBConnect. /// Implements the /// /// public partial class FrmDBConnect : Form { private string dbType = string.Empty; private FrmImportData frm = null; /// /// Initializes a new instance of the class. /// public FrmDBConnect() { this.InitializeComponent(); } /// /// Initializes a new instance of the class. /// /// The FRM. /// Type of the database. public FrmDBConnect(object frm, string dbType) { this.InitializeComponent(); this.frm = (FrmImportData)frm; this.dbType = dbType; this.txtPwd.Properties.UseSystemPasswordChar = true; if (dbType == "sqlserver") { this.txtIp.Text = this.frm.IpSqlServer; this.txtDb.Text = this.frm.DbSqlServer; this.txtUid.Text = this.frm.UidSqlServer; this.txtPwd.Text = this.frm.PwdSqlServer; } else if (dbType == "oracle") { this.txtIp.Text = this.frm.IpOracle; this.txtDb.Text = this.frm.DbOracle; this.txtUid.Text = this.frm.UidOracle; this.txtPwd.Text = this.frm.PwdOracle; } } private void BtnConnect_Click(object sender, EventArgs e) { if (this.Check()) { MessageBox.Show("连接成功", "提示"); } else { MessageBox.Show("连接失败", "提示"); } } private void BtnOk_Click(object sender, EventArgs e) { if (!this.Check()) { MessageBox.Show("连接失败", "提示"); return; } this.DialogResult = DialogResult.OK; } private bool Check() { Server2008 dB_SQL = null; Oracle10g dbOracle = null; try { string ip = this.txtIp.Text.Trim(); string db = this.txtDb.Text.Trim(); string uid = this.txtUid.Text.Trim(); string pwd = this.txtPwd.Text.Trim(); string constr = string.Empty; if (this.dbType == "sqlserver") { constr = "Data Source = " + ip + "; Initial Catalog = " + db + "; User ID = " + uid + "; Password = " + pwd + ";"; dB_SQL = new Server2008(constr); dB_SQL.Dispose(); this.frm.IpSqlServer = ip; this.frm.DbSqlServer = db; this.frm.UidSqlServer = uid; this.frm.PwdSqlServer = pwd; } else if (this.dbType == "oracle") { constr = "User ID=" + uid + ";Password=" + pwd + ";Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = " + ip + ")(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = " + db + ")))"; dbOracle = new Oracle10g(constr); dbOracle.Dispose(); this.frm.IpOracle = ip; this.frm.DbOracle = db; this.frm.UidOracle = uid; this.frm.PwdOracle = pwd; } this.frm.ConnectString = constr; return true; } catch (Exception e) { if (dB_SQL != null) { dB_SQL.Dispose(); } if (dbOracle != null) { dbOracle.Dispose(); } Console.WriteLine(e.ToString()); return false; } } } }