我在SQL Server 2008中有这个代码,但是我尝试从我的vb.net应用程序中插入数据 - 我该怎么做?
CREATE procedure [dbo].[PO_INSERT_WITH_LINE_ITEM]
(@PO_ID smallint = null output,
@SUPPLIER_ID smallint,
@CREATED_BY smallint,
@CREATION_DATE date,
@POD dbo.POD_UDT READONLY)
AS
BEGIN
SET XACT_ABORT, NOCOUNT ON
BEGIN TRAN;
INSERT INTO dbo.tbl_purchases_order (SUPPLIER_ID, CREATED_BY, CREATION_DATE, PO_TOTAL)
SELECT
@SUPPLIER_ID, @CREATED_BY, @CREATION_DATE,
PO_TOTAL = SUM(TOTAL_COST)
FROM
@POD i;
SELECT @PO_ID = CONVERT(SMALLINT, SCOPE_IDENTITY());
INSERT INTO dbo.tbl_pucheases_order_detail (PO_ID,PRODUCT_ID, DISCOUNT, UNIT_COST, QTY, TOTAL_BEFORE_DISCOUNT, DISCOUNTED_AMOUNT, TOTAL_COST)
SELECT
@PO_ID, PRODUCT_ID, DISCOUNT, UNIT_COST, QTY,
TOTAL_BEFORE_DISCOUNT, DISCOUNTED_AMOUNT, TOTAL_COST
FROM
@POD i;
COMMIT TRAN;
END;
那么我可以在Vb.net中从ListView插入多行吗?
感谢
答案 0 :(得分:1)
根据the documentation,您可以将TVP作为DataTable
,DbDataReader
或IEnumerable<SqlDataRecord>
传递。下面是一个DataTable
示例,它为每个列表视图项添加一行,为每个子项添加一列。更改类型以匹配您的实际表类型。
Dim dt = New DataTable
dt.Columns.Add("PO_ID", GetType(Int32))
dt.Columns.Add("PRODUCT_ID", GetType(Int32))
dt.Columns.Add("DISCOUNT", GetType(Decimal))
dt.Columns.Add("UNIT_COST", GetType(Decimal))
dt.Columns.Add("QTY", GetType(Int32))
dt.Columns.Add("TOTAL_BEFORE_DISCOUNT", GetType(Decimal))
dt.Columns.Add("DISCOUNTED_AMOUNT", GetType(Decimal))
dt.Columns.Add("TOTAL_COST", GetType(Decimal))
Dim columnValues(dt.Columns.Count - 1) As Object
For Each lvi As ListViewItem In ListView1.Items
columnValues(0) = Int32.Parse(lvi.SubItems(0).Text)
columnValues(1) = Int32.Parse(lvi.SubItems(1).Text)
columnValues(2) = Decimal.Parse(lvi.SubItems(2).Text)
columnValues(3) = Decimal.Parse(lvi.SubItems(3).Text)
columnValues(4) = Int32.Parse(lvi.SubItems(4).Text)
columnValues(5) = Decimal.Parse(lvi.SubItems(5).Text)
columnValues(6) = Decimal.Parse(lvi.SubItems(6).Text)
columnValues(7) = Decimal.Parse(lvi.SubItems(7).Text)
dt.Rows.Add(columnValues)
Next
Using connection As New SqlConnection(connectionString)
Using command As New SqlCommand("dbo.PO_INSERT_WITH_LINE_ITEM", connection)
command.CommandType = CommandType.StoredProcedure
connection.Open()
'add other parameters here
'add TVP parameter
Dim tvpParameter = command.Parameters.Add("@tvp", SqlDbType.Structured)
tvpParameter.Value = dt
command.ExecuteNonQuery()
'get output parameter value here
End Using
End Using