Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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# SQLDataReader不识别空列_C#_Sqldatareader_Dbnull - Fatal编程技术网

C# SQLDataReader不识别空列

C# SQLDataReader不识别空列,c#,sqldatareader,dbnull,C#,Sqldatareader,Dbnull,因此,我有一个函数,它读取查询的结果,稍后在程序中使用,如下所示: connection.Open(); int combination; using (SqlCommand com1 = new SqlCommand()) { com1.Connection = connection; com1.CommandText = "select FinalComboId from relationTable where sourceCombo=@source and destina

因此,我有一个函数,它读取查询的结果,稍后在程序中使用,如下所示:

connection.Open();
int combination;
using (SqlCommand com1 = new SqlCommand())
{
    com1.Connection = connection; 
    com1.CommandText = "select FinalComboId from relationTable where sourceCombo=@source and destinationCombo=@destination";
    com1.Parameters.Add(new SqlParameter("@source",combo.ToString() ?? ""));
    com1.Parameters.Add(new SqlParameter("@destination", destination ?? ""));
    SqlDataReader comboLinkReader = com1.ExecuteReader();
    if (!comboLinkReader.Read() || comboLinkReader.FieldCount==0)
    {
        ScriptManager.RegisterClientScriptBlock(this, GetType(),
                   "alertMessage", @"alert('Combination does not exists,please contact admin!')", true);
    }
    else 
    {
        combination = Convert.ToInt32(comboLinkReader["FinalComboId"]);

    }
}
我想要实现的是:如果结果为空,则执行警报脚本,否则将结果保存为整数,这将用于进一步的计算。我已经学习了几个关于这个问题的教程和示例,当我前几天执行这个函数时,它工作得很好。现在,从发布到生产的2小时内,它不计算第一个条件:

if (!comboLinkReader.Read() || comboLinkReader.FieldCount==0)
       {//calculations  here}
我也试过了

if (!comboLinkReader.Read() || comboLinkReader.IsDbNull(0))
   {//calculations}
我也有同样的问题。查询应返回一个值。 有什么地方我做错了吗?

是一种需要列索引参数的方法

int? finalComboId = null;
if(comboLinkReader.Read() && !comboLinkReader.IsDbNull(0))
    finalComboId = comboLinkReader.GetInt32(0);
if(!finalComboId.HasValue)
    ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", @"alert('Combination does not exists,please contact admin!')", true);
else 
    combination = finalComboId.Value;
是一个需要列索引参数的方法

int? finalComboId = null;
if(comboLinkReader.Read() && !comboLinkReader.IsDbNull(0))
    finalComboId = comboLinkReader.GetInt32(0);
if(!finalComboId.HasValue)
    ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", @"alert('Combination does not exists,please contact admin!')", true);
else 
    combination = finalComboId.Value;
是一个需要列索引参数的方法

int? finalComboId = null;
if(comboLinkReader.Read() && !comboLinkReader.IsDbNull(0))
    finalComboId = comboLinkReader.GetInt32(0);
if(!finalComboId.HasValue)
    ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", @"alert('Combination does not exists,please contact admin!')", true);
else 
    combination = finalComboId.Value;
是一个需要列索引参数的方法

int? finalComboId = null;
if(comboLinkReader.Read() && !comboLinkReader.IsDbNull(0))
    finalComboId = comboLinkReader.GetInt32(0);
if(!finalComboId.HasValue)
    ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", @"alert('Combination does not exists,please contact admin!')", true);
else 
    combination = finalComboId.Value;



IsDbNull
是一种需要列索引参数的方法。@TimSchmelter是的,我正在编辑问题,忘记了索引实际发生了什么?你怎么知道没有结果?你调试代码了吗?如果
Read
在您没有预料到的情况下返回true,那么它加载的行的内容是什么?没有理由计算字段数或检查空值(除非在此字段中存储了空值)。理论上,如果查询结果为空或没有值,则应触发警报脚本,否则应返回值。实际发生的情况是,有时第一个条件没有执行,尽管查询没有结果,但event
IsDbNull
是一个需要列索引参数的方法。@TimSchmelter是的,我正在编辑问题,忘记了索引实际发生了什么?你怎么知道没有结果?你调试代码了吗?如果
Read
在您没有预料到的情况下返回true,那么它加载的行的内容是什么?没有理由计算字段数或检查空值(除非在此字段中存储了空值)。理论上,如果查询结果为空或没有值,则应触发警报脚本,否则应返回值。实际发生的情况是,有时第一个条件没有执行,尽管查询没有结果,但event
IsDbNull
是一个需要列索引参数的方法。@TimSchmelter是的,我正在编辑问题,忘记了索引实际发生了什么?你怎么知道没有结果?你调试代码了吗?如果
Read
在您没有预料到的情况下返回true,那么它加载的行的内容是什么?没有理由计算字段数或检查空值(除非在此字段中存储了空值)。理论上,如果查询结果为空或没有值,则应触发警报脚本,否则应返回值。实际发生的情况是,有时第一个条件没有执行,尽管查询没有结果,但event
IsDbNull
是一个需要列索引参数的方法。@TimSchmelter是的,我正在编辑问题,忘记了索引实际发生了什么?你怎么知道没有结果?你调试代码了吗?如果
Read
在您没有预料到的情况下返回true,那么它加载的行的内容是什么?没有理由计算字段数或检查空值(除非在此字段中存储了空值)。理论上,如果查询结果为空或没有值,则应触发警报脚本,否则应返回值。实际发生的情况是,有时第一个条件没有执行,尽管查询没有结果,但在这段代码中,comboLinkReader.Read()显示空结果(它应该显示空结果)。但是,即使脚本被命中(在放置断点之后),它也不会出现在屏幕上。也许我遗漏了一些条件?什么意思是“comboLinkReader.Read()显示空结果”?您知道必须向客户端显示页面才能查看
警报
?您当前在服务器上,
警报
是一个javascript函数,在加载文档时执行。如果我在comboLinkReader上放置一个断点并打开“属性”,它会在结果视图属性中显示:Empty“Enumeration generated no results”@Ange1:哪个属性显示了这一点?不清楚出了什么问题。你到底在哪里设置了断点?你认为我应该把警报放在客户端而不是服务器端吗?我还有一些在服务器端生成脚本的页面,它们都工作得很好。断点位于'finalComboId=comboLinkReader.GetInt32(0);',为了使用这段代码查看comboLinkReader值,comboLinkReader.Read()会显示空结果(应该是这样)。但是,即使脚本被命中(在放置断点之后),它也不会出现在屏幕上。也许我遗漏了一些条件?什么意思是“comboLinkReader.Read()显示空结果”?您知道必须向客户端显示页面才能查看
警报
?您当前在服务器上,
警报
是一个javascript函数,在加载文档时执行。如果我在comboLinkReader上放置一个断点并打开“属性”,它会在结果视图属性中显示:Empty“Enumeration generated no results”@Ange1:哪个属性显示了这一点?不清楚出了什么问题。你到底在哪里设置了断点?你认为我应该把警报放在客户端而不是服务器端吗?我还有一些在服务器端生成脚本的页面,它们都工作得很好。断点位于'finalComboId=comboLinkReader.GetInt32(0);',为了使用这段代码查看comboLinkReader值,comboLinkReader.Read()会显示空结果(应该是这样)。但是,即使脚本被命中(在放置断点之后),它也不会出现在屏幕上。也许我遗漏了一些条件?什么是“comboLinkRead”