我尝试了各种方法来做到这一点,但似乎都没有。该例程不断返回 0,因为结果为空。
这是我的代码:
string strSql = "SELECT [ID] FROM [PurchaseOrder].[dbo].[FMSSupplier] where (ExternalRef = @ExternalRef) and (CompanyID = @CompanyID)";
try
{
SqlCommand command = new SqlCommand(strSql);
command.Connection = new SqlConnection(PO_ConnectionString);
command.CommandType = CommandType.Text;
command.Parameters.Add(new SqlParameter("@ExternalRef",SqlDbType.NVarChar ,50){Value = strExternalRef});
command.Parameters.Add(new SqlParameter("@CompanyID", SqlDbType.Int) { Value = intCompanyID });
if (command.Connection.State == ConnectionState.Closed) command.Connection.Open();
var result = command.ExecuteScalar();
if (result != null)
{
return (int)result;
}
else
{
return 0;
}
}
我在 SQL Server 中对此进行了测试,记录存在:
SELECT [ID]
FROM [PurchaseOrder].[dbo].[FMSSupplier]
WHERE (ExternalRef = 'EDE01') and (CompanyID = 24)
记录 ID 不为零,并且 ConnectionString 在其他实例中有效。
这些是我的两个参数的值:CompanyID = 24 strExternalRef = EDE01