简体   繁体   English

如何检测数据库SQL LINQ C#中何时没有值

[英]How to detect when there is no value from database SQL LINQ C#

I'm having trouble with detecting when my value in C# code is null or not, ( I'm using LINQ to call stored procedures from SQL SERVER ), I mean I have issues when I want to determine did SQL returned something to me after I called SP which expecting one parameter (ArticleID) or acctualy SQL did not return anything ( null - row does not exist ).我在检测 C# 代码中的值是否为空时遇到问题(我正在使用 LINQ 从 SQL SERVER 调用存储过程),我的意思是当我想确定 SQL 是否在之后向我返回了某些内容时遇到了问题我调用了 SP,它期望有一个参数 (ArticleID) 或实际 SQL 没有返回任何内容(空行不存在)。

In fact, I want to check does any of my articles has some "Sub articles", and I'm passing ArticleID from C# code to stored procedure.事实上,我想检查我的任何文章是否有一些“子文章”,并且我正在将 ArticleID 从 C# 代码传递到存储过程。

So In case if article is there, I want to do something with it, in case article with passed ArticleID does not exist I want to do something also, so as I said I need to detect does my article with given ArticleID exist in my sql database.因此,如果文章在那里,我想用它做一些事情,如果通过的文章 ID 的文章不存在,我也想做一些事情,所以正如我所说,我需要检测我的 sql 中是否存在给定文章 ID 的文章数据库。

Here is some of my code:这是我的一些代码:

private void btnCheckForArticle(object sender, RoutedEventArgs e)
{
        try
        {  
        if (gridArticles.SelectedItems.Count > 0) {
            Article a = (Article)gridArticles.SelectedItem;



                if (a.ArticleID != null)
                {

                    var existingArticle = DataServices.POS.proc_GetSubArticleByArticleID(a.ArticleID);
                    if (existingArticle != null)
                    {
                       //DO SOMETHING
                        return;
                    }
              }
        }
    }
}

My stored procedure :我的存储过程:

ALTER PROCEDURE [dbo].[proc_GetSubArticleByArticleID]
(
    @ArticleID int
)
AS
BEGIN
Select *
From SubArticles as S
Where S.ArticleID = @ArticleID
END

It's interesting that I do not know how to detect in c# code is my value null or not, because obliviosuly even if there is no row in sql database still I'm not getting null, and because of that code below existingArticle != null will allways execute..有趣的是,我不知道如何在 c# 代码中检测我的值是否为空,因为即使 sql 数据库中没有行,我仍然没有得到空值,并且因为在existingArticle != null下方的代码existingArticle != null会总是执行..

Now I will post what is happening with article which for sure does not have any subarticles, I mean, where result should be null 100% !现在我将发布文章中发生的事情,该文章肯定没有任何子文章,我的意思是,结果应该是 null 100%

在此处输入图片说明

ID that I passed to procedure is 2351, so I executed sp which should return value directly on sql and I replaced @ArticleID with 2351 and ofcourse It did not return me any results, but how can I notice that in my C# code..我传递给过程的 ID 是 2351,所以我执行了应该直接在 sql 上返回值的 sp,我用 2351 替换了 @ArticleID,当然它没有返回任何结果,但是我怎么能注意到在我的 C# 代码中..

because existingArticle will never be null cuz somehow it allways has some value and code below my if (existingArticle != null) will allways execute.因为existingArticle永远不会为空,因为它总是有一些值,并且低于我的if (existingArticle != null)将始终执行。 what I really dont want :/ ...我真的不想要:/ ...

Thanks guys!谢谢你们! Cheers干杯

Why don't you call FirstOrDefault()你为什么不调用 FirstOrDefault()

var existingArticle = DataServices.POS.proc_GetSubArticleByArticleID(a.ArticleID).FirstOrDefault();

if (existingArticle != null)
{
    // 
    return;
}

There is a special Datatype for checking null values from a database.有一种特殊的数据类型用于检查数据库中的空值。

DBNull.Value

In your case:在你的情况下:

if (existingArticle != DBNull.Value)
{
   //DO SOMETHING
   return;
}

First, modify your SP to have after BEGIN :首先,将您的 SP 修改为在BEGIN之后:

SET NOCOUNT ON

This is to avoid interference of "xx rows affected" message.这是为了避免干扰“xx 行受影响”消息。

Seccond, dont do SELECT * , instead use第二,不要做SELECT * ,而是使用

SELECT 1 From SubArticles as S
Where S.ArticleID = @ArticleID

Last, check for System.DBNull.Value instead of NULL, as this is what will be returned from DDBB.最后,检查System.DBNull.Value而不是 NULL,因为这是将从 DDBB 返回的内容。 This is because in .Net value types as Boolean can't be null这是因为在 .Net 值类型中,布尔值不能为空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM