json导入数据库
/// <summary> /// 显示视图 /// </summary> /// <returns></returns> public ActionResult AAA() { return View(); } /// <summary> /// 显示方法 /// </summary> /// <returns></returns> [HttpGet] public string A() { HttpClient client = new HttpClient(); var aaa = client.GetStringAsync("https://www.layui.com/test/table/demo1.json"); var bbb = aaa.Result; var json = JsonConvert.DeserializeObject<Table1>(bbb); var data = json.data; for (int i = 0; i < data.Count(); i++) { string sql = "INSERT INTO [dbo].[KuaYu]([id],[username],[email],[sex],[city],[sign],[experience],[ip],[logins],[joinTime]) VALUES(@id,@username,@email,@sex,@city,@sign,@experience,@ip,@logins,@joinTime)"; SqlParameter[] pa = new SqlParameter[] { new SqlParameter("@id",data[i].id), new SqlParameter("@username",data[i].username), new SqlParameter("@email",data[i].email), new SqlParameter("@sex",data[i].sex), new SqlParameter("@city",data[i].city), new SqlParameter("@sign",data[i].sign), new SqlParameter("@experience",data[i].experience), new SqlParameter("@ip",data[i].ip), new SqlParameter("@logins",data[i].logins), new SqlParameter("@joinTime",data[i].joinTime), }; int count = DBHelper.ZSG(sql, pa); } return bbb; } /// <summary> /// 添加 /// </summary> /// <param name="list"></param> /// <returns></returns> [HttpPost] public string AA(List<Table2> list) { for (int i = 0; i < list.Count(); i++) { Table2 t = list[i]; string sql = "INSERT INTO [dbo].[KuaYu]([id],[username],[email],[sex],[city],[sign],[experience],[ip],[logins],[joinTime]) VALUES(@id,@username,@email,@sex,@city,@sign,@experience,@ip,@logins,@joinTime)"; SqlParameter[] pa = new SqlParameter[] { new SqlParameter("@id",t.id), new SqlParameter("@username",t.username), new SqlParameter("@email",t.email), new SqlParameter("@sex",t.sex), new SqlParameter("@city",t.city), new SqlParameter("@sign",t.sign), new SqlParameter("@experience",t.experience), new SqlParameter("@ip",t.ip), new SqlParameter("@logins",t.logins), new SqlParameter("@joinTime",t.joinTime), }; int count = DBHelper.ZSG(sql, pa); } var data = new { a = 200, b = "添加成功" }; var json = JsonConvert.SerializeObject(data); return json; }
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>AAA</title> <script src="~/Scripts/jquery-3.3.1.min.js"></script> <script> var data; $(function () { $.ajax({ url: "/Month/A", data: "get", dataType: "json", success: function (res) { data = res.data; $.each(res.data, function (i, r) { var line = '<tr>' + '<td id="1">' + r.id + '</td>' + '<td id="2">' + r.username + '</td>' + '<td id="3">' + r.email + '</td>' + '<td id="4">' + r.sex + '</td>' + '<td id="5">' + r.city + '</td>' + '<td id="6">' + r.sign + '</td>' + '<td id="7">' + r.experience + '</td>' + '<td id="8">' + r.ip + '</td>' + '<td id="9">' + r.logins + '</td>' + '<td id="10">' + r.joinTime + '</td>' + '</tr>'; $("#td").append(line); }) } }) }) function Add(data) { $.ajax({ url: "/Month/AA", type: "post", dataType: "json", data: {list:data}, success: function (res) { $(res).each(function () { }) } }) } </script> </head> <body> <div> <input id="Button1" type="button" value="button" onclick="Add(data)"/> <table> <thead> <tr> <td>id</td> <td>username</td> <td>email</td> <td>sex</td> <td>city</td> <td>sign</td> <td>experience</td> <td>ip</td> <td>logins</td> <td>joinTime</td> </tr> </thead> <tbody id="td"></tbody> </table> </div> </body> </html>