[英]Calling a method with (sometimes) null parameters to execute an SQL statement. C# / ASP.NET
我有一个方法,并且正在尝试弄清楚如何处理带有空参数的该方法。
public static List<Incident> GetIncidentByTechnician(int techID)
{
List<Incident> incidents = new List<Incident>();
Incident inc = null; // found incident
// define connection
SqlConnection connection = TechSupportDB.GetConnection();
// define the select query command
string selectQuery = "select IncidentID, CustomerID, ProductCode, TechID, DateOpened, DateClosed, Title, Description " +
"from Incidents " +
"where TechID = @TechID";
SqlCommand selectCommand = new SqlCommand(selectQuery, connection);
selectCommand.Parameters.AddWithValue("@TechID", techID);
try
{
// open the connection
connection.Open();
// execute the query
SqlDataReader reader = selectCommand.ExecuteReader();
// process the result while there are incidents
while (reader.Read())
{
inc = new Incident();
inc.IncidentID = (int)reader["IncidentID"];
inc.CustomerID = (int)reader["CustomerId"];
inc.ProductCode = reader["ProductCode"].ToString();
inc.TechID = (int)reader["TechID"];
inc.DateOpened = (DateTime)reader["DateOpened"];
inc.DateClosed = (DateTime?)reader["DateClosed"];
inc.Title = reader["Title"].ToString();
inc.Description = reader["Description"].ToString();
incidents.Add(inc); //add to the list
}
}
catch (Exception ex)
{
throw ex; // let the form handle it
}
finally
{
connection.Close(); // close connecto no matter what
}
return incidents;
}
它使用“ techID”整数作为参数,并使用该技术员ID填充事件列表。 但是,某些事件的techID具有空值。 我不希望将它们添加到我的列表中。
我尝试了一个if(techID!= null),但是给出了一个错误,说它将永远是真实的。 我认为我的sql语句不好,因为在SQL Server Management Studio中测试此语句时“ where TechID = null”不会返回结果,并且应该为“ where TechID null”。 有什么建议来处理这些空值? 我的突发事件类也如下所示。 我将techID设置为接受带有int?的空值。
public class Incident
{
public Incident() { }
public int IncidentID { get; set; }
public int CustomerID { get; set; }
public string ProductCode { get; set; }
public int? TechID { get; set; }
public DateTime DateOpened { get; set; }
public DateTime? DateClosed { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
您应该添加另一个条件TechID IS NOT NULL
来检查SELECT
查询中的非null,例如
SELECT IncidentID, CustomerID, ProductCode, TechID, DateOpened, DateClosed,
Title, Description from Incidents
where TechID = @TechID AND TechID IS NOT NULL;
怎么样
while (reader.Read())
{
if (reader["Tech"] != null)
{
inc = new Incident();
inc.IncidentID = (int)reader["IncidentID"];
inc.CustomerID = (int)reader["CustomerId"];
inc.ProductCode = reader["ProductCode"].ToString();
inc.TechID = (int)reader["TechID"];
inc.DateOpened = (DateTime)reader["DateOpened"];
inc.DateClosed = (DateTime?)reader["DateClosed"];
inc.Title = reader["Title"].ToString();
inc.Description = reader["Description"].ToString();
incidents.Add(inc); //add to the list
}
}
如果您具有空值,请不要创建或添加Incident
对象。
根据您的函数原型, techID
是一个整数,不能为空:
public static List<Incident> GetIncidentByTechnician(int techID)
这意味着SQL调用将永远不会包含空@techID
,因为它是从不可空的techID
分配的:
selectCommand.Parameters.AddWithValue("@TechID", techID);
因此,您的select语句仅应返回具有非null techID的行。 毕竟,它必须匹配。
select IncidentID, CustomerID, ProductCode, TechID, DateOpened, DateClosed, Title, Description
from Incidents
where TechID = @TechID
因此,阅读器绝不应该返回空的techID
:
inc.TechID = (int)reader["TechID"];
给定您提供的代码示例,空的TechID
值应该不成问题。 你不应该添加一个if
或修改你where
子句来处理空techID
值,因为它们不会被C#代码被允许。
您尝试按原样运行代码吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.