C # Operations database, try to encapsulate into class-to seek the wrong guidance.

Source: Internet
Author: User

MySQL operation

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 (CMD, conn);                ds = new DataSet (); Mdap.            Fill (ds, "allhardtable");            } catch (Exception ex) {ds = null;        } return DS; }//Returns whether a user is found, and if >0 indicates that it can log in//public int checklogin (Mysqlparameter [] paras) public int checklogin (S            Tring user,string Pass)//Unexpected return-1 {int loginstatus; String sql = "SELECT COUNT (*) from login where [Email protected]_username and [Email protected]_password";//An                d [Email protected]_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 for each field 3 database type 4 type length public int o_adddata (String sql, string[] input, mysqldbtype [] DBT Ype, int[] dbsize) {//string sql = "INSERT into login (Onepc_username, Onepc_password, Onepc_level, one Pc_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 (A.length.tostring ());                Int[] C = new Int[a.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;//-1 means the regular match is not} length = filed.            Length; if (input. Length! = Length | | DbType. Length! = Length | | DbsIze.                Length! = length) {result =-2; return result;            The input parameter is not of the correct length} 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;                        Execute SQL statement Exception} finally {O_close ();        } return result; }            }}

Call

String sql = "INSERT INTO Login" (Onepc_username, Onepc_password, Onepc_level, Onepc_display, onepc_size) VALUES (@onepc_us Ername, @onepc_password, @onepc_level, @onepc_display, @onepc_size) ";            String[] Value = {"Huangwen", "Mima", "0", "Heart Static Ice", "+"};            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 (A.tostring ());

  

C # Operations database, try to encapsulate into class-to seek the wrong guidance.

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.