文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>通过winfrom界面 修改Mysql的用户(root)的密码

通过winfrom界面 修改Mysql的用户(root)的密码

时间:2010-10-27  来源:freeliver54

示例代码:

代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WinPassCheck
{
    public partial class PassCheck : Form
    {
        public PassCheck()
        {
            InitializeComponent();
        }  

        #region 执行命令

        public string ExecuteCmd(string setupPath, string ArgumentList)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = setupPath;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.Arguments = ArgumentList;

            p.Start();
            string strRst = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            
            return strRst;
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                string strResult = "";
                string newPath = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).Directory.FullName;
                if (this.txtNewPassword.Text != this.txtNewPasswordConfirm.Text)
                {
                    MessageBox.Show("两次输入密码不一致");
                    return;
                }

                if (fnIsConnDBOK(this.txtOldPassword.Text.Trim()))
                {
                    strResult = ExecuteCmd( "C:\\mysql\\bin\\mysql", " -u root -p" + this.txtOldPassword.Text.Trim() + " -e \"SET PASSWORD FOR root@'%' = OLD_PASSWORD('" + this.txtNewPassword.Text.Trim() + "')\"");

                    if (fnIsConnDBOK(this.txtNewPassword.Text.Trim()))
                    {
                        MessageBox.Show("修改成功");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("原密码错误,请核对数据库信息及原密码信息");
                    return;
                }
                
            }
            catch
            {
                MessageBox.Show("设置密码失败,请确认");
            } 
        }

        private bool fnIsConnDBOK(string strPassword)
        {
            try
            {
                //测试连接
                string connectionstring = "Host=" + this.txtIP.Text.Trim();
                connectionstring += ";User ID=" + this.txtUserName.Text.Trim();
                connectionstring += ";Password=" + strPassword;
                connectionstring += ";Port=" + this.txtPort.Text.Trim();
                connectionstring += ";Database=" + this.txtDBName.Text.Trim();
                connectionstring += ";pooling=false  ";
                using (MySqlConnection conn = new MySqlConnection(connectionstring))
                {
                    
                    conn.Open();
                    conn.Close();
                    conn.Dispose();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

 

 

相关阅读 更多 +
排行榜 更多 +
翌日波奇狗的历险记手机版下载

翌日波奇狗的历险记手机版下载

休闲益智 下载
怪兽远征安卓版下载

怪兽远征安卓版下载

角色扮演 下载
谷歌卫星地图免费版下载

谷歌卫星地图免费版下载

生活实用 下载