form提交多条记录到MySQL_form表单有多条数据,行是动态添加的,怎么用saveall批量插入数据库...

展开全部

姓名年龄血压
高压低压

JS文档

///

///

$(function () {

//定义一个全局变量i,用来标识添加了几行

var row = 0;

var strValue = "";

//将一e5a48de588b662616964757a686964616f31333361303731行添加到table中去

$('#btnInsert').click(function () {

row++;

//字符串拼接tr一行中的内容

var tr = "

";

for (var i = 0; i < 3; i++) {

tr += "

";

}

tr += "

";

$("#tab").append(tr);

})

//删除添加的行,先判断checkbox是否选中,然后删除

$('#btnDelete').click(function () {

$("input[name=ckb]:checked").each(function () { $(this).parent().parent().remove(); });

})

//将i遍历,判断是否存有值,如果有将数据插入数据库

$('#btnData').click(function () {

$('table input').each(function () {

strValue += $(this).val() + ",";

})

$.ajax({

type: 'post',

contentType: 'application/json',

url: "../WebService1.asmx/InsertInfo",

data: "{valuesStr:'" + strValue + "'}",

success: function (result) {

$('#mydiv').html(result.d);

}

})

})

})

CSS文档

table tr td{border:1px solid #eee;text-align:center;width:80px;}

#divInsert{width:100%;height:25px;}

#btnInsert{width:50px;height:25px;background-color:#eee;border-style:none;position:absolute;left:185px;}

#btnData{width:50px;height:25px;background-color:#eee;border-style:none;position:absolute;left:235px;}

#btnDelete{width:50px;height:25px;background-color:#eee;border-style:none;position:absolute;left:285px;}

input{width:70px;}

.txt{width:35px;float:left;position:relative;left:5px;}

.ck{width:10px;float:right;}

PersonInfo类文件

namespace ASPOilfiled

{

public class PersonInfo

{

public string Name { get; set; }

public int Age { get; set; }

public int Hblood { get; set; }

public int Lblood { get; set; }

}

}

WebService1.asmx 文件

[System.Web.Script.Services.ScriptService]

public class WebService1 : System.Web.Services.WebService

{

public static string sqlstr = ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;

[WebMethod]

public string InsertInfo(string valuesStr)

{

string result = "插入失败!";

string asd = valuesStr.Replace("on,", "");

string[] str2 = System.Text.RegularExpressions.Regex.Split(asd, ",");

//for (int i = 0; i < str2.Length; i++)

//{

// result += str2[i] + "
";

//}

for (int i = 0; i < str2.Length; i++)

{

PersonInfo info;

if (i % 4 == 0)

{

info = new PersonInfo();

info.Name = str2[i];

info.Age = Convert.ToInt32(str2[i + 1]);

info.Hblood = Convert.ToInt32(str2[i + 2]);

info.Lblood = Convert.ToInt32(str2[i + 3]);

result = InfoToSql(info).ToString();

}

}

return result;

}

public int InfoToSql(PersonInfo info)

{

string instSql = "insert into PersonInfo values(@name,@age,@Hblood,@Lblood)";

SqlConnection con = new SqlConnection(sqlstr);

con.Open();

SqlCommand cmd = con.CreateCommand();

cmd.CommandText = instSql;

cmd.Parameters.Add("@name", SqlDbType.VarChar, 32).Value = info.Name;

cmd.Parameters.Add("@age", SqlDbType.Int).Value = info.Age;

cmd.Parameters.Add("@Hblood", SqlDbType.Int).Value = info.Hblood;

cmd.Parameters.Add("@Lblood", SqlDbType.Int).Value = info.Lblood;

int i = cmd.ExecuteNonQuery();

cmd.Dispose();

con.Dispose();

return i;

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值