简单的批量导入的实现
//js:
$("#btninsert").click(function (e) {
var b = $("#txtmore").val()
var tmp1 = $("#txtmore").val().split("\n"); //过滤换行符,获取每一行的数据
var tmp = "";
var a = "";
for (i = 0; i < tmp1.length; i++)
{
tmp1[i] = tmp1[i].replace(/\s+/g, ' '); //过滤字符串前面的空格
tmp1[i] = tmp1[i].replace(/(^\s*)|(\s*$)/g, ''); //过滤字符串中间的空格
tmp = tmp1[i].split(" ");//过滤空格,获取每行里面的三个数据
//下面是对数据做判断生成log提示用户,还没做全
if (tmp.length != 3) {
a += "第" + i + 1 + "行数据格式不正确!\n";
}
if (!tmp[2] == "" && tmp[2].length < 6)
a += "第" + i + 1 + "行第三个密码长度必须超过6位\n";
}
if (a != "")
{
aler(a);
return false;
}
return true;
});
//c#:
try
{
basepage_common bp = new basepage_common();
if (string.isnullorempty(txtmore.text))
{
messagebox.show(this, "批量用户数据不能为空,请输入用户");
return;
}
var tmp1 = txtmore.text.split(new char[] {'\r', '\n' },stringsplitoptions.removeemptyentries);
string[] tmp;
list<userinfo_base> list = new list<userinfo_base>();
for (var i = 0; i < tmp1.length; i++)
{
var entity =conversionhelper.namevaluecollectiontogeneric<userinfo_base>(request.form);
tmp1[i] = tmp1[i].tostring().replace("\t", " ");
tmp1[i] = new regex("[\\s]+").replace(tmp1[i], " ");
tmp1[i] = tmp1[i].trim();
tmp=tmp1[i].split(' ');
if (tmp.length!= 3)
throw new logicerror("非法访问");//如果用户跳过js验证,说明是非法用户
entity.truename = tmp[0].tostring();
entity.loginname = tmp[1].tostring();
if (tmp[2].length < 6)
throw new logicerror("密码长度不能小于6位");
entity.loginpwd = tmp[2].tostring();
list.add(entity); //用list装数据
}
var nentity = sys.insert(request.url.tostring(), "systemuser", "systemuser_add", bp.userip, loginuser.userid, list); //因为采用的是多层架构所以直接调用dal层,遍历list执行插入操作,执行成功事务commit;
if (nentity)
messagebox.show(this," ","添加成功","user.aspx");
}
catch (logicerror lgex)
{
loghelper.debug(lgex.message);
messagebox.show(this, lgex.message);
}