我到目前为止:
// Update status history if the current work flow item has a status
int workflowID = Convert.ToInt32(statusCode.SelectedValue);
string status = "select status from jm_accountworkflowdetail where workid = @workID";
SqlConnection sqlConnection2 = new SqlConnection(sqlDevelopment.ConnectionString);
SqlCommand sqlComm2 = new SqlCommand(status, sqlConnection2);
sqlComm2.Parameters.AddWithValue("@workID", workflowID);
查询将返回一个值('SOC','POS')或(''),表示它没有附加状态。
我需要做的是,如果状态不为空,则执行其他一些代码。但我不确定如何执行所有操作只是为了检查状态是否有值。
答案 0 :(得分:5)
您需要使用ExecuteScalar();
string statusReturned = "";
int workflowID = Convert.ToInt32(statusCode.SelectedValue);
using (SqlConnection sqlConnection2 = new SqlConnection(sqlDevelopment.ConnectionString))
{
string status = "select status from jm_accountworkflowdetail where workid = @workID";
SqlCommand sqlComm2 = new SqlCommand(status, sqlConnection2);
sqlComm2.Parameters.AddWithValue("@workID", workflowID);
try
{
sqlConnection2.Open();
var returnValue = sqlComm2.ExecuteScalar()
if returnValue != null then
statusReturned = returnValue.ToString();
}
catch (Exception ex)
{
//handle exception
}
}
return statusReturned;
要检查字符串值,您可以:
if (!String.IsNullOrEmpty(statusReturned)) {//perform code for SOC or POS}
答案 1 :(得分:1)
为了将来参考,如果您担心检查null并为空值做出一些决定,可以在T-SQL中使用IsNull
函数。如果record为null,它基本上返回一个值。所以你的上面的代码将变成这样:
string status = "select IsNull(status,'NoStatus') from jm_accountworkflowdetail where workid = @workID";
然后在完成SQL之后,您将If Else
用于status
If(Status == "NoStatus") {your code block}
希望它有所帮助。