SqlParameter异常:SqlParameter已包含在另一个SqlParameterCollection中

时间:2016-12-11 01:14:33

标签: c# sql-server exception sqlparameter

我收到了异常

  

SqlParameter已包含在另一个SqlParameterCollection

我已将SqlParameter调用移出foreach循环,因为我认为这是问题,但我仍然遇到相同的异常。

private void SendToSQLServer_FactionStandings(Faction faction)
{
    string sqlCnnString = "Server=tcp:horde.database.windows.net,1433;Initial Catalog=horde_db;Persist Security Info=False;User ID=USERNAME@horde;Password=PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";

    SqlParameter sqlParaFaction_Faction_ID = new SqlParameter("@Faction_ID", SqlDbType.Int);
    SqlParameter sqlParaFaction_ToFactionID = new SqlParameter("@ToFaction_ID", SqlDbType.Int);
    SqlParameter sqlParaFaction_Standing = new SqlParameter("@Standing", SqlDbType.SmallInt);

    using (SqlConnection sqlCnn = new SqlConnection(sqlCnnString))
    {
        sqlCnn.Open();

        using (SqlTransaction sqlTrans = sqlCnn.BeginTransaction())
        {
            using (SqlCommand sqlCmd = new SqlCommand())
            {
                sqlCmd.Parameters.Clear();

                foreach (FactionStanding factionStanding in faction.FactionStandings)
                {
                    //
                    // - Insert Faction Standing Table Data
                    //

                    sqlParaFaction_Faction_ID.Value = factionStanding.Faction_ID; 
                    sqlCmd.Parameters.Add(sqlParaFaction_Faction_ID); // <---- Getting exception here

                    sqlParaFaction_ToFactionID.Value = factionStanding.ToFaction_ID;
                    sqlCmd.Parameters.Add(sqlParaFaction_ToFactionID);

                    sqlParaFaction_Standing.Value = factionStanding.DefaultStanding;
                    sqlCmd.Parameters.Add(sqlParaFaction_Standing);

                    string sqlCmdFactionStandings = "INSERT INTO FactionStandings (Faction_ID, ToFaction_ID, Standing) VALUES (@Faction_ID, @ToFaction_ID, @Standing)";

                    sqlCmd.Connection = sqlCnn;
                    sqlCmd.CommandText = sqlCmdFactionStandings;
                    sqlCmd.Transaction = sqlTrans;

                    sqlCmd.ExecuteNonQuery();
                }

                sqlTrans.Commit();
                sqlCmd.Parameters.Clear();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您将多次添加相同的SqlParameter到同一个SqlCommand。问题是Parameters.Clear()从命令中删除了参数,但参数仍然认为命令拥有&#39;他们。没有办法删除此所有权。这是不直观的,这是一个常见的错误:)

解决此问题的两种方法:

  1. 为每个循环迭代创建新的SqlParameter对象
  2. 或者使用单个SqlCommand(就像你拥有的那样)并将sqlCmd.Parameters.Add拉到循环外部。 e.g。

        SqlParameter sqlParaFaction_Faction_ID = new SqlParameter("@Faction_ID", SqlDbType.Int);
        SqlParameter sqlParaFaction_ToFactionID = new SqlParameter("@ToFaction_ID", SqlDbType.Int);
        SqlParameter sqlParaFaction_Standing = new SqlParameter("@Standing", SqlDbType.SmallInt);
    
        using (SqlConnection sqlCnn = new SqlConnection(sqlCnnString))
        {
            sqlCnn.Open();
            using (SqlTransaction sqlTrans = sqlCnn.BeginTransaction())
            {
                using (SqlCommand sqlCmd = new SqlCommand())
                {
                    sqlCmd.Connection = sqlCnn;
                    string sqlCmdFactionStandings = "INSERT INTO FactionStandings (Faction_ID, ToFaction_ID, Standing) VALUES (@Faction_ID, @ToFaction_ID, @Standing)";
                    sqlCmd.CommandText = sqlCmdFactionStandings;
                    sqlCmd.Transaction = sqlTrans;
                    sqlCmd.Parameters.Add(sqlParaFaction_Faction_ID);
                    sqlCmd.Parameters.Add(sqlParaFaction_ToFactionID);
                    sqlCmd.Parameters.Add(sqlParaFaction_Standing);
    
                    foreach (FactionStanding factionStanding in faction.FactionStandings)
                    {
                        //
                        // - Insert Faction Standing Table Data
                        //
                        sqlParaFaction_Faction_ID.Value = factionStanding.Faction_ID; 
                        sqlParaFaction_ToFactionID.Value = factionStanding.ToFaction_ID;
                        sqlParaFaction_Standing.Value = factionStanding.DefaultStanding;
    
                        sqlCmd.ExecuteNonQuery();
                    }
    
                    sqlTrans.Commit();
                }
            }
        }
    }