Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 02-01-2006, 06:25 PM   #1
Buccaneer
Head Coach
 
Join Date: Oct 2000
Location: Colorado
C# Help: OleDbCommand, Parameters for INSERT

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?


Last edited by Buccaneer : 02-01-2006 at 06:26 PM.
Buccaneer is offline   Reply With Quote
Old 02-01-2006, 06:38 PM   #2
PackerFanatic
Pro Starter
 
Join Date: Jul 2005
Location: Appleton, WI
You would declare three seperate parameter variables with the values you need. You only need one command, then you add the parameters in the order you need them in the SQL statement
__________________
Commissioner of the RNFL
PackerFanatic is offline   Reply With Quote
Old 02-01-2006, 06:43 PM   #3
PackerFanatic
Pro Starter
 
Join Date: Jul 2005
Location: Appleton, WI
And if you ever need any more help man, feel free to IM me on AIM at keksemeister or shoot me an e-mail. I love C# and use it ALL the time...yes, I am a geek
__________________
Commissioner of the RNFL
PackerFanatic is offline   Reply With Quote
Old 02-01-2006, 06:57 PM   #4
Buccaneer
Head Coach
 
Join Date: Oct 2000
Location: Colorado
Thanks for your response. Something like this?

string sql = "INSERT INTO TABLE2 (ITEM1, ITEM2, ITEM3) VALUES (?,?,?)";
OleDbCommand cmdInsert = new OleDbCommand(sql, conn);
OleDbParameter oleParam = new OleDbParameter():
oleParam.OleDbType = OleDbType.Integer;
oleParam.Value = intVariable1;
cmdInsert.Parameters.Add(oleParam);
OleDbParameter oleParam = new OleDbParameter():
oleParam.OleDbType = OleDbType.Integer;
oleParam.Value = intVariable2;
cmdInsert.Parameters.Add(oleParam);
OleDbParameter oleParam = new OleDbParameter():
oleParam.OleDbType = OleDbType.Integer;
oleParam.Value = intVariable3;
cmdInsert.Parameters.Add(oleParam);
cmdInsert.ExecuteNonQuery();
Buccaneer is offline   Reply With Quote
Old 02-01-2006, 07:36 PM   #5
PackerFanatic
Pro Starter
 
Join Date: Jul 2005
Location: Appleton, WI
That will work just fine! (although make sure you use three different names for the parameters)

Although, to cut down on the lines of code, you can actualy put the OleDbType and Value all on the same line as the parameter declaration...like so...

OleDbParameter oleParam = new OleDbParameter("@int1", OleDbType.Integer).Value = intVariable1;

The "@int1" is simply a name you give the param, you can name it anything. When you type in the .Value, nothing will come up for Intellisense, but it will work just the same.
__________________
Commissioner of the RNFL
PackerFanatic is offline   Reply With Quote
Old 02-01-2006, 07:39 PM   #6
Buccaneer
Head Coach
 
Join Date: Oct 2000
Location: Colorado
Thank you for your help. I looked at all of the overloads with the Add functions and just didn't know how to include the .Value part, which is the only important part of the whole function.
Buccaneer is offline   Reply With Quote
Old 02-01-2006, 09:07 PM   #7
PackerFanatic
Pro Starter
 
Join Date: Jul 2005
Location: Appleton, WI
Yeah, when I first starting using parameters, I was confused by that too. I didn't think the .Value would actually work, but it does and it is a very nice thing to use!

And no worries
__________________
Commissioner of the RNFL
PackerFanatic is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 03:29 AM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.