public void InsertSource(PMSRefDBConnectionObj source, bool bRecord)
        {
            InsertSource(source);
            if (bRecord)
            {
                string             name         = source.StrName;
                string             description  = source.StrDescription;
                PMSRefDBConnection rc           = source.RefDBConnection;
                s_DBSourceDefine   sourceDefine = new s_DBSourceDefine();
                sourceDefine.Name          = name;
                sourceDefine.Description   = description;
                sourceDefine.DBType        = (int)(rc.RefDBType);
                sourceDefine.DBServer      = rc.StrServerName;
                sourceDefine.DBName        = rc.StrDBName;
                sourceDefine.UserID        = rc.StrUserID;
                sourceDefine.Password      = rc.StrPassWord;
                sourceDefine.Protocol      = rc.EConnectType.ToString();
                sourceDefine.PortID        = rc.StrPortID;
                sourceDefine.ConnectAs     = rc.StrConnectAs;
                sourceDefine.Direct        = rc.BDirect;
                sourceDefine.Path          = rc.StrDBPath;
                sourceDefine.ConnectString = rc.ConnectString;

                PMSDBStructure.PMSCenterDataContext.AddTos_DBSourceDefine(sourceDefine);
                PMSDBStructure.PMSCenterDataContext.SaveChanges();
            }
        }
Exemple #2
0
        public void InsertSource(PMSRefDBConnectionObj source)
        {
            string             name        = source.StrName;
            string             description = source.StrDescription;
            PMSRefDBConnection rc          = source.RefDBConnection;
            ListViewGroup      group       = null;

            switch (rc.RefDBType)
            {
            case RefDBType.MSAccess: group = AddGroup(RefDBType.MSAccess); break;

            case RefDBType.MSSqlServer: group = AddGroup(RefDBType.MSSqlServer); break;

            case RefDBType.Oracle: group = AddGroup(RefDBType.Oracle); break;

            case RefDBType.OleDB: group = AddGroup(RefDBType.OleDB); break;

            default:
                break;
            }
            this.listView1.BeginUpdate();
            ListViewItem lvitem = new ListViewItem(name, group);

            lvitem.SubItems.Add(description);
            lvitem.Tag = rc;
            if (source.BDefault == true)
            {
                lvitem.ForeColor = Color.Purple;
            }
            this.listView1.Items.Add(lvitem);
            this.listView1.EndUpdate();
        }
 private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
 {
     if (e.Button == MouseButtons.Left)
     {
         if (listView1.SelectedItems.Count == 0)
         {
             return;
         }
         PMSRefDBConnection refdbc = listView1.SelectedItems [0].Tag as PMSRefDBConnection;
         using (DBRefStructForm dbrf = new DBRefStructForm()) {
             dbrf.RefDBConnection = refdbc;
             if (dbrf.ShowDialog() == DialogResult.OK)
             {
                 listView1.SelectedItems [0].Tag = dbrf.RefDBConnection;
                 //SetSelectObj(listView1.SelectedItems[0].Tag);
                 SetModified();
             }
         }
     }
 }
        public void InitRefDBConnection(PMSRefDBConnection refdbc)
        {
            if (refdbc.RefDBType != RefDBType.NULL)
            {
                this.tabControl1.SelectedIndex = (int)(refdbc.RefDBType);
            }

            if (refdbc.RefDBType == RefDBType.MSAccess)
            {
                textBox_MSAccessPath.Text = refdbc.StrDBPath;
                textBox_MSAccessUID.Text  = refdbc.StrUserID;
                textBox_MSAccessPass.Text = refdbc.StrPassWord;
            }
            else if (refdbc.RefDBType == RefDBType.MSSqlServer)
            {
                comboBox_Server.Text            = refdbc.StrServerName;
                comboBox_DB.Text                = refdbc.StrDBName;
                textBox_UserName.Text           = refdbc.StrUserID;
                textBox_Password.Text           = refdbc.StrPassWord;
                comboBox_Protocol.SelectedIndex = (int)(refdbc.EConnectType) - 1;
                textBox_PortID.Text             = refdbc.StrPortID;
            }
            else if (refdbc.RefDBType == RefDBType.Oracle)
            {
                comboBox_OracleServer.Text  = refdbc.StrServerName;
                textBox_OracleUserID.Text   = refdbc.StrUserID;
                textBox_OracleUserPass.Text = refdbc.StrPassWord;
                comboBox_ConnectAs.Text     = refdbc.StrConnectAs;
                comboBox_ConnectMode.Text   = refdbc.StrConnectMode;
                //comboBox_OracleHome.Text = refdbc.;
                comboBox_SID.Text            = refdbc.StrDBName;
                textBox_OracleProtID.Text    = refdbc.StrPortID;
                textBox_Owner.Text           = refdbc.Owner;
                this.checkBox_Direct.Checked = refdbc.BDirect;
            }
            else if (refdbc.RefDBType == RefDBType.OleDB)
            {
                this.comboBox_ConnectString.Text = refdbc.ConnectString;
            }
        }
        public bool Save()
        {
            try {
                if (this.IsModified)
                {
                    List <PMSRefDBConnectionObj> obl = new List <PMSRefDBConnectionObj> ();
                    foreach (ListViewItem item in listView1.Items)
                    {
                        PMSRefDBConnection    rc = (PMSRefDBConnection)(item.Tag);
                        PMSRefDBConnectionObj ob = new PMSRefDBConnectionObj();
                        ob.StrName        = item.SubItems [0].Text;
                        ob.StrDescription = item.SubItems [1].Text;
                        if (item.ForeColor == Color.Purple)
                        {
                            ob.BDefault = true;
                        }
                        ob.RefDBConnection = rc;
                        obl.Add(ob);
                    }
                    if (null != _DBDefineManager)
                    {
                        _DBDefineManager.CloseAllDBConnects();
                        _DBDefineManager.DBSourceConfigObjList = obl;
                    }
                    PMS.Libraries.ToolControls.PMSPublicInfo.CurrentPrjInfo.SaveRefDBConnectionObjList(obl);
                    // MES开发器环境 中 应保存数据源定义文件至文件服务器
                    //if (CurrentPrjInfo.CurrentEnvironment == MESEnvironment.MESDeveloper)
                    //    RaiseSomeEvent(this, new SaveCompleteEventArgs(ProjectPathClass.RefDBSourcesFilePath));
                    SetSaved();
                }
            } catch (Exception ex) {
                PMS.Libraries.ToolControls.PMSPublicInfo.Message.Error(PMS.Libraries.ToolControls.PMSPublicInfo.PublicFunctionClass.EnhancedStackTrace(ex));
                return(false);
            }

            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns>
        /// 0 -- 数据库连接失败
        /// 1 -- 用户登录失败
        /// 2 -- 连接成功
        /// </returns>
        private int ConnectDB()
        {
            this.button_OK.Enabled = false;
            bConnected             = false;
            if (_SelectDBType == RefDBType.MSSqlServer)
            {
                string myServerName = comboBox_Server.Text.Trim();
                if (string.IsNullOrEmpty(myServerName))
                {
                    string strText    = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMSDBServerNULL");
                    string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                    MessageBox.Show(strText, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.button_OK.Enabled = true;
                    return(0);
                }
                string myDBName = comboBox_DB.Text.Trim();
                if (string.IsNullOrEmpty(myDBName))
                {
                    string strText    = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMSDBDBNameNULL");
                    string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                    MessageBox.Show(strText, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.button_OK.Enabled = true;
                    return(0);
                }
                string userId = textBox_UserName.Text.Trim();
                if (string.IsNullOrEmpty(userId))
                {
                    string strText    = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMSDBUserIDNULL");
                    string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                    MessageBox.Show(strText, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.button_OK.Enabled = true;
                    return(0);
                }
                string passWord = textBox_Password.Text.Trim();

                ConnectType iProtocolType = (ConnectType)(comboBox_Protocol.SelectedIndex + 1);

                string portID = textBox_PortID.Text.Trim();
                //string strConnect = "Data Source=" + myServerName +
                //        ";Initial Catalog=" + myDBName +
                //        ";User Id=" + userId +
                //        ";Password="******";";
                try
                {
                    _RefDBConnection               = new PMSRefDBConnection();
                    _RefDBConnection.RefDBType     = RefDBType.MSSqlServer;
                    _RefDBConnection.StrServerName = myServerName;
                    _RefDBConnection.StrDBName     = myDBName;
                    _RefDBConnection.StrUserID     = userId;
                    _RefDBConnection.StrPassWord   = passWord;
                    _RefDBConnection.EConnectType  = iProtocolType;
                    _RefDBConnection.StrPortID     = portID;
                    _conn = _RefDBConnection.GetSqlConnection();

                    //sqlStructure = new SqlStructure(myServerName, myDBName, userId, passWord, iProtocolType, portID);

                    //_conn = SqlStructure.GetSqlConncetion();
                    if (_conn == null)
                    {
                        bConnected             = false;
                        this.button_OK.Enabled = true;
                        return(0);
                    }
                    //if (!SqlStructure.ConnectSMOServer())
                    //{
                    //    bConnected = false;
                    //    this.button_OK.Enabled = true;
                    //    return 0;
                    //}

                    //SqlStructure.PMSCenterDataContext = new PMSCenterDataContext(_conn);
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                    bConnected             = false;
                    this.button_OK.Enabled = true;
                    return(0);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    bConnected             = false;
                    this.button_OK.Enabled = true;
                    return(0);
                }

                if (_conn.State == ConnectionState.Open)
                {
                    // 连接数据库成功
                    SaveSettings();
                    bConnected    = true;
                    strServerName = myServerName;
                    strDBName     = myDBName;
                    strUserID     = userId;
                    strPassWord   = passWord;

                    // 连接成功关闭
                    this.Close();
                    this.DialogResult = DialogResult.OK;
                    return(2);
                }
                else
                {
                    bConnected = false;
                    return(0);
                }
            }
            else if (_SelectDBType == RefDBType.Oracle)
            {
                string myServerName = comboBox_OracleServer.Text.Trim();
                if (string.IsNullOrEmpty(myServerName))
                {
                    string strText    = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMSDBServerNULL");
                    string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                    MessageBox.Show(strText, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.button_OK.Enabled = true;
                    return(0);
                }
                string userId = textBox_OracleUserID.Text.Trim();
                if (string.IsNullOrEmpty(userId))
                {
                    string strText    = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMSDBUserIDNULL");
                    string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                    MessageBox.Show(strText, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.button_OK.Enabled = true;
                    return(0);
                }
                string passWord          = textBox_OracleUserPass.Text.Trim();
                string OracleConnectAs   = comboBox_ConnectAs.Text.Trim();
                string OracleConnectMode = comboBox_ConnectMode.Text.Trim();
                string OracleHome        = comboBox_OracleHome.Text.Trim();
                string OracleSID         = string.Empty;
                string OraclePortID      = string.Empty;
                string OracleOwner       = textBox_Owner.Text.Trim();
                bool   OracleDirect      = checkBox_Direct.Checked;
                if (OracleDirect)
                {
                    OracleSID = comboBox_SID.Text.Trim();
                    if (string.IsNullOrEmpty(OracleSID))
                    {
                        string strText    = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMSDBOracleSIDNULL");
                        string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                        MessageBox.Show(strText, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.button_OK.Enabled = true;
                        return(0);
                    }
                    OraclePortID = textBox_OracleProtID.Text.Trim();
                    if (string.IsNullOrEmpty(OraclePortID))
                    {
                        string strText    = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMSDBOraclePortIDNULL");
                        string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                        MessageBox.Show(strText, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.button_OK.Enabled = true;
                        return(0);
                    }
                }

                try
                {
                    _RefDBConnection                = new PMSRefDBConnection();
                    _RefDBConnection.RefDBType      = RefDBType.Oracle;
                    _RefDBConnection.StrServerName  = myServerName;
                    _RefDBConnection.StrDBName      = OracleSID;
                    _RefDBConnection.StrUserID      = userId;
                    _RefDBConnection.StrPassWord    = passWord;
                    _RefDBConnection.BDirect        = OracleDirect;
                    _RefDBConnection.StrConnectAs   = OracleConnectAs;
                    _RefDBConnection.StrConnectMode = OracleConnectMode;
                    _RefDBConnection.StrPortID      = OraclePortID;
                    _RefDBConnection.Owner          = OracleOwner;
                    _conn = _RefDBConnection.GetOracleConnection();

                    if (_conn == null)
                    {
                        bConnected             = false;
                        this.button_OK.Enabled = true;
                        return(0);
                    }
                }
                catch (Oracle.DataAccess.Client.OracleException ex)
                {
                    MessageBox.Show(ex.ToString());
                    bConnected             = false;
                    this.button_OK.Enabled = true;
                    return(0);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    bConnected             = false;
                    this.button_OK.Enabled = true;
                    return(0);
                }

                if (_conn.State == ConnectionState.Open)
                {
                    // 连接数据库成功
                    SaveSettings();
                    bConnected    = true;
                    strServerName = myServerName;
                    strDBName     = OracleSID;
                    strUserID     = userId;
                    strPassWord   = passWord;

                    // 连接成功关闭
                    this.Close();
                    this.DialogResult = DialogResult.OK;
                    return(2);
                }
                else
                {
                    bConnected = false;
                    return(0);
                }
            }
            else if (_SelectDBType == RefDBType.MSAccess)
            {
                strMsAccessPath = textBox_MSAccessPath.Text.Trim();
                if (string.IsNullOrEmpty(strMsAccessPath))
                {
                    string strText    = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMSDBAccessPathNULL");
                    string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                    MessageBox.Show(strText, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.button_OK.Enabled = true;
                    return(0);
                }

                string userId   = textBox_MSAccessUID.Text.Trim();
                string passWord = textBox_MSAccessPass.Text.Trim();

                try
                {
                    _RefDBConnection           = new PMSRefDBConnection();
                    _RefDBConnection.RefDBType = RefDBType.MSAccess;
                    _RefDBConnection.StrDBPath = strMsAccessPath;
                    _OleDbConn = _RefDBConnection.GetOleConnection();
                    // 利用 OleDbConnectionStringBuilder 对象来构建
                    // 连接字符串。
                    //acsStructure = new AccessStructure(strMsAccessPath, userId, passWord);
                    //if (AccessStructure.ConnectDB())
                    //    _OleDbConn = AccessStructure.OleDbConn;
                    if (_OleDbConn == null)
                    {
                        string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                        MessageBox.Show(_RefDBConnection.GetLastError(), strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        bConnected             = false;
                        this.button_OK.Enabled = true;
                        return(0);
                    }
                }
                catch (System.Data.OleDb.OleDbException ex)
                {
                    MessageBox.Show(ex.ToString());
                    bConnected             = false;
                    this.button_OK.Enabled = true;
                    return(0);
                }

                if (_OleDbConn.State == ConnectionState.Open)
                {
                    // 连接数据库成功
                    SaveSettings();
                    bConnected = true;

                    return(2);
                }
                else
                {
                    bConnected = false;
                    return(0);
                }
            }
            else if (_SelectDBType == RefDBType.OleDB)
            {
                ConnectionString = comboBox_ConnectString.Text.Trim();
                if (string.IsNullOrEmpty(ConnectionString))
                {
                    bConnected = false;
                    return(0);
                }

                try
                {
                    _RefDBConnection               = new PMSRefDBConnection();
                    _RefDBConnection.RefDBType     = RefDBType.OleDB;
                    _RefDBConnection.ConnectString = ConnectionString;
                    _OleDbConn = _RefDBConnection.GetOleConnection();

                    if (_OleDbConn == null)
                    {
                        string strCaption = GetStringFromPublicResourceClass.GetStringFromPublicResource("PMS_Warnning");
                        MessageBox.Show(_RefDBConnection.GetLastError(), strCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        bConnected             = false;
                        this.button_OK.Enabled = true;
                        return(0);
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    bConnected             = false;
                    this.button_OK.Enabled = true;
                    return(0);
                }

                if (_OleDbConn.State == ConnectionState.Open)
                {
                    // 连接数据库成功
                    SaveSettings();
                    bConnected = true;
                    return(2);
                }
                else
                {
                    bConnected = false;
                    return(0);
                }
            }

            return(0);
        }