参数未传入SQL Server存储过程

时间:2013-02-05 03:01:28

标签: c# asp.net stored-procedures ado.net

我需要帮助;看起来很简单,但我无法弄清楚。

我的代码:

    public DataTable GetUserAssociatedModules(string UserEmail)
    {
        // retrieve module titles from Module table to be passed to ddlModules in Student.Master
        try
        {
            SqlCommand getModules = new SqlCommand("GetUserAssociatedModules", con);
            getModules.Parameters.AddWithValue("@userEmail", UserEmail);

            SqlDataReader dr;
            dr = getModules.ExecuteReader();

            //DataTable dt = new DataTable();
            dt.Columns.Add("moduleTitle");
            dt.Load(dr);
            return dt;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("Exception: " + ex.Message + ", CourseDAO.GetModules()");
            return null;
        }
        finally
        {
            // close database connection
            if (con != null)
            {
                con.Close();
            }
        }
    }

我一直有例外:

  

程序或功能' GetUserAssociatedModules'需要参数' @ userEmail',这是未提供的。

我在UserEmail上放了一块手表;它应该从UI中传递值,但是datareader保持为空...

我检查了我的SQL;它执行正常。

我进行了双重和三重检查,以确保代码中调用的存储过程名称与数据库中的过程匹配,并且代码中的参数名称与过程中的参数名称匹配...

程序......

ALTER PROCEDURE [dbo].[GetUserAssociatedModules]
    @userEmail nvarchar(MAX)
AS
BEGIN
    SELECT 
        [dbo].[Module].[moduleId],[moduleTitle] 
    FROM 
        [dbo].[Module] 
    INNER JOIN 
        [dbo].[ModuleUser] ON [dbo].[Module].[moduleId] = [dbo].[ModuleUser].[moduleId]
    WHERE 
        [dbo].[ModuleUser].[userId] = (SELECT [userId]
                                       FROM [dbo].[User]
                                       WHERE [eMail] = @userEmail)
    ORDER BY 
        [moduleTitle] ASC
END

1 个答案:

答案 0 :(得分:5)

您缺少打开连接:

<强> conn.Open();

或者没有指定这一行:

sqlCommand.CommandType = CommandType.StoredProcedure;

public DataTable GetUserAssociatedModules(string UserEmail)
{
var dt = new DataTable();
string connString = AppConfigurationManager.GetAppSetting("ConnectionString",IsTest,IsQA, IsProd);
using (var conn = new SqlConnection(connString))
{
    conn.Open();
    using (var sqlCommand = new SqlCommand("GetUserAssociatedModules", conn))
    {
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlCommand.Parameters.AddWithValue("@userEmail", userEmail);

        SqlDataReader dr = null;
        DataSet ds = new DataSet();
        try
        {
            dr = sqlCommand.ExecuteReader();
            dt = new DataTable("DatatTable_GetUserAssociatedModules");
            ds.Tables.Add(dt);
            ds.Load(dr, LoadOption.OverwriteChanges, dt);
        }
        catch (SqlException ex)
        {
            LogExceptions(ex, new List<Param> { new Param { Name = "userEmail", Value = userEmail} });
         }
        catch (Exception ex)
        {
            LogExceptions(ex, new List<Param> { new Param { Name = "userEmail", Value = userEmail} });
        }
        finally
        {

        }
    }
}
return dt;
}