SqlParameter有已经包含在另一个SqlParameterCollection - command.Parameters.Clear()不工作 [英] The SqlParameter is already contained by another SqlParameterCollection - command.Parameters.Clear() does not work

查看:488
本文介绍了SqlParameter有已经包含在另一个SqlParameterCollection - command.Parameters.Clear()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我越来越绝望这里。我无法弄清楚为什么会发生这种例外情况发生。我搜索并阅读有关这个​​问题,似乎每个人管理与command.Parameters.Clear()来解决这个问题,但是这并不为我工作。这是我的code:

I am getting desperate here. I cant figure out why does this exception occur. I searched and read about this problem and it seems everyone managed to solve it with command.Parameters.Clear(), but this does not work for me. Here is my code:

    public class ProductsDataAccess : Connection 
{
    public static SqlParameter[] createParams(Products prod)
    {
        SqlParameter idParam = new SqlParameter("@id", prod.prodID);
        idParam.SqlDbType = System.Data.SqlDbType.VarChar;
        idParam.Size = 45;
        idParam.IsNullable = false;
        idParam.Direction = ParameterDirection.Input;

        SqlParameter nameParam = new SqlParameter("@name", prod.prodName);
        nameParam.SqlDbType = System.Data.SqlDbType.VarChar;
        nameParam.Size = 45;
        nameParam.IsNullable = false;
        nameParam.Direction = ParameterDirection.Input;

        SqlParameter demandParam = new SqlParameter("@demand", prod.demand);
        demandParam.SqlDbType = System.Data.SqlDbType.Int;
        demandParam.IsNullable = false;
        demandParam.Direction = ParameterDirection.Input;

        SqlParameter demandLTParam = new SqlParameter("@demandLT", prod.demandLT);
        demandLTParam.SqlDbType = System.Data.SqlDbType.Int;
        demandLTParam.IsNullable = false;
        demandLTParam.Direction = ParameterDirection.Input;

        SqlParameter leadTimeParam = new SqlParameter("@leadTime", prod.leadTime);
        leadTimeParam.SqlDbType = System.Data.SqlDbType.Int;
        leadTimeParam.IsNullable = false;
        leadTimeParam.Direction = ParameterDirection.Input;

        SqlParameter fixedCoastParam = new SqlParameter("@k", prod.k);
        fixedCoastParam.SqlDbType = System.Data.SqlDbType.Float;
        fixedCoastParam.IsNullable = false;
        fixedCoastParam.Direction = ParameterDirection.Input;

        SqlParameter cParam = new SqlParameter("@c", prod.c);
        cParam.SqlDbType = System.Data.SqlDbType.Float;
        cParam.IsNullable = false;
        cParam.Direction = ParameterDirection.Input;

        SqlParameter chPrecentageParam = new SqlParameter("@chPrecent", prod.precentOfC);
        chPrecentageParam.SqlDbType = System.Data.SqlDbType.Int;
        chPrecentageParam.IsNullable = false;
        chPrecentageParam.Direction = ParameterDirection.Input;

        SqlParameter chParam = new SqlParameter("@ch", prod.ch);
        chParam.SqlDbType = System.Data.SqlDbType.Float;
        chParam.IsNullable = false;
        chParam.Direction = ParameterDirection.Input;

        SqlParameter optimalQParam = new SqlParameter("@q", prod.q);
        optimalQParam.SqlDbType = System.Data.SqlDbType.Int;
        optimalQParam.IsNullable = false;
        optimalQParam.Direction = ParameterDirection.Input;

        SqlParameter annualChParam = new SqlParameter("@annualCh", prod.annualCh);
        annualChParam.SqlDbType = System.Data.SqlDbType.Float;
        annualChParam.IsNullable = false;
        annualChParam.Direction = ParameterDirection.Input;

        SqlParameter annualKParam = new SqlParameter("@annualK", prod.annualK);
        annualKParam.SqlDbType = System.Data.SqlDbType.Float;
        annualKParam.IsNullable = false;
        annualKParam.Direction = ParameterDirection.Input;

        SqlParameter optimalTACParam = new SqlParameter("@tac", prod.tac);
        optimalTACParam.SqlDbType = System.Data.SqlDbType.Float;
        optimalTACParam.IsNullable = false;
        optimalTACParam.Direction = ParameterDirection.Input;

        SqlParameter rParam = new SqlParameter("@r", prod.r);
        rParam.SqlDbType = System.Data.SqlDbType.Int;
        rParam.IsNullable = false;
        rParam.Direction = ParameterDirection.Input;

        SqlParameter inventoryLvlParam = new SqlParameter("@invLvl", prod.inventoryLvl);
        inventoryLvlParam.SqlDbType = System.Data.SqlDbType.Int;
        inventoryLvlParam.IsNullable = false;
        inventoryLvlParam.Direction = ParameterDirection.Input;

        SqlParameter statusParam = new SqlParameter("@status", prod.status);
        statusParam.SqlDbType = System.Data.SqlDbType.VarChar;
        statusParam.Size = 10;
        statusParam.IsNullable = false;
        statusParam.Direction = ParameterDirection.Input;

        SqlParameter[] paramList = {idParam, nameParam, demandParam, demandLTParam,
                                   leadTimeParam, fixedCoastParam, cParam,
                                   chPrecentageParam, chParam, optimalQParam,
                                   annualChParam, annualKParam, optimalTACParam, 
                                   rParam, inventoryLvlParam, statusParam };
        return paramList;

    }

    public static void insertProduct(Products product)
    {


        String comStr = "insert into Products(prodID, name, demand, demandLT, leadTime, fixedCoast, " + 
            "c, chAsPrecentage, ch, optimalQ, annualCh, annualK, optimalTAC, r, inventoryLvl, prodStatus) " + 
            "values(@id, @name, @demand, @demandLT, @leadTime, @k, @c, @chPrecent, " + 
            "@ch, @q, @annualCh, @annualK, @tac, @r, @invLvl, @status)";

        createConncetion();

        SqlCommand command = new SqlCommand(comStr, con);
        command.Parameters.AddRange(createParams(product));

        openConnection();

        command.ExecuteNonQuery();
        closeConnection();

        command.Parameters.Clear()
        command.Dispose();
    }

    public static DataTable getAllProducts()
    {
        String comStr = "select prodID, name, demand, demandLT, leadTime, fixedCoast, c, chAsPrecentage, " + 
            "ch, optimalQ, annualCh, annualK, optimalTAC, r, inventoryLvl, prodStatus from Products";

        createConncetion();

        SqlCommand command = new SqlCommand(comStr, con);

        DataTable table = new DataTable("allProduct");
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand=command;

        openConnection();

        adapter.Fill(table);
        command.Dispose();
        closeConnection();
        return table;
    }

}

下面是在我使用sqlParameters其他类:

Here is the other class in which I am using sqlParameters:

    public class StatisticsDataAccess : Connection
{

    public static SqlParameter[] createParams(Statistics stat, string prodID)
    {

        SqlParameter idParam = new SqlParameter("@id", prodID);
        idParam.SqlDbType = System.Data.SqlDbType.VarChar;
        idParam.Size = 45;
        idParam.IsNullable = false;
        idParam.Direction = ParameterDirection.Input;

        SqlParameter m1Param = new SqlParameter("@m1", stat.month1);
        m1Param.SqlDbType = System.Data.SqlDbType.Int;
        m1Param.IsNullable = false;
        m1Param.Direction = ParameterDirection.Input;

        SqlParameter m2Param = new SqlParameter("@m2", stat.month2);
        m2Param.SqlDbType = System.Data.SqlDbType.Int;
        m2Param.IsNullable = false;
        m2Param.Direction = ParameterDirection.Input;

        SqlParameter m3Param = new SqlParameter("@m3", stat.month3);
        m3Param.SqlDbType = System.Data.SqlDbType.Int;
        m3Param.IsNullable = false;
        m3Param.Direction = ParameterDirection.Input;

        SqlParameter m4Param = new SqlParameter("@m4", stat.month4);
        m4Param.SqlDbType = System.Data.SqlDbType.Int;
        m4Param.IsNullable = false;
        m4Param.Direction = ParameterDirection.Input;

        SqlParameter m5Param = new SqlParameter("@m5", stat.month5);
        m5Param.SqlDbType = System.Data.SqlDbType.Int;
        m5Param.IsNullable = false;
        m5Param.Direction = ParameterDirection.Input;

        SqlParameter m6Param = new SqlParameter("@m6", stat.month6);
        m6Param.SqlDbType = System.Data.SqlDbType.Int;
        m6Param.IsNullable = false;
        m6Param.Direction = ParameterDirection.Input;

        SqlParameter m7Param = new SqlParameter("@m7", stat.month7);
        m7Param.SqlDbType = System.Data.SqlDbType.Int;
        m7Param.IsNullable = false;
        m7Param.Direction = ParameterDirection.Input;

        SqlParameter m8Param = new SqlParameter("@m8", stat.month8);
        m8Param.SqlDbType = System.Data.SqlDbType.Int;
        m8Param.IsNullable = false;
        m8Param.Direction = ParameterDirection.Input;

        SqlParameter m9Param = new SqlParameter("@m9", stat.month9);
        m9Param.SqlDbType = System.Data.SqlDbType.Int;
        m9Param.IsNullable = false;
        m9Param.Direction = ParameterDirection.Input;

        SqlParameter m10Param = new SqlParameter("@m10", stat.month10);
        m10Param.SqlDbType = System.Data.SqlDbType.Int;
        m10Param.IsNullable = false;
        m10Param.Direction = ParameterDirection.Input;

        SqlParameter m11Param = new SqlParameter("@m11", stat.month11);
        m11Param.SqlDbType = System.Data.SqlDbType.Int;
        m11Param.IsNullable = false;
        m11Param.Direction = ParameterDirection.Input;

        SqlParameter m12Param = new SqlParameter("@m12", stat.month12);
        m12Param.SqlDbType = System.Data.SqlDbType.Int;
        m12Param.IsNullable = false;
        m12Param.Direction = ParameterDirection.Input;

        SqlParameter[] paramList = {idParam, m1Param, m2Param, m3Param, m4Param,
                                   m5Param, m6Param, m7Param, m8Param, m8Param,
                                   m10Param, m11Param, m12Param };
        return paramList;
    }

    public static void insertStatistic(Statistics stat, string prodID)
    {


        String comStr = "insert into LTDStatistic(prodID, month1, month2, month3, month4, month5, " +
            "month6, month7, month8, month9, month10, month11, month12)" +
            "values(@id, @m1, @m2, @m3, @m4, @m5, @m6, @m7, @m8, @m9, @m10, @m11, @m12)";

        createConncetion();

        SqlCommand command = new SqlCommand(comStr, con);
        command.Parameters.AddRange(createParams(stat, prodID));

        openConnection();

        command.ExecuteNonQuery();
        closeConnection();
        command.Parameters.Clear();
        command.Dispose();

    }

}

WCF服务使用这些类方法,以修改数据库

WCF Service uses methods from those classes in order to modify a data base

        public void create(ProductsData product)
    {

        Statistics statDLT = new Statistics(product.demandLTStat.month1, product.demandLTStat.month2,
            product.demandLTStat.month3, product.demandLTStat.month4, product.demandLTStat.month5,
            product.demandLTStat.month6, product.demandLTStat.month7, product.demandLTStat.month8,
            product.demandLTStat.month9, product.demandLTStat.month10, product.demandLTStat.month11,
            product.demandLTStat.month12);

        Products newProd = new Products(product.prodID, product.prodName,
            product.leadTime, product.k, product.c, product.precentOfC, 
            product.demand, statDLT);

        newProd.callculate();
        newProd.determineR();
        ProductsDataAccess.insertProduct(newProd);
        //String plsWork = newProd.prodID;
        StatisticsDataAccess.insertStatistic(newProd.demandLTStat, newProd.prodID);
    }

也许我缺少的是如何工作的一些重要的方面......我不知道。希望有人能帮助我,对不起长期code段。

Maybe Im missing some important aspect of how things work ... I dont know. Hope someone can help me, and sorry for the long code segments.

推荐答案

m8Param 中出现了两次由 StatisticsDataAccess.createParams返回的数组()

这篇关于SqlParameter有已经包含在另一个SqlParameterCollection - command.Parameters.Clear()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆