博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
服务器端cs文件
阅读量:6647 次
发布时间:2019-06-25

本文共 3618 字,大约阅读时间需要 12 分钟。

 服务器端向mysql数据库写数据

using System;using Newtonsoft.Json;using Newtonsoft.Json.Linq;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Data.OleDb;using System.Drawing;using System.Data;using MySQLDriverCS;using System.Web.Script.Serialization;namespace GLSXJSON2.Controllers{    public class AdviseInsertController : Controller    {        public string Index(string text)  //这里的text是从service文件中获取的参数        {            MySQLConnection conn = null;            conn = new MySQLConnection(new MySQLConnectionString("localhost", "glsx", "root", "ecust2016", 3306).AsString);            conn.Open();     //这几句为建立一个MySQL的连接conn            //sql语句写成string形式            string str0 = string.Format("select MAX(Id) from fag order by Id desc");            int id;            MySQLCommand cmd0 = new MySQLCommand(str0, conn);        //建立MySQL执行命令cmd0 参数为str0和conn            if(cmd0.ExecuteScalar()!=null&&!Convert.IsDBNull(cmd0.ExecuteScalar())){
//执行查询操作,ExecuteScalar()返回查询结果第一行 id=int.Parse(cmd0.ExecuteScalar().ToString())+1; //这是不为空情况把id+1 } else id=0; //若为空id置0 //sql语句写成string形式 string str = string.Format("insert into fag(Id,content) values({0},'{1}')", id, text); try { MySQLCommand cmd = new MySQLCommand(str, conn); //建立MySQL执行命令cmd 参数为str和conn cmd.ExecuteNonQuery(); //返回执行的条数 return "1"; //执行成功返回1 } catch { return "-1"; //失败返回-1 } } }}
View Code

 服务器端从mysql数据库读数据

1 using System; 2 using Newtonsoft.Json; 3 using Newtonsoft.Json.Linq; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Mvc; 8 using System.Data.OleDb; 9 using System.Drawing;10 using System.Data;11 using MySQLDriverCS; 12 using System.Web.Script.Serialization;13 14 namespace GLSXJSON2.Controllers15 {16     public class OrderController : Controller17     {18        19         public JArray Index(string value,string id)20         {21             MySQLConnection conn = null;22             conn = new MySQLConnection(new MySQLConnectionString("localhost", "glsx", "root", "ecust2016", 3306).AsString);23             conn.Open();24             //sql语句写成string形式25             string str = 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));26 27             //用conn连接数据库然后执行sql语句生成datatable28             MySQLDataAdapter sda = new MySQLDataAdapter(str, conn);29             DataTable dt = new DataTable();30             sda.Fill(dt);//填充数据到dt31 32             //创建一个用以序列化的对象33             JavaScriptSerializer jss = new JavaScriptSerializer();34             //从datatable中逐列获取值到数组35              System.Collections.ArrayList dic = new System.Collections.ArrayList();36              foreach (DataRow dr in dt.Rows)37              {38                  System.Collections.Generic.Dictionary
drow = new System.Collections.Generic.Dictionary
();39 foreach (DataColumn dc in dt.Columns)40 {41 drow.Add(dc.ColumnName, dr[dc.ColumnName]);42 }43 dic.Add(drow);44 }45 conn.Close();46 47 //将获取到的数列序列化以后转化为string然后再转化为JArray48 return JArray.Parse(jss.Serialize(dic));49 }50 51 }52 }
View Code

 

转载地址:http://jtuto.baihongyu.com/

你可能感兴趣的文章