选择“使用多个 SQL 参数的查询”不起作用

本文关键字:参数 查询 不起作用 SQL 使用多个 选择 | 更新日期: 2023-09-27 18:30:23

这是我的代码

SqlParameter paramUserID = new SqlParameter("@paramUserID", SqlDbType.VarChar, 50);
paramUserID.Value = userID;
SqlParameter paramAccountType = new SqlParameter("@paramAccountType", SqlDbType.Int);
paramAccountType.Value = accountType;
SqlParameter paramSessionID = new SqlParameter("@paramSessionID", SqlDbType.VarChar, 50);
paramSessionID.Value = sessionID;
sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[genericConstants.SVAR_DB_CONN_NAME].ToString());
//for enterprise user, get the permission list for the user
sqlCommand = new SqlCommand("SELECT status FROM WSLogin WHERE ([userID] = @paramUserID) AND ([sessionID] = @paramSessionID) AND ([accountType] = @paramAccountType)", sqlConnection);
sqlCommand.Parameters.Add(paramUserID);
sqlCommand.Parameters.Add(paramAccountType);
sqlCommand.Parameters.Add(paramSessionID);
sqlConnection.Open();
reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
    returnResult = Int32.Parse(reader["status"].ToString());
}
reader.Close();

但是如果我复制粘贴我传递的值(paramUserID, paramAccountTypeparamSessionID)并使用SQL Server Management Studio运行查询,我会得到状态值1。

这是我在管理工作室中的查询

SELECT [status] 
FROM [Public].[dbo].[WSLogin] 
WHERE ([userID] = '207') 
  AND ([sessionID] = 'IJiRVkKss14iIDccY8F4ldaV+jNYX7vIeb6IQIlo/78=') 
  AND ([accountType] = 1)

选择“使用多个 SQL 参数的查询”不起作用

尝试使用以下方式对参数进行参数

sqlCommand.Parameters.AddWithValue("@paramUserID",paramUserID);
sqlCommand.Parameters.AddWithValue("@paramAccountType",paramAccountType);
sqlCommand.Parameters.AddWithValue("@paramSessionID",paramSessionID);