【问题标题】:Insert multiple values from ListBox with stored procedure使用存储过程从 ListBox 插入多个值
【发布时间】:2021-12-06 06:51:41
【问题描述】:

我有两个相关的表,第一个名为 postaDip (IDpostaDip, CODdip, CODposta, from),第二个名为 cassPosta (IDposta, Desc)。

我正在尝试通过加载表cassPosta 的列表框将多行插入postaDip 表。例如,我想插入与我在列表框中选择的 ID 一样多的多行。使用此代码我正在尝试,如果我在列表框中只选择一项,相同的一项,它会重复插入 2 次。如果我选择多个元素,则只输入一个,两次!

标记:

<asp:DropDownList ID="sel_dip" CssClass="chzn-select" Width="50%" 
     runat="server" DataSourceID="SqlDataSource1" 
     DataTextField="nomecogn" DataValueField="IDdipendenti" 
     ValidateRequestMode="Enabled">
    <asp:ListItem Text="Seleziona Dip" Value=""></asp:ListItem>
</asp:DropDownList>

<asp:TextBox runat="server" id="sel_data" CssClass="form-control" clientidmode="static" Width="20%" ></asp:TextBox>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" AppendDataBoundItems="true" DataSourceID="SqlDataSource2" DataTextField="Desc" DataValueField="IDposta" ></asp:ListBox>
<asp:button ID="btnAssPc" runat="server" OnClick="btnAssPc_Click"/>

后面的代码:

Protected Sub ass_postaDip()

  For Each selectedItem As Object In ListBox1.SelectedValue

            Dim cmdText As String = "Sp_ass_postaDip2"
            Dim postaid As Integer = Int32.Parse(selectedItem.ToString())
    
            Using Myconnection As New SqlConnection(SqlContConnStrinG), command As New SqlCommand(cmdText, Myconnection), da As New SqlDataAdapter(command)

                Myconnection.Open()
                command.CommandType = CommandType.StoredProcedure
                command.Parameters.Add("@CodDip", SqlDbType.Int).Value = sel_dip.Text
                command.Parameters.Add("@CodPosta", SqlDbType.Int).Value = postaid
                command.Parameters.Add("@Data", SqlDbType.Date).Value = sel_data.Text
                command.ExecuteNonQuery()
                Dim dst As New DataSet
                da.Fill(dst)

                Myconnection.Close()
            End Using
        Next   
    End Sub

这是存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Sp_ass_postaDip2] 
    @CodDip int,
    @CodPosta int,
    @Data date
AS
BEGIN
    SET NOCOUNT OFF;

    INSERT INTO postaDip (CODdip, CODposta, from)
    VALUES (@CodDip, @CodPosta, @Data);
END

【问题讨论】:

  • json 上设置值并将 jason 传递给 SP,在 SP 中使用 openjson 并选择将数据插入表中
  • 谢谢@Amirhossein,我从来没有使用过openjson函数,你能更好地解释一下怎么做吗?
  • 尝试使用列表并将其发送到SQL服务器并通过批量插入插入到数据库中。
  • 旁注:您应该为您的存储过程使用sp_ 前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免 sp_ 并使用其他东西作为前缀 - 或者根本不使用前缀!
  • 你为什么有SET NOCOUNT OFF

标签: sql asp.net sql-server stored-procedures


【解决方案1】:

你发布了一个很棒的例程。

请记住,列表框中有两个值。显示值、值(文本),然后是其他隐藏值(通常是 PK 值)。

只需 STRONG TYPE 发布的例程,您就会得到:

        selectedItem.Text
        selectedItem.Value

所以:

    For Each selectedItem As ListItem In ListBox1.Items

        If selectedItem.Selected Then

            Dim cmdText As String = "Sp_ass_postaDip2"

            Using Myconnection As New SqlConnection(SqlContConnStrinG), command As New SqlCommand(cmdText, Myconnection)
                command.CommandType = CommandType.StoredProcedure
                command.Parameters.Add("@CodDip", SqlDbType.Int).Value = sel_dip.Text
                command.Parameters.Add("@CodPosta", SqlDbType.Int).Value = selectedItem.Value
                command.Parameters.Add("@Data", SqlDbType.Date).Value = sel_data.Text
                Myconnection.Open()
                command.ExecuteNonQuery()

            End Using
        End If

    Next

所以,请注意如何获取/获取 .Value 或 .Text

如果您不允许多行,原始代码会很好 - 但您可以这样做。

【讨论】:

  • 非常感谢我明白你的意思,通过你的更正,代码可以完美运行。
【解决方案2】:

这里不需要DataAdapter,它只是导致程序再次执行。您也不需要关闭连接,因为 Using 会处理该问题

Protected Sub ass_postaDip()

  For Each selectedItem As Object In ListBox1.SelectedValue

            Dim cmdText As String = "Sp_ass_postaDip2"
            Dim postaid As Integer = Int32.Parse(selectedItem.ToString())
    
            Using Myconnection As New SqlConnection(SqlContConnStrinG), command As New SqlCommand(cmdText, Myconnection)

                command.CommandType = CommandType.StoredProcedure
                command.Parameters.Add("@CodDip", SqlDbType.Int).Value = sel_dip.Text
                command.Parameters.Add("@CodPosta", SqlDbType.Int).Value = postaid
                command.Parameters.Add("@Data", SqlDbType.Date).Value = sel_data.Text
                Myconnection.Open()
                command.ExecuteNonQuery()
            End Using
        Next   
    End Sub

【讨论】:

  • 感谢您的回复,我更正了代码,我收到此错误“从 Char 类型到整数类型的无效转换”
  • 抱歉现在修复了,改回原来的代码。我以为已经是Integer
  • 对不起,插入有效,对您来说这很荒谬,但我注意到它通常与我在列表框中选择的内容不对应!!!无论是单选还是多选,条目都不匹配。错误可能在“ListBox1.SelectedValue”中?
  • 试试SelectedItem吧?
  • with SelectedItem 错误是“表达式属于 ListItem 类型,它不是集合类型”
猜你喜欢
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多