C # connect to a mysql instance,
Using System; using System. configuration; using MySql. data. mySqlClient; // <summary> // summary of TestDatebase /// </summary> public class TestDatebase {public TestDatebase () {// TODO: add the constructor logic //} public static void Main (String [] args) {MySqlConnection mysql = getMySqlCon (); // query SQL String sqlSearch = "select * from student"; // insert SQL String sqlInsert = "insert into student values (12, 'zhang san', 25,'') "; // Modify SQL String sqlUpdate =" update student set name = 'lily' where id = 3 "; // delete SQL String sqlDel = "delete from student where id = 12"; // print the SQL statement Console. writeLine (sqlDel); // four statement objects // MySqlCommand mySqlCommand = getSqlCommand (sqlSearch, mysql); // MySqlCommand mySqlCommand = getSqlCommand (sqlInsert, mysql ); // MySqlCommand mySqlCommand = getSqlCommand (sqlUpdate, mysql); MySqlCommand mySqlCommand = g EtSqlCommand (sqlDel, mysql); mysql. open (); // getResultset (mySqlCommand); // getInsert (mySqlCommand); // getUpdate (mySqlCommand); getDel (mySqlCommand); // remember to close mysql. close (); String readLine = Console. readLine ();} /// <summary> /// create a mysql database link /// </summary> /// <returns> </returns> public static MySqlConnection getMySqlCon () {String mysqlStr = "Database = test; Data Source = 127.0.0.1; User Id = root; Passw Ord = root; pooling = false; CharSet = utf8; port = 3306 "; // String mySqlCon = ConfigurationManager. connectionStrings ["MySqlCon"]. connectionString; MySqlConnection mysql = new MySqlConnection (mysqlStr); return mysql ;} /// <summary> /// create the execution command statement object // </summary> /// <param name = "SQL"> </param> // <param name = "mysql"> </param> // <returns> </returns> public static MySqlCommand getSqlCommand (String SQL, mySqlCo Nnection mysql) {MySqlCommand mySqlCommand = new MySqlCommand (SQL, mysql); // MySqlCommand mySqlCommand = new MySqlCommand (SQL); // mySqlCommand. connection = mysql; return mySqlCommand ;} /// <summary> /// query and obtain the result set and traverse the result set /// </summary> /// <param name = "mySqlCommand"> </param> public static void getResultset (MySqlCommand mySqlCommand) {MySqlDataReader reader = mySqlCommand. executeReader (); try {whil E (reader. read () {if (reader. hasRows) {Console. writeLine ("No.:" + reader. getInt32 (0) + "| Name:" + reader. getString (1) + "| age:" + reader. getInt32 (2) + "| Education:" + reader. getString (3) ;}} catch (Exception) {Console. writeLine ("query failed! ");} Finally {reader. close ();}} /// <summary> /// add data /// </summary> /// <param name = "mySqlCommand"> </param> public static void getInsert (MySqlCommand mySqlCommand) {try {mySqlCommand. executeNonQuery ();} catch (Exception ex) {String message = ex. message; Console. writeLine ("failed to insert data! "+ Message );}} /// <summary> /// modify data /// </summary> /// <param name = "mySqlCommand"> </param> public static void getUpdate (MySqlCommand mySqlCommand) {try {mySqlCommand. executeNonQuery ();} catch (Exception ex) {String message = ex. message; Console. writeLine ("failed to modify data! "+ Message );}} /// <summary> /// delete data /// </summary> /// <param name = "mySqlCommand"> </param> public static void getDel (MySqlCommand mySqlCommand) {try {mySqlCommand. executeNonQuery ();} catch (Exception ex) {String message = ex. message; Console. writeLine ("failed to delete data! "+ Message );}}}