This article explains C # to the MySQL database for the increase and deletion check operation.
1. Install the database, and note that you want to install Connector NET.
2. Introduce MySql.Data.dll, add the project reference, the file general location is: C:\Program files (x86) \mysql\connector NET xx\assemblies\v xx (determined by the MYSQL installation location).
3. Add a test table to MySQL Workbench, this example creates a database test and creates a user table in the database with username and password two table entries, as follows:
4. For the database to be increased and censored, C # code is as follows:
1 usingSystem;2 usingMySql.Data.MySqlClient;3 4 namespaceMySQL Database operations5 {6 class Program7 {8 Static voidMain (string[] args)9 {Ten //Database Configuration One stringConnStr ="Database=test;datasource=127.0.0.1;port=3306;user=root;pwd=root;"; AMysqlconnection conn =Newmysqlconnection (CONNSTR); - - Conn. Open (); the - #regionInquire - // //querying all entries in the user table - //mysqlcommand cmd = new Mysqlcommand ("SELECT * from User", conn); + // - //mysqldatareader reader = cmd. ExecuteReader (); + // A // //reading data row by line at //While (reader. Read ()) - // { - //string username = reader. GetString ("username"); - //string password = reader. GetString ("password"); - //Console.WriteLine (username + ":" + password); - // } in // - //Reader. Close (); to #endregion + - #regionInsert the // //Insert a piece of data normally * //string username = "LJ"; string password = "6666"; $ //mysqlcommand cmd = new Mysqlcommand ("INSERT into user set username = '" + Username + "'" + ", password= '" + Password + "'", conn);Panax Notoginseng //cmd. ExecuteNonQuery (); - the // //SQL injection, the database is deleted + //string username = "LJ"; string password = "6666"; Delete from user; "; A //mysqlcommand cmd = new Mysqlcommand ("INSERT into user set username = '" + Username + "'" + ", password= '" + Password + "'", conn); the //cmd. ExecuteNonQuery (); + - // //prevent injection, do not execute Delete database statement $ //string username = "LJ"; string password = "6666"; Delete from user; "; $ //mysqlcommand cmd = new Mysqlcommand ("INSERT into user set [email protected], password = @pwd", conn); - //cmd. Parameters.addwithvalue ("UID", username); - //cmd. Parameters.addwithvalue ("pwd", password); the //cmd. ExecuteNonQuery (); - #endregionWuyi the #regionDelete - // //Delete an entry with an ID of 6 Wu //mysqlcommand cmd = new Mysqlcommand ("Delete from user where id = @id", conn); - //cmd. Parameters.addwithvalue ("id", 6); About // $ //cmd. ExecuteNonQuery (); - #endregion - - #regionUpdate A //change the entry with ID 7 pwd to lll +Mysqlcommand cmd =NewMysqlcommand ("Update user Set password = @pwd where id = 7", conn); theCmd. Parameters.addwithvalue ("pwd","lll"); - $ cmd. ExecuteNonQuery (); the #endregion the the Conn. Close (); the - Console.readkey (); in } the } the}
C # operations MySQL Database