服務器端cs文件

 服務器端向mysql數據庫寫數據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數據庫讀數據sql

 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.Controllers
15 {
16     public class OrderController : Controller
17     {
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語句生成datatable
28             MySQLDataAdapter sda = new MySQLDataAdapter(str, conn);
29             DataTable dt = new DataTable();
30             sda.Fill(dt);//填充數據到dt
31 
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<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
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而後再轉化爲JArray
48             return JArray.Parse(jss.Serialize(dic));
49         }
50 
51     }
52 }
View Code
相關文章
相關標籤/搜索