【问题标题】:Looping through my CheckBoxList only inserts one record循环通过我的 CheckBoxList 只插入一条记录
【发布时间】:2012-02-15 05:26:00
【问题描述】:

标题说明了我的问题——我的复选框一次只能插入一条记录。模式弹出,我点击 2 个复选框,但只有一个被插入到我的数据库中并显示在我的页面上。我必须一次选中 1 个复选框,而且我有很多很多复选框。这是我的代码。 :) 在此先感谢

<!-- Add a Feature -->
    <li>
        <asp:LinkButton ID="FeatureButton" runat="server">Feature</asp:LinkButton>
        <asp:Panel ID="FeaturePanel" runat="server" CssClass="modalPopup" Style="display:none">
            <asp:CheckBoxList ID="cbxAddFeature" runat="server" DataSourceID="dsNewFeatures" DataTextField="FeatureTitle" DataValueField="FeatureID"></asp:CheckBoxList>
            <asp:Button ID="SubmitFeatures" runat="server" Text="Submit" /><asp:Button ID="CancelSubmitFeatures" runat="server" Text="Cancel" />
        </asp:Panel>
        <asp:ModalPopupExtender ID="FeatureModal" runat="server" BackgroundCssClass="modalBackground" CancelControlID="CancelSubmitFeatures" DropShadow="True" DynamicServicePath="" Enabled="True" PopupControlID="FeaturePanel" TargetControlID="FeatureButton"></asp:ModalPopupExtender>
    </li>

Protected Sub SubmitFeatures_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitFeatures.Click
  FeatureModal.Hide()
    For Each feature As ListItem In cbxAddFeature.Items
      If feature.Selected Then

       'SQL INSERT: Marketing Table
       Dim strSQL As String = "INSERT INTO Marketing (ProductID, MarketingTypeID, MarketingTitle, MarketingData) VALUES (@ProductID, 3, 'Feature', @MarketingData)"

       Using cn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString)

       Using cmd As New SqlCommand(strSQL, cn)

        cmd.Parameters.Add(New SqlParameter("@ProductID", ProductID.Value))
        cmd.Parameters.Add(New SqlParameter("@MarketingData", feature.Value))

        cn.Open()

        cmd.ExecuteNonQuery()
        End Using
       End Using
End If
Next
 Response.Redirect(Request.RawUrl)
End Sub

【问题讨论】:

  • 您的主题不包含实际问题。它唯一说的是您认为复选框可以插入记录,而它们不能。
  • 你不应该这样打开/关闭 sql 连接。这完全是对资源的浪费。打开循环外的连接并在循环完成后关闭它。最坏的情况是,如果没有选中任何复选框,您将浪费连接。否则,您将为选中的每个复选框打开/关闭连接。
  • 是否要将多个insert语句合并到一个字符串中并在1个连接中执行?
  • @MarcB,我已经研究并了解了有关 SQL 注入的更多信息,因此我回到帖子并为将来的用户重新编写了它。

标签: asp.net vb.net checkbox


【解决方案1】:

在循环的第一次通过后不要重定向:

For Each feature As ListItem In cbxAddFeature.Items
        If feature.Selected Then
            Dim sqlAddFeatures As String = Nothing
            'SQL INSERT: Marketing Table
            sqlAddFeatures = "INSERT INTO Marketing (ProductID, MarketingTypeID, MarketingTitle, MarketingData) VALUES (" & ProductID.Value & ",3, 'Feature', " & feature.Value & ")"
            sqlAddFeatures.Replace("'", "''")
            Dim SqlConnection As New SqlConnection("Server=off-db1;uid=productsDB_admin;pwd=*****;database=Products")
            SqlConnection.Open()
            Dim sqlCommand As New SqlCommand(sqlAddFeatures, SqlConnection)
            sqlCommand.ExecuteNonQuery()
            SqlConnection.Close()
            'Response.Redirect(Request.RawUrl) 
        End If
    Next

【讨论】:

  • 我将 response.redirect 拉到了 Next 中,它起作用了。谢谢!
【解决方案2】:

jlg,您将无法在 SQL 中一次性插入所有行。您没有指定使用的 SQL 版本,但在 SQL Server 2008 中,您可以将 DataTable 参数传递给存储过程,然后同时插入所有记录。

这要求您创建一个与您的 DataTable 具有完全相同结构的用户定义数据类型。 Read here.

或者如果你不想走那条路……

You can at least use transactions

【讨论】:

  • 这是SQL Server 2008。我对存储过程基本上一无所知,但复选框现在似乎正在向数据库中添加多条记录。 :) 感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 2018-11-18
  • 2014-09-06
  • 2013-02-03
  • 1970-01-01
  • 2020-09-04
相关资源
最近更新 更多