gpt4 book ai didi

c# - SQL参数异常: The SqlParameter is already contained by another SqlParameterCollection

转载 作者:行者123 更新时间:2023-11-30 21:44:03 25 4
gpt4 key购买 nike

<分区>

我遇到了异常

The SqlParameter is already contained by another 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();
}
}
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com