c# - 数据库问题 - 外键始终为空 (T-SQL)
问题描述
我的问题描述起来可能有点长,因为我们正在进行的项目有点大,但我会尽量做到精确。
基本上,我们正在开发一个基于网络的伤口管理(大学项目的一部分),用户可以在其中输入伤口并设置其他信息,如大小、一致性、上传图片、选择位置……。所有这些信息都应存储在数据库中(我们正在使用 MS SQL Studio 和 Visual Studio 2017),用户也可以稍后检索它以在模块上查看它。
我们现在面临的问题是,如果我们想向用户显示一个特殊伤口的伤口,我们无法让外键工作。我们可以通过病例编号进行过滤(这是有效的),但我们不能通过伤口的 ID 过滤伤口信息(每个伤口都有一个唯一的 ID) - 所以如果我们选择一个伤口,我们仍然会得到关于所有伤口的信息为给定的 casenr 存储。
这是我们的“主表”,其中每个伤口都有一个唯一的 ID,它也是一个升序标识列:
[wound_id] INT IDENTITY (1, 1) NOT NULL,
[wound_type] VARCHAR (500) NULL,
[wound_description] VARCHAR (500) NULL,
[decuGrade] INT NULL,
[wound_comments] VARCHAR (500) NULL,
[wound_timeReal] DATETIME NULL,
[wound_timeGiven] DATETIME NULL,
[casenumber] INT NULL,
[username] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([wound_id] ASC)
);
如果用户输入信息并单击“下一步”,则会在代码中调用一个函数来填充表格:
_db.SaveWoundDetails(casenr, woundValue, decu, additional_info, realTime, givenBackDocDate, user);
这导致了我们的数据库类,在这里我们对数据库进行了查询,在这种情况下:
public void SaveWoundDetails(int casenr, string woundType, int decuGrade, string woundComment, DateTime timeReal, DateTime timeGiven , string user)
{
var table = ConfigurationManager.AppSettings["woundDetailsTable"];
var insertQuery = "INSERT INTO " + table + "(casenumber, wound_type, decuGrade, wound_comments, wound_timeReal, wound_timeGiven, username) VALUES (@casenr, @woundType, @decuGrade, @woundComment, @timeReal, @timeGiven, @user)";
var cmd = new SqlCommand(insertQuery);
cmd.Parameters.AddWithValue("@casenr", casenr);
cmd.Parameters.AddWithValue("@woundType", woundType);
cmd.Parameters.AddWithValue("@decuGrade", decuGrade);
cmd.Parameters.AddWithValue("@woundComment", woundComment);
cmd.Parameters.AddWithValue("@timeReal", timeReal);
cmd.Parameters.AddWithValue("@timeGiven", timeGiven);
cmd.Parameters.AddWithValue("@user", user);
var db = DatabaseController.getDataBaseController();
try
{
var sqlcmd = db.executeSQL(cmd);
}
catch (SqlException e)
{
}
}
连接等位于目前不相关的数据库处理程序类中。
直到这里它工作正常。但现在我们有第二个表格,可以了解更多关于伤口的信息,下次点击时也会填写,与此表格相关:
CREATE TABLE [dbo].[epadoc_mod_wound_progress] (
[progress_id] INT IDENTITY (1, 1) NOT NULL,
[wound_length] INT NULL,
[wound_width] INT NULL,
[wound_depth] INT NULL,
[wound_surrounding] VARCHAR (500) NULL,
[wound_consistence] VARCHAR (500) NULL,
[wound_state] VARCHAR (200) NULL,
[wound_painscale] VARCHAR (MAX) NULL,
[wound_itch] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([progress_id] ASC)
使用插入方法:
public void SaveWoundProgress(int woundLength, int woundWidth, int woundDepth, string woundSurrounding, string woundConsistence, string woundState, string woundPainScale, string woundItch)
{
var table = ConfigurationManager.AppSettings["woundProgressTable"];
var insertQuery = "INSERT INTO " + table + "(wound_length,wound_width,wound_depth, wound_surrounding, wound_consistence, wound_state, wound_painscale, wound_itch) VALUES (@woundLength, @woundWidth, @woundDepth, @woundSurrounding, @woundConsistence, @woundState, @woundPainScale, @woundItch)";
var cmd = new SqlCommand(insertQuery);
cmd.Parameters.AddWithValue("@woundLength", woundLength);
cmd.Parameters.AddWithValue("@woundWidth", woundWidth);
cmd.Parameters.AddWithValue("@woundDepth", woundDepth);
cmd.Parameters.AddWithValue("@woundSurrounding", woundSurrounding);
cmd.Parameters.AddWithValue("@woundConsistence", woundConsistence);
cmd.Parameters.AddWithValue("@woundState", woundState);
cmd.Parameters.AddWithValue("@woundPainScale", woundPainScale);
cmd.Parameters.AddWithValue("@woundItch", woundItch);
var db = DatabaseController.getDataBaseController();
try
{
var sqlcmd = db.executeSQL(cmd);
}
catch (SqlException e)
{
}
}
以及方法
_db.SaveWoundProgress(wound_length, wound_width, wound_depth, woundArea, woundEdge, woundStatus, painStatus, itchStatus);
这是在上面提到的方法之后立即执行的。
我知道如何在两个表之间创建外键,但是我们尝试的一切都失败了——如果我们尝试使用一个非空的外键集来执行它,我们就会得到一个空异常。
我们尝试的示例:
CONSTRAINT [FK_epadoc_mod_wound_details] FOREIGN KEY ([wound_id])
REFERENCES [dbo].[epadoc_mod_wound_progress] ([progress_id])
如果我们像这样设置一个外键,它就不起作用了。
我们得出的结论是,执行这两个方法时调用堆栈一定有问题 - 但我们不知道如何解决它。也许我们必须将插入查询中的外键设置为显式变量?
我们想要实现的是将详细信息表的伤口 ID 作为进度表的外键,以便以后可以更改伤口(例如,如果伤口愈合,用户可以重新输入新尺寸等)如果单击特定伤口,我们可以按 ID 过滤以仅向患者显示一个伤口,而不是同时显示所有伤口。
遗憾的是,我不是大型数据库专家,所以我希望你能听从我的解释:)。
谢谢你的帮助!
解决方案
您epadoc_mod_wound_progress
需要包含一[wound_id] INT NOT NULL
列。这就是你的外键应该建立的,这样一个伤口可以有很多伤口进展。然后,在您的插入语句中,您将在表插入wound_id
中生成的插入到.woundDetail
epadoc_mod_wound_progress
推荐阅读
- excel - 如何旋转图像并将其移动到特定位置?
- android - 如何从 android.googlesource.com 导入 Android Studio 项目?
- java - 如何在休眠验证器引擎中指定时间范围?
- php - 路由 [verification.resend] 未定义
- node.js - 如何修复:“错误 fsevents@2.0.7:平台“linux”与此模块不兼容。”
- javascript - axios 调用从第一个 api 获取响应并传递到第二个 api
- c++ - 如何在 OpenGL 窗口中获取 Unity 上下文
- java - 找到“消息:”后如何停止正则表达式?
- php - php中SQL等效的NOT IN运算符
- php - 为什么我的 sql 语句适用于命令行,但不适用于 PHP 上的 mysqli_query?