Example #1
0
        //连接数据库
        private void Btn_connect_Click(object sender, EventArgs e)
        {
            //获取数据库类型
            string    jdbcType = cob_jdbctype.Text.Trim();
            string    jdbc     = txt_jdbcname.Text.Trim();
            string    user     = txt_user.Text.Trim();
            string    password = txt_password.Text.Trim();
            DataTable dt       = new DataTable();

            //sqlserver数据库
            if (jdbcType == "sqlserver")
            {
                string connect = string.Format("Data Source=.;Initial Catalog={0};uid={1};pwd={2};"
                                               , jdbc, user, password);
                sqlContent = new SqlConnect_Ms(connect);
                dt         = sqlContent.GetAllTableNames();
            }
            //mysql数据库
            else if (jdbcType == "mysql")
            {
                string connect = string.Format("server=127.0.0.1;port=3306;user={0};password={1}; database={2};"
                                               , user, password, jdbc);
                sqlContent = new SqlConnect_My(connect);
                dt         = sqlContent.GetAllTableNames();
            }
            MessageBox.Show("数据库连接成功", "提示", MessageBoxButtons.OK);

            List <string> items = new List <string>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                items.Add(dt.Rows[i]["TABLE_NAME"].ToString());
            }
            items.Sort();
            cob_tablename.Items.Clear();//先清除所有项
            cob_tablename.Items.AddRange(items.ToArray());
        }