c# - SQL 服务器 2005 : dynamically adding parameters to a stored procedure

标签 c# sql-server tsql stored-procedures

场景

我有一个采用单个参数的存储过程。我想更新此存储过程以获取可变数量的参数——一个我永远不会知道的数字。 我目前通过 C# 接口(interface)使用 SQLConnections,以便将单个参数传递给存储过程并返回结果。

SQL 部分

假设我有一个存储过程,它根据单个输入参数“@ccy”-(货币)返回结果列表。现在假设我想更新这个存储过程以获取货币列表而不是单个货币,但是这个数字将根据情况而变化。

SQL 代码

ALTER PROCEDURE [dbo].[SEL_BootStrapperInstRICs]
(
@ccy varchar(10)
)
AS

SELECT DISTINCT i.CCY, i.Instrument, i.Tenor, r.RIC, r.[Server], r.RIType
FROM MDR.dbo.tblBootStrapperInstruments as i, MDR.dbo.tblBootStrapperRICs as r
WHERE i.Instrument = r.MurexInstrument
AND
i.Tenor = r.Tenor
AND i.CCY = r.CCY
AND i.CCY = @ccy
AND r.RIType NOT LIKE '%forward%'

C#部分

这个特定的存储过程是从使用“SqlCommand.Parameters.AddWithValue()”方法的 C# WinForms 应用程序调用的。如前所述,此方法当前将单个 Currency 作为参数传递给存储过程,并将结果作为 DataSet 返回。

    public DataSet GetBootStrapperInstRICsDS(List<string> ccys)
    {
        DataSet ds;
        SqlConnection dbConn = null;
        SqlCommand dbCmd = new SqlCommand();

        try
        {
            dbConn = GetSQLConnection();
            dbCmd = GetSqlCommand();
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.CommandText = Utils.Instance.GetSetting     ("SELBootStrapInsRics", "default");
            foreach(string ccy in ccys)
              dbCmd.Parameters.AddWithValue("@ccy", ccy);

            dbCmd.CommandTimeout = 600;
            dbCmd.Connection = dbConn;

            SqlDataAdapter adapter = new SqlDataAdapter(dbCmd);
            ds = new DataSet();
            adapter.Fill(ds, "tblBootStrapperInstRICs");

            dbCmd.Connection.Open();

            return ds;
        }
        catch (Exception ex)
        {
            ApplicationException aex = new ApplicationException        ("GetBootStrapperInstRICsDS", ex);
            aex.Source = "Dal.GetBootStrapperInstRICsDS " + ex.Message;
            MainForm.job.Log(aex.Source, Job.MessageType.Error);
            Job.incurredErrors = true;
            throw aex;
        }
        finally
        {
            if (dbCmd != null)
                dbCmd.Dispose();
            if (dbConn != null)
            {
                dbConn.Close();
                dbConn.Dispose();
            }
        }
    }

问题

在 C# 方面,我认为我最好的选择是使用“foreach/for 循环”来遍历参数列表并向 SPROC 动态添加一个新参数。 (我已经在上面的 C# 代码中进行了此更新)。

但是 - 有什么方法可以在 SQL 存储过程中执行此操作吗?我的想法分为两个可能的选项——在 SPROC 中创建 20 个或更多参数(每个参数名称相同但末尾有一个递增的数字,例如 - @ccy1、@ccy2 等)并使用“for(int i= 0;我

    for(int i=0;i<NumberOfCurrenciesToAdd;i++)
       dbCmd.Parameters.AddWithValue("@ccy"+i, currencyArray[i]);   

或者另一种选择是做一些完全不同的事情,减少垃圾和 hack-esque。非常感谢帮助。

编辑 - SQL Server 2005

EDIT2 - 必须使用 SPROCS - 公司规范要求。

最佳答案

您从未指定 SQL Server 版本,但对于 2008 年有 Table-Valued Parameters ,这可能对您有帮助:

Table-valued parameters are a new parameter type in SQL Server 2008. Table-valued parameters are declared by using user-defined table types. You can use table-valued parameters to send multiple rows of data to a Transact-SQL statement or a routine, such as a stored procedure or function, without creating a temporary table or many parameters.

关于c# - SQL 服务器 2005 : dynamically adding parameters to a stored procedure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2252260/

相关文章:

sql - 有类似 "table pointer"的东西吗?

c# - 尝试在我的程序中查看打开的事件窗口时收到问号

c# - 无法使用模板字段更新我的网格

c# - 约束 System.Type 作为类型安全的通用参数

sql-server - 具有事件副本的 Azure SQL 数据库副本

sql - 当前和上一个日期的累积列值

sql - T-SQL存储过程返回google风格 "suggested"搜索结果

c# - 如何以编程方式关闭 IE8 WebBrowser 控件中的怪癖模式?

sql-server - Adapt 替换所有表中的所有字符串以处理文本

sql-server - 使用触发器来强制数据完整性 - 多余吗?