使用C#修改MySQL資料庫使用者的root密碼

來源:互聯網
上載者:User

標籤:bcd   apt   cdata   drive   建立   串連資料庫   設定   comm   option   

前言

  利用C#調用CMD視窗實現修改資料庫密碼

  實現思路: 

      先通過odbc串連資料庫,擷取到Mysql的根目錄。根據根目錄的路徑調用CMD,串連MySQL,實現修改MySQL資料庫密碼。

 1 private void modifyPath_Click(object sender, EventArgs e) 2 { 3     bool isD = true; 4     string MysqlPath = string.Empty; 5     try 6     { 7         //擷取MySQL安裝路徑 8         DataTable db = ExecuteQuery("select @@basedir as basePath from dual","123"); 9         string sqlPath = string.Empty;10         sqlPath = db.Rows[0]["basePath"].ToString();11         //只判斷目錄在C/D盤的可能12         if (sqlPath.Contains("D:"))13         {14             path = sqlPath.Replace("D:", "");15         }16         else17         {18             path = sqlPath.Replace("C:", "");19             isD = false;20         }21     }22     catch23     {24         Application.Exit();25     }26 27     Process process = new Process();//建立進程對象28     try29     {30         ProcessStartInfo startInfo = new ProcessStartInfo();31         startInfo.FileName = "cmd.exe";//設定需要執行的命令32         //startInfo.Arguments = "/C " + 2;//“/C”表示執行完命令後馬上退出33         startInfo.UseShellExecute = false;//不使用系統外殼程式啟動34         startInfo.RedirectStandardInput = true;//不重新導向輸入35         startInfo.RedirectStandardOutput = true; //重新導向輸出36         //startInfo.RedirectStandardError = true;   //重新導向標準錯誤輸出37         startInfo.CreateNoWindow = true;//不建立視窗38         process.StartInfo = startInfo;39         if (process.Start())//開始進程40         {41             string newPsd = "123456";42             newPsd = "mysqladmin -u root -p123 password " + newPsd;43             if (isD)44             {45                 process.StandardInput.WriteLine("d:");46             }47             else48             {49                 process.StandardInput.WriteLine("cd\\");50             }51             process.StandardInput.WriteLine("cd" + path +"bin");52             process.StandardInput.WriteLine(newPsd);53         }54     }55     catch (Exception ex)56     {57         ex.ToString();58     }59     finally60     {61         if (process != null)62             process.Close();63     }64 }
//備忘: 密碼可以採用密文的方式,此處為了方便

串連資料庫語句ExecuteQuery的方法
 1 public static DataTable ExecuteQuery(string commandText, string psd) 2 { 3     DataSet dataSet = new DataSet(); 4     string connectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;OPTION=3;UID=root;PASSWORD=" + psd + ";Stmt=SET NAMES gb2312;"; 5     IDbConnection dbConnection = new OdbcConnection(connectionString); 6     DataTable result; 7     try 8     { 9         if (dbConnection.State == ConnectionState.Closed)10         {11             dbConnection.Open();12         }13         IDbCommand dbCommand = dbConnection.CreateCommand();14         dbCommand.CommandText = commandText;15         ((IDataAdapter)new OdbcDataAdapter16         {17             SelectCommand = dbCommand18         }).Fill(dataSet);19         result = dataSet.Tables[0];20     }21     catch (Exception ex)22     {23         MessageBox.Show("系統不相容,配置失敗");24         Application.Exit();25         result = null;26     }27     finally28     {29         try30         {31             dbConnection.Close();32         }33         catch34         {35         }36     }37     return result;38 }

使用C#修改MySQL資料庫使用者的root密碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.