C# SqlParameter异常:SqlParameter已包含在另一个SqlParameterCollection中
我有个例外 SqlParameter已包含在另一个SqlParameterCollection中 我已经将我的C# SqlParameter异常:SqlParameter已包含在另一个SqlParameterCollection中,c#,sql-server,exception,sqlparameter,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
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();
}
}
}
}
private void SendToSQLServer\u派系排名(派系)
{
string sqlCnnString=“Server=tcp:horde.database.windows.net,1433;初始目录=horde\u db;持久安全信息=False;用户ID=USERNAME@horde;Password=Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;连接超时=30;“;
SqlParameter sqlparaction\u-partion\u-ID=新的SqlParameter(“@partion\u-ID”,SqlDbType.Int);
SqlParameter sqlparaction\u ToFaction ID=新的SqlParameter(“@ToFaction\u ID”,SqlDbType.Int);
SqlParameter sqlparaction_Standing=新的SqlParameter(“@Standing”,SqlDbType.SmallInt);
使用(SqlConnection sqlCnn=newsqlconnection(sqlCnnString))
{
sqlCnn.Open();
使用(SqlTransaction sqlTrans=sqlCnn.BeginTransaction())
{
使用(SqlCommand sqlCmd=new SqlCommand())
{
sqlCmd.Parameters.Clear();
foreach(派系立场派系立场派系立场)
{
//
//-插入表格数据
//
SQLPARAPROTIONE\u PROTIONE\u ID.Value=PARTIONSTANDING.PROTIONE\u ID;
sqlCmd.Parameters.Add(sqlparaction\u-partition\u-ID);//您将同一个SqlParameter多次添加到同一个SqlCommand中。问题是Parameters.Clear()
从命令中删除参数,但参数仍然认为命令“拥有”这些参数。无法删除此所有权。这是不直观的,也是常见的错误:)
解决此问题的两种方法:
为每个循环迭代创建新的SqlParameter对象
或者使用一个SqlCommand(就像你所拥有的)并将sqlCmd.Parameters添加到循环之外
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();
}
}
}
}
我添加了两行以在参数被使用后清除参数,希望它们不会被using语句处理。同样的问题。更新了代码和行号。请查看帮助文章,还有。谢谢!我不敢相信我忽略了这一点。我不会再犯那个错误了!