码迷,mamicode.com
首页 > 其他好文 > 详细

批量添加小知识

时间:2017-11-20 16:42:48      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:type   data   ndt   this   cut   nbsp   foreach   ati   alert   

function AddP()
{
//定义字符串
var str = "";
//遍历动态行的值
$("tbody tr").each(function () {
//遍历当前行下的input元素
$(this).children().children().each(function () {
//判断当前元素是否为空
if ($(this).val() != ""&& $(this).attr("class")!="")
{
//不为空则获取当前数据并拼接
str += $(this).val() + ",";
}
})
//判断字符串是否为空
if (str != "" && str != null && str != undefined)
{
//不为空则为截取每行数据后的点并添加分割符号
str = str.substring(0, str.length - 1) + "|";
}
})
str = str.substring(0, str.length - 1);
alert(str);
//传值
$.ajax({
url: "/Show/Addplit",
type: "post",
data: { "str": str },
dataType:"json",
success: function (da)
{
alert(da);
if (da > 0) {
alert("保存成功");
location.href = ‘/Show/Index‘;
}
else {
alert("保存失败");
}
},
error: function () {
alert(‘获取错误,正在加载中......‘)
}
})
}

 

string[] strlist = str.Split(‘|‘);
//创建集合
List<Class1> li = new List<Class1>();
//循环遍历数组中的数据并添加进集合
for (int i = 0; i < strlist.Length; i++)
{
//实例一个对象
Class1 t = new Class1();
t.Stu_name = strlist[i].Split(‘,‘)[0].ToString();
t.Stu_class = Convert.ToInt32(strlist[i].Split(‘,‘)[1].ToString());
//一行一行添加进集合
li.Add(t);
}
var zz = new BLL.Pbll().AddPlist(li);

 

con.Open();
SqlCommand com = con.CreateCommand();
string str = "";
int k = 0;
//循环集合中数据往数据库中添加
foreach (var i in list)
{
k++;
str += "insert into Student values (‘" + i.Stu_name + "‘," + i.Stu_class + ")";
}
com.CommandText =str;
var rult= com.ExecuteNonQuery();

 

批量添加小知识

标签:type   data   ndt   this   cut   nbsp   foreach   ati   alert   

原文地址:http://www.cnblogs.com/w-pengchao/p/7866890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!