以下是插入我的数据的功能。
using (SqlCommand insSwipeDataCommand = connection.CreateCommand())
{
insSwipeDataCommand.Transaction = transaction;
insSwipeDataCommand.CommandType = CommandType.StoredProcedure;
insSwipeDataCommand.CommandText = "dbo.insSwipeData_sp";
SqlParameter attendeeTableParam = insSwipeDataCommand.Parameters.Add("@AttendeeTable", SqlDbType.Structured);
attendeeTableParam.Value = this.dataTable;
attendeeTableParam.TypeName = "AttendeeTableType";
// add orgid parameter
insSwipeDataCommand.Parameters.Add("@orgId", SqlDbType.UniqueIdentifier).Value = this.organizationId;
insSwipeDataCommand.ExecuteNonQuery();
}
insSwipeData_sp
create PROC dbo.insSwipeData_sp
(@AttendeeTable AttendeeTableType READONLY
,@orgId UNIQUEIDENTIFIER
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @enteredUserId UNIQUEIDENTIFIER
SET @enteredUserId = 'xxxxxxxxx-xxxx-xxx-xxxx-xxxxxxxxxx'
-- Delete old Swipe records
DELETE FROM dbo.swipeData_tbl
WHERE orgId = @orgId
-- CREATE Swipe Records
INSERT INTO dbo.swipeData_tbl
(orgId, sdid, rawData, enteredUserId, enteredUtc, manualEntry)
SELECT @orgId, attendeeId, barcode
,@enteredUserId, GETUTCDATE(), 0 -- Consider ( datepart , date ) if date here is needed as NVARCHAR
FROM @AttendeeTable
WHERE barcode IS NOT NULL and LTRIM(RTRIM(barcode)) <> '';
END
以下是我的AttendeeTableType
架构的图片。
这是我this.datatable
用于attendeeTableParam
的{{1}}图片
在insSwipeDataCommand.ExecuteNonQuery();
行,我收到以下错误。
表值参数“@AttendeeTable”的数据不符合参数的表类型。
答案 0 :(得分:5)
根据错误,您的数据不完全符合表类型。请注意“完全” - 如果您没有为列指定类型,则会推断它们,并且可以很容易地推断它们。这里最好的方法是创建一个与表类型定义匹配的表:
var dt = new DataTable();
dt.Columns.Add("firstName", typeof(string)).MaxLength = 100;
dt.Columns.Add("lastName", typeof(string)).MaxLength = 100;
dt.Columns.Add("studentId", typeof(string)).MaxLength = 10;
dt.Columns.Add("email", typeof(string)).MaxLength = 100;
dt.Columns.Add("barcode", typeof(string)).MaxLength = 100;
dt.Columns.Add("dob", typeof(string)).MaxLength = 200;
dt.Columns.Add("major", typeof(string)).MaxLength = 200;
dt.Columns.Add("gender", typeof(string)).MaxLength = 200;
dt.Columns.Add("classCode", typeof(string)).MaxLength = 15;
dt.Columns.Add("currentclassCode", typeof(string)).MaxLength = 15;
dt.Columns.Add("entranceCode", typeof(string)).MaxLength = 15;
dt.Columns.Add("attendeeId", typeof(Guid));
然后在需要插入数据时使用.Clone()
创建具有正确模式的新表。这样,如果您的类型或长度不匹配,它将被捕获在客户端。
答案 1 :(得分:3)
我在搜索我遇到的类似问题时到达了此页面,但没有一个回复确实帮助了我。经过一些头部敲击后,我发现在从代码传递的数据表中有一些数据与TVP类型规范不匹配时会产生错误。
例如,如果您定义了以下类型:
CREATE TYPE EmployeeType AS TABLE
(
EmpID BigInt, EmpName VARCHAR(100)
)
并假设您传递的数据表(例如)有一个EmpName
超过100个字符然后&#34; ***不符合表类型&#34;生成错误。
这解决了我的问题。希望它也能帮助别人。
答案 2 :(得分:0)
你的attendeeId
看起来很奇怪。在C#方面必须是Guid
。