Buccaneer
02-01-2006, 06:25 PM
I struggled today on exactly how to INSERT multiple variables into a OleDbConnection (Access table) using OleDbCommand. Thought I would ask for help here since most of the examples in MSDN and online show using a value from a datatable. In my GIS application, I'm picking up the several values from multiple GIS features and want to populate a previously-created table each record at a time. Here's what I've got for a single variable:
string sql = "INSERT INTO TABLE1 (ITEM1) VALUES (?)";
OleDbParameter oleParam = new OleDbParameter():
oleParam.OleDbType = OleDbType.Integer;
oleParam.Value = intVariable1;
OleDbCommand cmdInsert = new OleDbCommand(sql, conn);
cmdInsert.Parameters.Add(oleParam);
cmdInsert.ExecuteNonQuery();
That works fine as I loop through each feature and picking up the intVariable value. But in the next table, I want to add three variables. I am unclear as to creating the objects and getting the value into the parameter:
string sql = "INSERT INTO TABLE2 (ITEM1, ITEM2, ITEM3) VALUES (?,?,?)";
//do I repeat the oleParam object 3 times, each with a different .Value?
//how many OleDbCommand objects do I need and when do I add the Parameter to the collection?
string sql = "INSERT INTO TABLE1 (ITEM1) VALUES (?)";
OleDbParameter oleParam = new OleDbParameter():
oleParam.OleDbType = OleDbType.Integer;
oleParam.Value = intVariable1;
OleDbCommand cmdInsert = new OleDbCommand(sql, conn);
cmdInsert.Parameters.Add(oleParam);
cmdInsert.ExecuteNonQuery();
That works fine as I loop through each feature and picking up the intVariable value. But in the next table, I want to add three variables. I am unclear as to creating the objects and getting the value into the parameter:
string sql = "INSERT INTO TABLE2 (ITEM1, ITEM2, ITEM3) VALUES (?,?,?)";
//do I repeat the oleParam object 3 times, each with a different .Value?
//how many OleDbCommand objects do I need and when do I add the Parameter to the collection?