使用多个sqlparameter选择查询不起作用

时间:2014-11-14 04:45:52

标签: c# sql-server

这是我的代码

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。

这是我在Management Studio中的查询

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

1 个答案:

答案 0 :(得分:0)

尝试使用以下方式参数

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