【问题标题】:Parameter not passing into SQL Server stored procedure参数未传递到 SQL Server 存储过程
【发布时间】:2013-02-05 03:01:28
【问题描述】:

我需要这方面的帮助;看起来很简单,但我就是想不通。

我的代码:

    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

【问题讨论】:

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


    【解决方案1】:

    您缺少打开连接:

    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;
    }
    

    【讨论】:

    • 你遗漏的比那个 user1748443 多很多
    • 该死的伙计,漫长的一天结束了,我的大脑无法弄清楚这一点。我已经离开了命令类型。你饶了我哈哈
    猜你喜欢
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2013-07-06
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    相关资源
    最近更新 更多