Json导入数据库

 

<!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: '/Home/A',
data: 'get',
dataType: 'json',
success: function (res) {
data = res.data;
$.each(res.data, function (i, r) {
var tr = '<tr>'
+ '<td id="1">' + this.id + '</td>'
+ '<td id="2">' + this.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>';
$("#tb").append(tr);
})

}

})
})


function Add(data) {
alert(data);
$.ajax({
url: "/Home/Add",
type: "post",
dataType: "json",
data: { list: data },
success: function (res) {

}
})

}

//function Tianjia() {

// //var id = document.getElementById('1').innerHTML;
// //var username = document.getElementById('2').innerHTML;
// //var email = document.getElementById('3').innerHTML;
// //var sex = document.getElementById('4').innerHTML;
// //var city = document.getElementById('5').innerHTML;
// //var sign = document.getElementById('6').innerHTML;
// //var experience = document.getElementById('7').innerHTML;
// //var ip = document.getElementById('8').innerHTML;
// //var logins = document.getElementById('9').innerHTML;
// //var joinTime = document.getElementById('10').innerHTML;
// //$.ajax({
// // url: "/Home/Add",
// // type: "post",
// // dataType: "json",
// // data: { id: id, username: username, email: email, sex: sex, city: city, sign: sign, experience: experience, ip: ip, logins: logins, joinTime: joinTime },
// // success: function (data) {
// // if (data > 0) {
// // alert('1');
// // }
// // }
// //})
//}
</script>
</head>
<body>
<div>
<input id="Button1" type="button" value="button" onclick="Add(data)" />
<table>
<tr>
<td>ID</td>
<td>用户名</td>
<td>邮箱</td>
<td>性别</td>
<td>城市</td>
<td>签名</td>
<td>积分</td>
<td>IP</td>
<td>登入次数</td>
<td>加入时间</td>
</tr>
<tbody id="tb">

</tbody>
</table>
</div>
</body>
</html>

 

public ActionResult AAA()
{
return View();
}
[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<Class1>(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;
}
[HttpPost]
public string Add(List<Stock> list)
{
for (int i = 0; i < list.Count(); i++)
{
Stock k = 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",k.id),
new SqlParameter("@username",k.username),
new SqlParameter("@email",k.email),
new SqlParameter("@sex",k.sex),
new SqlParameter("@city",k.city),
new SqlParameter("@sign",k.sign),
new SqlParameter("@experience",k.experience),
new SqlParameter("@ip",k.ip),
new SqlParameter("@logins",k.logins),
new SqlParameter("@joinTime",k.joinTime),
};
int count = DBHelper.ZSG(sql, pa);
}

var data = new
{
a = 200,
b = "添加成功"
};
var json = JsonConvert.SerializeObject(data);
return json;

}

posted on 2019-10-29 09:25  林深鹿  阅读(1621)  评论(2编辑  收藏  举报

导航