gpt4 book ai didi

c# - bool doesExist = command.ExecuteScalar() != null 由于某种原因不断评估为真

转载 作者:行者123 更新时间:2023-11-30 21:03:07 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
ExecuteScalar returns null or DBNull (development or production server)

我有一个存储过程,用于检查预先存在的文件 ID 是否与某个项目相关联。如果 select 语句返回值,它应该为 true 并将“true”分配给 bool。然而,当 select 语句返回 null 因为它不存在时,我后面的代码仍然使 .Execute 返回“true”

这是我的存储过程:

ALTER PROCEDURE [dbo].[Events_TaskIDExists] 
@EventID int
AS
BEGIN
select TaskID from Events where EventID = @EventID
END

下面是我的代码:

public void hasTaskAssociatedToNote()
{
String[] Notes = hidSelectedEventIDs.Value.Split(',');
bool exists = false;
foreach (var note in Notes)
{
var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
var command = new SqlCommand("Events_TaskIDExists", connection);
command.Parameters.Add(new SqlParameter("@EventID", SqlDbType.Int));
command.Parameters["@EventID"].Value = Convert.ToInt32(note.Trim());
command.CommandType = CommandType.StoredProcedure;
try
{
connection.Open();
exists = command.ExecuteScalar() != null;//causes true when it returns null......
var temp = command.ExecuteScalar();//this was just to check something else
if (exists)
{
exhibitWarning.Visible = true;
Warning1.Text = "There is an existing Task associated 0.";
}
}
catch (SqlException sql)
{
lblStatus.Text = "Couldn't connect to the Database - Error";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
catch (Exception ex)
{
lblStatus.Text = "An error occured";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com