将存储过程结果加载到数据表中

时间:2013-07-06 09:57:58

标签: c# stored-procedures sql-server-2012

我一直在网上搜索答案,但一次又一次空出来。

我正在尝试做什么: 将存储过程的结果加载到DataTable中。

出了什么问题: 我没有收到任何行。

这是我的存储过程(SQL Server 2012)。它获取您输入的表的下一个自动递增ID并将其返回。

ALTER procedure [dbo].[GET_NEXT_AUTO_ID_OF_TABLE]
    @TABLE_NAME nvarchar(128),
    @NEXT_ID int output
as

declare @latest_id int, @row_count int
    begin 
        set @latest_id = (select IDENT_CURRENT(@TABLE_NAME))
    end
    if @latest_id = 1
    begin
    declare @lRowCountSql nvarchar(1000)
    set @lRowCountSql = N'select @row_count = count(*) from ' + @TABLE_NAME
    exec sp_executesql @lRowCountSql, N'@row_count int out', @row_count out

    if @row_count > 0
        set @next_id = @latest_id + 1
    else
        set @next_id = @latest_id
end
else
    set @next_id = @latest_id + 1
return

问题是我的proc(我对sql不好)?当我在SQL Server中测试proc时,我得到了我期望的结果。 但不是来自我的C#代码:

        List<SqlParameter> aSqlParams = new List<SqlParameter>();
        aSqlParams.Add(new SqlParameter("@TABLE_NAME", "your table name") { Direction = System.Data.ParameterDirection.Input, SqlDbType = System.Data.SqlDbType.NVarChar });
        aSqlParams.Add(new SqlParameter() { ParameterName = "@NEXT_ID", Direction = System.Data.ParameterDirection.Output, SqlDbType = SqlDbType.Int });
        DataTable lDt = SQLServerUtils.ExecuteStoredProc("GET_NEXT_AUTO_ID_OF_TABLE", aSqlParams);
        int lNextID = lDt.Rows[0].Field<int>("NEXT_ID");

    public static DataTable ExecuteStoredProc(string aProcName, List<SqlParameter> aSqlParams)
    {
        DataTable lResults = new DataTable();

        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            SqlCommand cmd = new SqlCommand(aProcName, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            if (aSqlParams != null)
                foreach (SqlParameter lP in aSqlParams)
                    cmd.Parameters.Add(lP);
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(lResults);
        }
        return lResults;
    }

2 个答案:

答案 0 :(得分:0)

输出参数由其自身返回,不包含在数据表中 我认为你需要一个不同的程序来执行这种查询,

public static int ExecuteOutputIntParam(string aProcName, string outputParamName, List<SqlParameter> aSqlParams)
{
    int outValue = -1;
    using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(aProcName, conn);
        cmd.CommandType = CommandType.StoredProcedure;

        if (aSqlParams != null)
            foreach (SqlParameter lP in aSqlParams)
                cmd.Parameters.Add(lP);

        int result = cmd.ExecuteNonQuery();

        if (aSqlParams != null)
        {
            outValue = Convert.ToInt32(aSqlParams[outputParamName].Value);

        } 
    }
    return outValue;
}

修改 我已经复制/粘贴了您的示例,我没有注意到您依赖SqlDataAdapter来打开/关闭连接。在我的示例中,应该明确打开连接

答案 1 :(得分:0)

这里的技巧是我想要的值返回out参数。 固定代码看起来像这样......

SQL proc:

ALTER procedure [dbo].[GET_NEXT_AUTO_ID_OF_TABLE]
    @TABLE_NAME nvarchar(128),
    @NEXT_ID int output
as

declare @latest_id int, @row_count int
begin 
    set @latest_id = (select IDENT_CURRENT(''+@TABLE_NAME+''))
end
if @latest_id = 1
    begin
        declare @lRowCountSql nvarchar(1000)
        set @lRowCountSql = N'select @row_count = count(*) from ' + @TABLE_NAME
        exec sp_executesql @lRowCountSql, N'@row_count int out', @row_count out

        if @row_count > 0
            set @next_id = @latest_id + 1
        else
            set @next_id = @latest_id
    end
else
    set @next_id = @latest_id + 1
return

C#:

    public static int GetNextIdOfTable(string aTableName)
    {
        int lNextID = 0;

        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            SqlCommand cmd = new SqlCommand("GET_NEXT_AUTO_ID_OF_TABLE", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@TABLE_NAME", aTableName) { Direction = System.Data.ParameterDirection.Input, SqlDbType = System.Data.SqlDbType.NVarChar });
            cmd.Parameters.Add(new SqlParameter() { ParameterName = "@NEXT_ID", Direction = System.Data.ParameterDirection.Output, SqlDbType = SqlDbType.Int });

            conn.Open();
            cmd.ExecuteReader();

            if (cmd.Parameters["@NEXT_ID"] != null && cmd.Parameters["@NEXT_ID"].Value != DBNull.Value)
                return int.Parse(cmd.Parameters["@NEXT_ID"].Value.ToString());
        }
        return lNextID;
    }