在我的函数应用程序中,我有一个IF语句,只有当两个变量设置为1和0时才会启动代码的主要部分。这些变量是存储在Azure SQL表中的标志。我做了一个简单的选择查询,但变量值总是-1。我怎么解决这个问题?这是我的代码:
conn.Open();
int IsDatamartSynced, IsCubeProcessed;
var select_for_IsDatamartSynced = "SELECT TOP (1) IsDatamartSynced FROM dbo.test";
var select_for_IsCubeProcessed = "SELECT TOP (1) IsCubeProcessed FROM dbo.test";
/*var text = "UPDATE SalesLT.SalesOrderHeader " +
"SET [Status] = 5 WHERE ShipDate < GetDate();";*/
using (SqlCommand cmd = new SqlCommand(select_for_IsDatamartSynced, conn))
{
IsDatamartSynced = cmd.ExecuteNonQuery();
log.Info($"in using {IsDatamartSynced}");
}
using (SqlCommand cmd = new SqlCommand(select_for_IsCubeProcessed, conn))
{
IsCubeProcessed = cmd.ExecuteNonQuery();
log.Info($"in using {IsCubeProcessed}");
}
答案 0 :(得分:1)
ExecuteNonQuery
返回受更改数据的语句影响的行数,或-1表示其他类型的查询(包括SELECT
)。
如果要检索值,请使用ExecuteScalar
:
using (SqlCommand cmd = new SqlCommand(select_for_IsDatamartSynced, conn))
{
IsDatamartSynced = cmd.ExecuteScalar();
log.Info($"in using {IsDatamartSynced}");
}