目前,您的存储过程可能会执行类似的操作(我在这里完全猜测数据类型,并且我假设您使用的是 SQL Server 2008 或更高版本 - 请在提问时指定您的版本!):
CREATE PROCEDURE dbo.pr_Collection_Store
@GetID INT,
@GetTimeMark DATETIME,
@UTCTimeMark DATETIME,
@GetValue INT
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.SomeTable(ID, TimeMark, UTCTimeMark, Value)
SELECT @GetID, @GetTimeMark, @UTCTimeMark, @GetValue;
END
GO
你可以这样做:
CREATE TYPE dbo.Collection AS TABLE
(
GetID INT,
GetTimeMark DATETIME,
UTCTimeMark DATETIME,
GetValue INT
);
GO
CREATE PROCEDURE dbo.pr_Collection_Store2
@Collection AS dbo.Collection READONLY
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.SomeTable(ID, TimeMark, UTCTimeMark, Value)
SELECT GetID, GetTimeMark, UTCTimeMark, GetValue
FROM @Collection AS c
WHERE NOT EXISTS (SELECT 1 FROM dbo.SomeTable WHERE ID = c.GetID);
END
GO
然后从应用程序。对不起,我知道 C#,希望你能把它翻译成 VB.NET。
DataTable tvp = new DataTable();
tvp.Columns.Add(new DataColumn("i"));
foreach(var i in CollectionOutput)
{
tvp.Rows.Add(i.GetID, i.GetTIMEMARK, i.GetTIMEMARK.ToUniversalTime(), i.GetValue);
}
using (connection)
{
SqlCommand cmd = new SqlCommand("dbo.pr_Collection_Store2", connection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvparam = cmd.Parameters.AddWithValue("@Collection", tvp);
tvparam.SqlDbType = SqlDbType.Structured;
connection.Open();
cmd.ExecuteNonQuery();
}