Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据库问题-外键始终为空(T-SQL)_C#_Sql_Tsql_Ado.net - Fatal编程技术网

C# 数据库问题-外键始终为空(T-SQL)

C# 数据库问题-外键始终为空(T-SQL),c#,sql,tsql,ado.net,C#,Sql,Tsql,Ado.net,我的问题可能有点长,因为我们正在进行的项目有点大,但我会尽量精确 基本上,我们正在开发一个web bases woundmanagement(大学项目的一部分),用户可以在其中输入伤口并设置其他信息,如大小、一致性、上传图片、选择位置等等。 所有这些信息都应该存储在一个数据库中(我们正在与MS SQL Studio和Visual Studio 2017合作),用户以后也可以在该数据库中检索这些信息,以便在模块上查看这些信息 我们现在面临的问题是,如果我们想向用户显示一个特殊伤口的伤口,我们就不能

我的问题可能有点长,因为我们正在进行的项目有点大,但我会尽量精确

基本上,我们正在开发一个web bases woundmanagement(大学项目的一部分),用户可以在其中输入伤口并设置其他信息,如大小、一致性、上传图片、选择位置等等。 所有这些信息都应该存储在一个数据库中(我们正在与MS SQL Studio和Visual Studio 2017合作),用户以后也可以在该数据库中检索这些信息,以便在模块上查看这些信息

我们现在面临的问题是,如果我们想向用户显示一个特殊伤口的伤口,我们就不能让外键工作。 我们可以通过casenumber进行过滤(这是有效的),但我们不能通过伤口的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);
在上述方法之后立即执行

我知道如何在两个表之间创建外键,但是我们尝试的一切都失败了——如果我们尝试使用一个不为NULL的外键集来执行它,我们将得到一个NULL异常

我们尝试的示例:

CONSTRAINT [FK_epadoc_mod_wound_details] FOREIGN KEY ([wound_id])
REFERENCES [dbo].[epadoc_mod_wound_progress] ([progress_id])
如果我们像这样设置外键,它就不起作用了

我们得出的结论是,当执行这两个方法时,调用堆栈肯定会出现问题,但我们不知道如何解决它。 也许我们必须在INSERT查询中将外键设置为显式变量

我们想要实现的是,细节表的伤口id作为进度表的外键,以便稍后可以更改伤口(例如,如果伤口愈合,用户可以重新输入新的大小等)我们可以通过ID过滤,只向患者显示一个伤口,而不是在单击特定伤口时同时显示所有伤口

遗憾的是,我不是大型数据库专家,所以我希望您能按照我的解释:)


谢谢你的帮助

您的
epadoc\u mod\u伤口\u progress
需要包含
[wrand\u id]INT NOT NULL
列。这是你的外键应该建立在这样一个伤口可以有很多伤口进展。然后,在insert语句中,将在
woundDetail
表insert中生成的
wound\u id
插入
epadoc\u mod\u伤口\u progress
您的
epadoc\u mod\u伤口\u progress
需要包含
[wound\u id]INT NOT NULL
列。这是你的外键应该建立在这样一个伤口可以有很多伤口进展。然后,在insert语句中,将在
woundDetail
表insert中生成的
wound\u id
插入
epadoc\u mod\u-wound\u progress
尝试添加注释,但我没有50个声誉

从我所看到的情况来看,我假设您正试图在“主表”和“epadoc\u mod\u wrand\u progress”表之间实现一对多的关系,对吗

如果是这样,那么在“epadoc\u mod\u伤口\u progress”表中似乎没有存储伤口\u id的字段,如果没有存储伤口\u id,您如何尝试创建外键


建议epadoc_mod_伤口_进度表的主键是伤口_id和进度_id的连接键,伤口_id也是链接到主表的外键。

尝试添加注释,但我没有50信誉

从我所看到的情况来看,我假设您正试图在“主表”和“epadoc\u mod\u wrand\u progress”表之间实现一对多的关系,对吗

如果是这样,那么在“epadoc\u mod\u伤口\u progress”表中似乎没有存储伤口\u id的字段,如果没有存储伤口\u id,您如何尝试创建外键


建议epadoc_mod_伤口_进度表的主键是伤口_id和进度_id的连接键,伤口_id也是链接到主表的外键。

在表
epadoc_mod_伤口_进度中
必须有
伤口_id INT NOT NULL
列作为外键

此外,必须将约束添加到外键表,即1到n关系的n侧的表。假设主表的名称是
epadoc\u mod\u wrand\u details
(您没有显示它):


此外,通过在删除级联中添加
,当您删除伤口细节时,伤口细节的进度将自动删除。

在表
epadoc\u mod\u伤口进度中
必须有一个
伤口id INT NOT NULL
列作为外键

此外,必须将约束添加到外键表,即1到n关系的n侧的表。假设主表的名称是
epadoc\u mod\u wrand\u details
(您没有显示它):

此外,通过在删除级联中添加
在删除伤口细节时,伤口细节的进度将自动删除。

删除所有隐藏错误的
catch{}
块。现在,您不知道这些语句是否成功或抛出错误<代码>捕获{}
不会删除错误或使您
ALTER TABLE dbo.epadoc_mod_wound_progress
ADD CONSTRAINT FK_progress_details FOREIGN KEY (wound_id)
REFERENCES dbo.epadoc_mod_wound_details (wound_id)
ON DELETE CASCADE