Use C # to modify the root password of the MySQL database user

Source: Internet
Author: User
Tags odbc

Objective

Using C # to invoke the CMD window implementation to modify the database password

Implementation ideas:

Get to the root directory of MySQL by connecting to the database via ODBC first. Call cmd according to the path of the root directory, connect MySQL, implement modify MySQL database password.

1 Private voidModifypath_click (Objectsender, EventArgs e)2 {3     BOOLIsD =true;4     stringMysqlpath =string. Empty;5     Try6     {7         //get the MySQL installation path8DataTable db = ExecuteQuery ("SELECT @ @basedir as BasePath from dual","123");9         stringSQLPath =string. Empty;TenSQLPath = db. rows[0]["BasePath"]. ToString (); One         //only judge the possibility of the catalogue on the C/D disk. A         if(Sqlpath.contains ("D:")) -         { -Path = Sqlpath.replace ("D:",""); the         } -         Else -         { -Path = Sqlpath.replace ("C:",""); +IsD =false; -         } +     } A     Catch at     { - application.exit (); -     } -  -Process Process =NewProcess ();//Create a Process object -     Try in     { -ProcessStartInfo StartInfo =NewProcessStartInfo (); toStartinfo.filename ="Cmd.exe";//set the command to be executed +         //startinfo.arguments = "/C" + 2;//"/C" means to exit immediately after executing the command. -Startinfo.useshellexecute =false;//booting without using the system shell theStartinfo.redirectstandardinput =true;//do not redirect input *Startinfo.redirectstandardoutput =true;//REDIRECT output $         //Startinfo.redirectstandarderror = true; //REDIRECT standard error OutputPanax NotoginsengStartinfo.createnowindow =true;//do not create a window -Process. StartInfo =StartInfo; the         if(Process. Start ())//START Process +         { A             stringNEWPSD ="123456"; theNEWPSD ="mysqladmin-u root-p123 Password"+newpsd; +             if(IsD) -             { $Process. Standardinput.writeline ("D:"); $             } -             Else -             { theProcess. Standardinput.writeline ("cd\\"); -             }WuyiProcess. Standardinput.writeline ("CD"+ Path +"Li"); the process. Standardinput.writeline (NEWPSD); -         } Wu     } -     Catch(Exception ex) About     { $ Ex. ToString (); -     } -     finally -     { A         if(Process! =NULL) + process. Close (); the     } -}
Note: Password can be used in ciphertext, here for convenience

Method of connecting Database statement ExecuteQuery
1  Public StaticDataTable ExecuteQuery (stringCommandText,stringPSD)2 {3DataSet DataSet =NewDataSet ();4     stringConnectionString ="driver={mysql ODBC 3.51 DRIVER}; server=localhost;option=3; Uid=root; password="+ PSD +"; Stmt=set NAMES gb2312;";5IDbConnection dbConnection =NewOdbcConnection (connectionString);6 DataTable result;7     Try8     {9         if(Dbconnection.state = =connectionstate.closed)Ten         { One Dbconnection.open (); A         } -IDbCommand DbCommand =Dbconnection.createcommand (); -Dbcommand.commandtext =CommandText; the((IDataAdapter)NewOdbcDataAdapter -         { -SelectCommand =DbCommand -         }). Fill (DataSet); +result = dataset.tables[0]; -     } +     Catch(Exception ex) A     { atMessageBox.Show ("System not compatible, configuration failed"); - application.exit (); -result =NULL; -     } -     finally -     { in         Try -         { to dbconnection.close (); +         } -         Catch the         { *         } $     }Panax Notoginseng     returnresult; -}

Use C # to modify the root password of the MySQL database user

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.