C # connect to a mysql instance,

Source: Internet
Author: User

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 );}}}

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.