Server-side CS file

Source: Internet
Author: User

Server-side write data to MySQL database

usingSystem;usingNewtonsoft.json;usingNewtonsoft.Json.Linq;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Data.OleDb;usingSystem.Drawing;usingSystem.Data;usingMysqldrivercs;usingSystem.Web.Script.Serialization;namespaceGLSXJSON2. controllers{ Public classAdviseinsertcontroller:controller { Public stringIndex (stringText//the text here is the parameter obtained from the service file{Mysqlconnection conn=NULL; Conn=NewMysqlconnection (NewMysqlconnectionstring ("localhost","glsx","Root","ecust2016",3306).            asstring); Conn.     Open (); //here are some sentences for building a MySQL connection conn//SQL statements are written in string form            stringSTR0 =string. Format ("select MAX (ID) from the from-a-do order by Id Desc"); intID; Mysqlcommand cmd0=NewMysqlcommand (STR0, conn);//build mysql Execute command cmd0 parameters for STR0 and Conn            if(cmd0. ExecuteScalar ()! =NULL&&! Convert.isdbnull (cmd0. ExecuteScalar ())) {//performs a query operation, ExecuteScalar () returns the first row of query resultsId=int. Parse (cmd0. ExecuteScalar (). ToString ()) +1;//This is not an empty case to put id+1            }            ElseID=0;//If an empty ID is set to 0//SQL statements are written in string form            stringstr =string. Format ("INSERT into id,content values ({0}, ' {1} ')", id, text); Try{mysqlcommand cmd=NewMysqlcommand (STR, conn);//build mysql execute command cmd parameter for STR and connCmd. ExecuteNonQuery ();//returns the number of executed bars                return "1";//execution successful return 1            }            Catch            {                return "-1";//failed return-1            }                     }    }}
View Code

Server-side read data from MySQL database

1 usingSystem;2 usingNewtonsoft.json;3 usingNewtonsoft.Json.Linq;4 usingSystem.Collections.Generic;5 usingSystem.Linq;6 usingsystem.web;7 usingSYSTEM.WEB.MVC;8 usingSystem.Data.OleDb;9 usingSystem.Drawing;Ten usingSystem.Data; One usingMysqldrivercs; A usingSystem.Web.Script.Serialization; -  - namespaceGLSXJSON2. Controllers the { -      Public classOrdercontroller:controller -     { -         +          PublicJarray Index (stringValuestringID) -         { +Mysqlconnection conn =NULL; Aconn =NewMysqlconnection (NewMysqlconnectionstring ("localhost","glsx","Root","ecust2016",3306). asstring); at Conn. Open (); -             //SQL statements are written in string form -             stringstr =string. Format ("Select placetime,price,cloth11,cloth12,cloth13,cloth14,cloth21,cloth22,cloth23 from ' Order ' where status= ' {0} ' and customer= ' {1} '",int. Parse (value),int. Parse (ID)); -  -             //connect the database with conn and execute the SQL statement to generate the DataTable -Mysqldataadapter SDA =NewMysqldataadapter (STR, conn); inDataTable dt =NewDataTable (); -Sda. Fill (DT);//populate data to DT to  +             //create an object to serialize -JavaScriptSerializer JSS =NewJavaScriptSerializer (); the             //getting values from a DataTable column to an array *System.Collections.ArrayList dic =NewSystem.Collections.ArrayList (); $              foreach(DataRow Drinchdt. Rows)Panax Notoginseng              { -system.collections.generic.dictionary<string,Object> Drow =Newsystem.collections.generic.dictionary<string,Object>(); the                  foreach(DataColumn DCinchdt. Columns) +                  { A Drow. ADD (DC. ColumnName, DR[DC. ColumnName]); the                  } + DiC. ADD (drow); -              } $ Conn. Close (); $  -             //Converts the obtained sequence of numbers into a string and then into a jarray -             returnJarray.parse (JSS. Serialize (DIC)); the         } - Wuyi     } the}
View Code

Server-side CS file

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.