通过winfrom界面 修改Mysql的用户(root)的密码
处理思路:
1、利用MySql.Data.MySqlClient 验证当前的数据库连接信息是否正确;
2、调用命令行 执行C:\\mysql\\bin\\mysql 的 SET PASSWORD 进行用户密码修改;
3、利用新的用户密码 重新验证对数据库的连接是否正确;
4、成功或失败的提示;
示例代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
}
}
}
}
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;
}
}
}
}
posted on 2010-10-27 13:04 freeliver54 阅读(455) 评论(1) 编辑 收藏 举报