c# - 使用 SqlDatRecord 结果列表插入行 数据库中的外键错误
问题描述
我正在尝试使用 #SqlDataRecord 和 #MetaData 的技术将记录列表插入数据库。在一切正常的情况下,我遇到了外键约束的问题:
首先如您所知,我们在 sql 中创建 UserType:
USE [db_Visitors]
GO
/****** Object: UserDefinedTableType [dbo].[type_PathType] Script Date: 9/7/2018 10:14:29 AM ******/
DROP TYPE [dbo].[type_PathType]
GO
/****** Object: UserDefinedTableType [dbo].[type_PathType] Script Date: 9/7/2018 10:14:29 AM ******/
CREATE TYPE [dbo].[type_PathType] AS TABLE(
[GateID] [int] NULL,
[VisitRequestID] [int] NULL,
[VisitorID] [int] NULL,
[Confirmation] [bit] NULL,
[Finished] [bit] NULL
)
GO
第二:我们创建存储过程:
USE [db_Visitors]
GO
/****** Object: StoredProcedure [dbo].[AddTypePathList] Script Date: 9/7/2018 9:16:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[AddTypePathList]
@TBL_PathType type_PathType READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO TBL_Path
(GateID , VisitRequestID , VisitorID , Confirmation , Finished)
SELECT
pat.GateID ,
pat.VisitRequestID ,
pat.VisitorID ,
pat.Confirmation ,
pat.Finished FROM @TBL_PathType pat
END
第三::后端数据访问:创建 SqlDataRecords 列表:
private List<SqlDataRecord> SqlList_TypePath(List<TypePath> PathTypeList)
{
List<SqlDataRecord> SqlDataRecordList = new List<SqlDataRecord>();
SqlMetaData[] metadata = new SqlMetaData[5];
metadata[0] = new SqlMetaData("GateID", SqlDbType.Int);
metadata[1] = new SqlMetaData("VisitRequestID", SqlDbType.Int);
metadata[2] = new SqlMetaData("VisitorID", SqlDbType.Int);
metadata[3] = new SqlMetaData("Confirmation", SqlDbType.Bit);
metadata[4] = new SqlMetaData("Finished", SqlDbType.Bit);
foreach (TypePath path in PathTypeList)
{
SqlDataRecord record = new SqlDataRecord(metadata);
TypePath pathType = new TypePath();
record.SetInt32(0, pathType.GateID);
record.SetInt32(1, pathType.VisitRequestID);
record.SetInt32(2, pathType.VisitorID);
record.SetBoolean(3, pathType.Confirmation);
record.SetBoolean(4, pathType.Finished);
SqlDataRecordList.Add(record);
}
return SqlDataRecordList;
}
第四:连接-命令-参数-执行:
private void _Command_AddTypePath(List<TypePath> PathTypeList)
{
string procedureName = "AddTypePathList";
string ConnectionString = ConfigurationManager.ConnectionStrings["VisitorConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand command = new SqlCommand(procedureName, connection);
command.CommandType = CommandType.StoredProcedure;
if (connection.State != ConnectionState.Open)
{
connection.Close();
connection.Open();
}
var parameter = new SqlParameter("@TBL_PathType", SqlDbType.Structured);
parameter.TypeName = "type_PathType";
parameter.Value = List_TypePath(PathTypeList);
command.Parameters.Add(parameter);
command.ExecuteNonQuery();
}
我检查了 sql 列表中的记录,它们都存在。我已经检查了表中存在的所有外键 ID ** 但它保留了外键问题 **
第五:表:TBL_Path 包含:
[PathID]
,[VisitRequestID] : Primary
,[GateID] : Foreign
,[DateOfEntrance]
,[DateOfLeave]
,[Finished]
,[CardID] : Foreign
,[VisitorID] : Foreign
,[Confirmation]
,[Remarks]
FROM [dbo].[TBL_Path]
最后:我已经放弃了外键的约束。行被插入,但使用默认值,而不是实际值。问题出在 c# 代码内部。我附上了一个屏幕截图,让您看到列表已正确填写
解决方案
推荐阅读
- angular - *ngIf 条件进入无限循环
- python - 玩家碰撞检测
- android - 多行通知标题
- c# - tid 13929 (.AppTest.Project1) 中的致命信号 6 (SIGABRT),代码 -6 (SI_TKILL),pid 13929 (.AppTest.Project1)
- java - 如何发送终端命令
- http - Hyper 0.12.x:为结构实现服务
- ios - 通过在 swift 中修改匹配的字符串来使用正则表达式替换匹配项
- html - css选择器,两个类之间的元素
- jquery - 如何从 HTML 获取 json 数据到 Nodejs 服务器
- arrays - VBA将许多单元格合二为一,数组+四舍五入