繁体   English   中英

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

[英]How can I pass value to a Specified parameter in stored procedure using 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;
}

代替:

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

尝试将参数作为键值对传递。 例:

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

string将存储参数的名称,而object将保留值。 这样,您可以通过命名参数传递值。 只会设置这些参数的值,其余的将保持为空(顺序无关紧要)

用法:

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

我相信你正在寻找这样的东西。 这里一个存储过程与所有插入,更新,删除一起选择。 @QueryType必须每次都传递一次,但是rest变量仅在需要时才需要调用,因为如果没有传递任何值,它将分别分配默认值。

示例>> 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM