【发布时间】: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 注入的更多信息,因此我回到帖子并为将来的用户重新编写了它。