Beispiel #1
0
        /// <summary>
        /// 获取数据库配置
        /// </summary>
        /// <returns></returns>
        public static DbConfigInfo GetDbConfig()
        {
            DbConfigInfo dbConfigInfo = new DbConfigInfo();
            string       config       = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "db.xml";

            if (!File.Exists(config))
            {
                DataTable dt = new DataTable("DBConfig");
                dt.Columns.Add("ServerName", Type.GetType("System.String") ?? throw new InvalidOperationException());
                dt.Columns.Add("UserName", Type.GetType("System.String") ?? throw new InvalidOperationException());
                dt.Columns.Add("UserPassword", Type.GetType("System.String") ?? throw new InvalidOperationException());
                dt.Columns.Add("Port", Type.GetType("System.Int32") ?? throw new InvalidOperationException());
                dt.Columns.Add("DataBase", Type.GetType("System.String") ?? throw new InvalidOperationException());
                dt.Columns.Add("DbType", Type.GetType("System.String") ?? throw new InvalidOperationException());
                dt.Columns.Add("ConnectionName", Type.GetType("System.String") ?? throw new InvalidOperationException());
                dbConfigInfo.ServerName     = "127.0.0.1";
                dbConfigInfo.UserName       = "******";
                dbConfigInfo.UserPassword   = "******";
                dbConfigInfo.Port           = 3306;
                dbConfigInfo.DataBase       = "test";
                dbConfigInfo.DbType         = SQLDbTypes.MySQL;
                dbConfigInfo.ConnectionName = "test";

                DataRow dr = dt.NewRow();
                dr["ServerName"]     = dbConfigInfo.ServerName;
                dr["UserName"]       = dbConfigInfo.UserName;
                dr["UserPassword"]   = dbConfigInfo.UserPassword;
                dr["Port"]           = dbConfigInfo.Port;
                dr["DataBase"]       = dbConfigInfo.DataBase;
                dr["DbType"]         = dbConfigInfo.DbType;
                dr["ConnectionName"] = dbConfigInfo.ConnectionName;
                dt.Rows.Add(dr);
                dt.AcceptChanges();
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.WriteXml(config, XmlWriteMode.WriteSchema);
            }
            else
            {
                DataSet ds = new DataSet();
                ds.ReadXml(config, XmlReadMode.ReadSchema);
                DataRow dr = ds.Tables[0].Rows[0];
                dbConfigInfo.ServerName   = dr["ServerName"].ToString();
                dbConfigInfo.UserName     = dr["UserName"].ToString();
                dbConfigInfo.UserPassword = dr["UserPassword"].ToString();
                try
                {
                    dbConfigInfo.Port = int.Parse(dr["Port"].ToString());
                }
                catch
                {
                    dbConfigInfo.Port = 3306;
                }
                dbConfigInfo.DataBase = dr["DataBase"].ToString();
                Enum.TryParse(dr["DbType"].ToString(), true, out SQLDbTypes dbType);
                dbConfigInfo.DbType = dbType;
            }
            return(dbConfigInfo);
        }
Beispiel #2
0
        public static void SaveDbConfig(DbConfigInfo dbConfigInfo)
        {
            string  config = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "db.xml";
            DataSet ds     = new DataSet();

            ds.ReadXml(config, XmlReadMode.ReadSchema);
            DataRow dr = ds.Tables[0].Rows[0];

            dr["ServerName"]   = dbConfigInfo.ServerName;
            dr["UserName"]     = dbConfigInfo.UserName;
            dr["UserPassword"] = dbConfigInfo.UserPassword;
            dr["Port"]         = dbConfigInfo.Port;
            dr["DataBase"]     = dbConfigInfo.DataBase;
            dr["DbType"]       = dbConfigInfo.DbType;
            ds.Tables[0].AcceptChanges();
            ds.WriteXml(config, XmlWriteMode.WriteSchema);
        }
Beispiel #3
0
        private void TopMenuItem2_Click(object sender, EventArgs e)
        {
            DbConfigInfo dbConfigInfo = DbConfigUtil.GetDbConfig();

            if (dbConfigInfo != null)
            {
                Global.GetInstance().DB = dbConfigInfo;
            }

            try
            {
                Global.GetInstance().Tables = BSqlFunction.GetInstance().GetTables();
                CreateTreeNode();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Beispiel #4
0
 private void InitData()
 {
     _dbConfigInfo      = DbConfigUtil.GetDbConfig();
     txtServerName.Text = _dbConfigInfo.ServerName;
     txtUserName.Text   = _dbConfigInfo.UserName;
     txtPwd.Text        = _dbConfigInfo.UserPassword;
     txtPort.Text       = _dbConfigInfo.Port.ToString("D");
     txtDataBase.Text   = _dbConfigInfo.DataBase;
     if (_dbConfigInfo.DbType == SQLDbTypes.MySQL)
     {
         rbMySQL.Checked = true;
     }
     else
     {
         rbSQLServer.Checked = true;
     }
     //cmbbSQLType.SelectedValue = dbConfigInfo.DbType;
     //cmbbSQLType.SelectedIndex = cmbbSQLType.Items.IndexOf(dbConfigInfo.DbType);
     //cmbbSQLType.SelectedText = dbConfigInfo.DbType;
 }