Reputation: 3
I have built a wrapper method around System.Data.SQLite that allows me to execute parameterized queries.
When I try to add SQLiteParameter[] to the list by .Add(), all the items in the list become duplicates of the last item added..
Here is the code:
string sql = "INSERT INTO request_flow (request_id, sequence, date_assigned " +
"VALUES (@request_id, @sequence, @date_assigned);";
List<SQLiteParameter[]> paramList = new List<SQLiteParameter[]>();
SQLiteParameter[] param = new SQLiteParameter[3];
SQLiteParameter p1 = new SQLiteParameter("request_id", DbType.Int32);
SQLiteParameter p2 = new SQLiteParameter("sequence", DbType.Double);
SQLiteParameter p3 = new SQLiteParameter("date_assigned", DbType.String);
for (int i = 0; i < assigneeIDs.Count; i++)
{
p1.Value = requestID;
p2.Value = Convert.ToDouble(last_seq + "." + (i + 1));
p3.Value = DateTime.Now.ToString(ThisAddIn.FullDate);
param[0] = p1;
param[1] = p2;
param[2] = p3;
paramList.Add(param);
}
int inserted = SQLite.ExecuteParams(sql, paramList);
I did put a bearkpoint after each loop to debug, and I confirmed that the values after the first iteration are as they are intended. However, in the next iteration, the first item (at index 0) gets overwritten somehow with the values of the second iteration. So last item added is always going to overwrite every single previous item in the list.
What did I do wrong?
EDIT: Here is a simpler (less dependency required) sample code that has the same behavior:
List<string[]> paramList = new List<string[]>();
string[] param = new string[3];
string p1 = "";
string p2 = "";
string p3 = "";
for (int i = 0; i < 3; i++)
{
p1 = i.ToString() + ".1";
p2 = i.ToString() + ".2";
p3 = i.ToString() + ".3";
param[0] = p1;
param[1] = p2;
param[2] = p3;
paramList.Add(param);
}
foreach (string[] s in paramList)
{
this.textBox1.Text += String.Join(",", s) + "\r\n";
}
Upvotes: 0
Views: 220
Reputation: 83
I think thats occurs because you are workin on the same instance of "param". Try to Instantiate it inside your loop.
Upvotes: 0
Reputation: 203812
You're not creating a new parameter array on each iteration, nor are you creating new parameters on each iteration, you're just mutating the same ones over and over. You need to ensure that a new array is created each iteration of the loop, and that new parameters are created for each iteration of the loop. In this case you simply shouldn't define those variables outside of the loop; define them inside of the loop:
for (int i = 0; i < assigneeIDs.Count; i++)
{
SQLiteParameter[] param = new SQLiteParameter[3];
SQLiteParameter p1 = new SQLiteParameter("request_id", DbType.Int32);
SQLiteParameter p2 = new SQLiteParameter("sequence", DbType.Double);
SQLiteParameter p3 = new SQLiteParameter("date_assigned", DbType.String);
p1.Value = requestID;
p2.Value = Convert.ToDouble(last_seq + "." + (i + 1));
p3.Value = DateTime.Now.ToString(ThisAddIn.FullDate);
param[0] = p1;
param[1] = p2;
param[2] = p3;
paramList.Add(param);
}
Upvotes: 3