我有兩個相關的表,第一個稱為postaDip(IDpostaDip, CODdip, CODposta, from),第二個稱為cassPosta(IDposta, Desc)。
我正在嘗試postaDip通過加載表的串列框將多行插入到表中cassPosta。例如,我想插入多行與我在串列框中選擇的 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
uj5u.com熱心網友回復:
你不需要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
uj5u.com熱心網友回復:
你發布了一個很棒的例程。
請記住,串列框中有兩個值。顯示值、值(文本),然后是另一個隱藏值(通常是 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
如果您不允許多行,原始代碼會很好 - 但您確實這樣做了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/325410.html
標籤:sql asp.net sql-server 存储过程
上一篇:[Vue警告]:無效的道具:道具“eventKey”的型別檢查失敗。預期的字串、數字、得到陣列并避免使用非原始值作為鍵
