Dim MaxItemID As Integer
Dim objCMD As New SqlCommand(StrSql, objConn)
MaxItemID = IIf(IsDBNull(objCMD.ExecuteScalar()), 0, objCMD.ExecuteScalar()) + 1
我如何在 C# 中做到这一点?
Dim MaxItemID As Integer
Dim objCMD As New SqlCommand(StrSql, objConn)
MaxItemID = IIf(IsDBNull(objCMD.ExecuteScalar()), 0, objCMD.ExecuteScalar()) + 1
我如何在 C# 中做到这一点?
int MaxItemID = 0;
using(SqlCommand objCMD = new SqlCommand(StrSql, objConn))
{
MaxItemID = (Converter.IsDBNull(objCMD.ExecuteScalar()) ? 0 : objCMD.ExecuteScalar()) + 1;
}
int MaxItemID;
SqlCommand objCMD =new SqlCommand (StrSql, objConn);
MaxItemID = IsDBNull(objCMD.ExecuteScalar()? 0: objCMD.ExecuteScalar()) + 1;
这应该这样做(并避免执行 SQL 两次):
var objCMD = new SqlCommand(StrSql, objConn);
var sqlRes = objCMD.ExecuteScalar();
int maxItemID = 1 + (Convert.IsDBNull(sqlRes) ? 0 : (int)sqlRes);
注意 VB 会将object
返回值隐式转换ExecuteScalar
为 an int
,但 C# 不会。