简体   繁体   English

如何使用代码将值传递给存储过程中的指定参数?

[英]How can I pass value to a Specified parameter in stored procedure using my code?

The following is my code 以下是我的代码

public static DataSet GetDataSet(string storedProcedure, params object[] parameters)
{
    ConnectionHelper c = new ConnectionHelper();
    c.opencon();
    c.getcmd.CommandType = CommandType.StoredProcedure;
    c.getcmd.CommandText = storedProcedure;
    c.getcmd.Parameters.Clear();

    SqlCommandBuilder.DeriveParameters(c.getcmd);
    int i = 0, j = 0;

    foreach (SqlParameter SPParams in c.getcmd.Parameters)
    {
        if (j > 0)
        {
            SPParams.Value = parameters[i];
            i++;
        }
        j++;
    }

    DataSet ds = new DataSet();

    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = c.getcmd;
    da.Fill(ds);

    return ds;
}

Instead of: 代替:

public static DataSet GetDataSet(string storedProcedure, params object[] parameters)

Try passing parameters as a key value pair. 尝试将参数作为键值对传递。 Example: 例:

public static DataSet GetDataSet(string storedProcedure, params KeyValuePair<string, object> parameters)

string will store the name of the parameter and object will hold the value. string将存储参数的名称,而object将保留值。 This way you can pass value by named parameters. 这样,您可以通过命名参数传递值。 Only these parameter's value will be set and rest will remain null (order doesn't matter) 只会设置这些参数的值,其余的将保持为空(顺序无关紧要)

Usage: 用法:

foreach (var param in parameters)
{
   c.getcmd.Parameters.Add(new SqlParameter(param.Key, param.Value));
}

I belive you are looking for something like this. 我相信你正在寻找这样的东西。 Here one stored procedure with all insert update delete select together. 这里一个存储过程与所有插入,更新,删除一起选择。 @QueryType must to pass all every single time, however the rest variable need to call only when needed because it assignd default values respectively if no value is passed. @QueryType必须每次都传递一次,但是rest变量仅在需要时才需要调用,因为如果没有传递任何值,它将分别分配默认值。

Example >> MyTable has 3 fields, ID (int PK autoincrease, Field1 int, Field2 varchar(20), Below is single stored procedure. 示例>> MyTable具有3个字段,ID(int PK自动增加,Field1 int,Field2 varchar(20),下面是单个存储过程。

CREATE PROCEDURE uspInserUpdateDeleteSelect 
    -- Add the parameters for the stored procedure here
    @QueryType varchar(10),
    @ID int = 0,
    @Field1 int = 0,
    @Field2 varchar(20) = NULL

BEGIN
        IF @QueryType = 'INSERT' BEGIN
            INSERT INTO MyTable (Field1, Field2) VALUES (@Field1, @Field2)
        END

        ELSE IF @QueryType = 'UPDATE' BEGIN
            UPDATE MyTable 
            SET Field1 = @Field1, Field2 = @Field2
            WHERE ID=@ID
        END

        ELSE IF @QueryType = 'DELETE' BEGIN
            DELETE FROM MyTable
            WHERE ID=@ID
        END

        ELSE IF @QueryType = 'SELECTALL' BEGIN
            SELECT * FROM MyTable
        END

        ELSE IF @QueryType = 'SELECTBYID' BEGIN
            SELECT * FROM MyTable WHERE ID=@ID  
        END

END


//For params Error, create a new class and pass as list of the class

    public class ParaNameValue
    {
        public string ParaName{ get; set; }
        public string ParaValue{ get; set; }

        public ParaNameValue(string PName, string PValue){
            this.ParaName=PName;
            this.ParaValue = PValue;
        }
    }

//For caller function, how to pass parameter list

    List<ParaNameValue> ListPara= new List<ParaNameValue>();
    ListPara.Add(new ParaNameValue("@Name", "BlaBla 1");
    ListPara.Add(new ParaNameValue("@Email", "BlaBla 2");

    GetDataSet(string storedProcedure, ListPara);


//for dataset function
    public static DataSet GetDataSet(string storedProcedure, List<ParaNameValue> ParaList)"
    {
        //do other stuffs here

        //getting sqlpara values
        foreach (ParaNameValue p in ParaList)
        {
             c.getcmd.Parameters.Add(new SqlParameter(p.ParaName, p.ParaValue));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理从存储过程到C#代码的空值参数返回? - How can I deal with the return of a null value parameter from a stored procedure to C# code? 如何将空值传递给使用存储过程的SqlDataSource? - How do I pass a null value to a SqlDataSource that is using a stored procedure? 如何从存储过程中获取返回值? - How can I get the return value from my stored procedure? 我们如何从C#将空值传递给dateTime参数到存储过程 - How can we pass a empty Value to dateTime Parameter to a stored procedure from c# 将参数添加到sqldatasource,我应该如何传递它们以在ASP.NET中执行我的存储过程 - Adding parameter to sqldatasource, How should i pass them to execute my stored procedure in asp.net 如何将下拉列表值作为存储过程的参数传递 - how to pass Drop Down List value as a parameter for stored procedure SSIS如何将表值参数传递到存储过程 - SSIS how to pass Table-Value parameter into stored procedure 获取存储过程的值,并将其作为参数传递给另一个存储过程 - Get the value of a stored procedure and pass that as a parameter to another stored procedure 使用 RepoDB 时如何将 OUT 参数传递给 MySQL 存储过程 - How Can I pass OUT parameter to MySQL Stored Procedure when use RepoDB 如何将表值参数从 .net 代码传递给存储过程 - How to pass table value parameters to stored procedure from .net code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM