繁体   English   中英

使用存储过程从 ListBox 插入多个值

[英]Insert multiple values from ListBox with stored procedure

我有两个相关的表,第一个称为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

这里不需要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

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

请记住,列表框中有两个值。 显示值、值(文本),然后是另一个隐藏值(通常是 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM