我有一个方法,并试图弄清楚如何处理使用null参数调用该方法。
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;
}
需要&#34; techID&#34; integer作为参数,并使用该技术人员ID填充事件列表。但是,某些事件的techID值为null。我不希望将它们添加到我的列表中。
我尝试了一个if(techID!= null),但是它给出了一个错误,说它永远都是真的。我认为我的sql语句不好,因为&#34;其中TechID = null&#34;不会返回结果,它应该是&#34;其中TechID为null&#34;在sql server management studio中测试此语句时。有任何处理这些空值的建议吗? 我的事故课也如下所示。我将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; }
}
答案 0 :(得分:0)
您应该添加另一个条件TechID IS NOT NULL
来检查SELECT
查询中的不存在,例如
SELECT IncidentID, CustomerID, ProductCode, TechID, DateOpened, DateClosed,
Title, Description from Incidents
where TechID = @TechID AND TechID IS NOT NULL;
答案 1 :(得分:0)
怎么样
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
对象...
答案 2 :(得分:0)
根据你的函数原型,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"];
鉴于您提供的代码示例,null TechID
值不应成为问题。您不必添加if
或修改where
子句来处理空techID
值,因为c#代码不允许这些值。
您已尝试按原样运行代码?