C # operate the database and try to encapsulate it into a class,
Mysql operations
Using System; using System. collections. generic; using System. linq; using System. text; using System. data; using System. data. sqlClient; using MySql. data. mySqlClient; using System. text. regularExpressions; namespace importTxtToMysql {class oMySql {// private static String mysqlcon = "Data Source = MySQL; Password =; User ID = root; Location = 172.1.1.179 "; private static String mysqlcon = "database = onepc; Password =; User ID = root; server = 172.1.1.1 "; private MySqlConnection conn; public oMySql () {conn = new MySqlConnection (mysqlcon);} private void o_open () {if (conn. state = ConnectionState. open) {conn. close ();} conn. open ();} private void o_close () {if (conn. state = ConnectionState. open) {conn. close () ;}} public DataSet ReadMysql (String cmd) {DataSet ds; try {MySqlDataAdapter mdap = new MySqlDataAdapter (c Md, conn); ds = new DataSet (); mdap. fill (ds, "allhardtable") ;}catch (Exception ex) {ds = null;} return ds ;}// check whether a user exists, if it is greater than 0, you can log on to // public int CheckLogin (MySqlParameter [] paras) public int CheckLogin (String user, String pass) // if an exception occurs,-1 {int loginstatus is returned; string SQL = "select count (*) from login where onepc_username = @ onepc_username and onepc_password = @ onepc_password"; // and onepc_password = @ Onepc_password) try {o_open (); MySqlCommand cmd = new MySqlCommand (); cmd. commandText = SQL; cmd. connection = conn; MySqlParameter puser = new MySqlParameter ("@ onepc_username", MySqlDbType. varChar, 128); MySqlParameter ppass = new MySqlParameter ("@ onepc_password", MySqlDbType. varChar, 128); puser. value = user; ppass. value = pass; cmd. parameters. add (puser); cmd. parameters. add (ppass); loginstatus = Convert. toInt32 (cmd. executeScalar ();/* foreach (MySqlParameter para in paras) {cmd. parameters. add (para);} loginstatus = (int) cmd. executeScalar (); */} catch (Exception ex) {loginstatus =-1;} finally {o_close ();} return loginstatus ;} // Add record 1 SQL statement 2 values of each field 3 Database Type 4 length public int o_AddData (String SQL, String [] input, MySqlDbType [] dbtype, int [] dbsize) {// String SQL = "insert into lo Gin (onepc_username, onepc_password, onepc_level, onepc_display, onepc_size) values (@ onepc_username, @ onepc_password, @ onepc_level, @ onepc_display, @ onepc_size )"; // Regex R = new Regex (@ "\ s * insert \ s + into \ s + w + \ s * \ ([^)] *) \). * ");/* MessageBox. show (M. value, M. groups [1]. value); String [] a = M. groups [1]. value. split (','); MessageBox. show (. length. toString (); int [] c = new int [. length]; foreach (string B in a) {MessageBox. show (B);} */int length = 0, result = 0; String [] filed; regex R = new Regex (@ "\ s * insert \ s + into \ s + \ w + \ s * \ ([^)] *) \). * "); Match M = R. match (SQL); if (M. success) {filed = M. groups [1]. value. split (','); // Split table field} else {result =-1; return result; // return-1 indicates that the regular expression cannot match} length = filed. length; if (input. length! = Length | dbtype. Length! = Length | dbsize. Length! = Length) {result =-2; return result; // The length of the input parameter is incorrect} MySqlCommand cmd = new MySqlCommand (); MySqlParameter [] paras = new MySqlParameter [length]; for (int I = 0; I <length; I ++) {// int iv; // if (dbtype [I] = MySqlDbType. int32) // {//} paras [I] = new MySqlParameter ("@" + filed [I]. trim (), dbtype [I], dbsize [I]); //, input [I]);} for (int I = 0; I <length; I ++) {if (dbtype [I] = MySqlDbType. int32) {paras [I]. value = Convert. toInt32 (input [I]);} else {paras [I]. value = input [I] ;}} cmd. connection = conn; cmd. commandText = SQL; for (int I = 0; I <length; I ++) {cmd. parameters. add (paras [I]); // Add parameter} try {o_open (); cmd. executeNonQuery (); result = 1; // normal execution} catch (Exception ex) {result = 0; // an Exception occurred while executing the SQL statement} finally {o_close ();} return result ;}}}
Call
String SQL = "insert into login (values, onepc_password, onepc_level, onepc_display, onepc_size) values (@ onepc_username, @ onepc_password, @ onepc_level, @ onepc_display, @ onepc_size )"; string [] value = {"huangwen", "mima", "0", "Xin RuO Jing Ice", "100"}; MySqlDbType [] vtype = {MySqlDbType. varChar, MySqlDbType. varChar, MySqlDbType. varChar, MySqlDbType. varChar, MySqlDbType. int32}; int [] vsize = {128,128,128,128, 10}; oMySql insert = new oMySql (); int a = insert. o_AddData (SQL, value, vtype, vsize); MessageBox. show (. toString ());