0

我一直在网上寻找答案,但一次又一次地空着。

我正在尝试做的事情:将存储过程中的结果加载到 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;
    }
4

2 回答 2

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 来打开/关闭连接。在我的示例中,应显式打开连接

于 2013-07-06T10:17:11.660 回答
0

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

SQL过程:

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;
    }
于 2013-07-07T17:50:45.377 回答