根据循环更改参数值

时间:2014-02-09 19:30:20

标签: c# asp.net sql-server tsql

如果它们是可见的,我如何基于html表循环,并为每个循环分配不同的参数值。不确定这是否是解决此问题的正确方法,但是如果可能的话,请提供一些建议以及如何完成以下操作的示例代码。

表格示例

Four of these tables so for the second table I have the id="txtLName1"
    <table id="tblEvent1" class="tblAttendee">
                <tr>
                    <td>Last Name:</td>
                    <td><input id="txtLName" runat="server" type="text" name="name" /></td>
                </tr>
                <tr>
                    <td>First Name:</td>
                    <td><input id="txtFName" runat="server" type="text" name="name" /></td>
                </tr>
                <tr>
                    <td>Gender:</td>
                    <td><asp:DropDownList ID="ddlGender" runat="server" Width="65%">
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td>Age:</td>
                    <td><asp:DropDownList ID="ddlAge" runat="server" Width="65%">
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td>Comments:</td>
                    <td><textarea id="txtComments" runat="server" type="text"></textarea></td>
                </tr>
            </table>

Table1 - visible = true
Table2 - visible = true
Table3 - visible = true
Table4 - visible = false

  using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = con.CreateCommand())
            {

                cmd.CommandText = "StoredProcedureName";
                cmd.CommandType = CommandType.StoredProcedure;

Loop through tables where visible = true
//first loop
cmd.Parameters.AddWithValue("@LastName", Request.Form["(Table1.LastName"]);
insert new record to database
//second loop
cmd.Parameters.AddWithValue("@LastName", Request.Form["Table2.LastName"]);
insert new record to database using same storedprocedure call
//third loop
cmd.Parameters.AddWithValue("@LastName", Request.Form["Table3.LastName"]);
insert new record to database using same storedprocedure call
No 4th loop because Table is not visible.

最后的数据库表应该在其中使用相同的存储过程但是循环,因此在这种情况下将多次调用以插入3条记录

(LastName)Smith (firstName)Bob (gender)Male (age)13 (linkid)1
(LastName)Smith (firstName)Timmy (gender)Male (age)7 (linkid)1
(LastName)Smith (firstName)Jenny (gender)Female (age)9 (linkid)1

1 个答案:

答案 0 :(得分:0)

更改了sql存储过程以接受所有参数,但只有在它们不为空时才插入表中