Connecting Tech Pros Worldwide Help | Site Map

using a dictionary with Npgsql

Newbie
 
Join Date: Jun 2008
Posts: 2
#1: Jun 27 '08
Hi, I'm trying to use a dictionary with c# and npgsql in visual studio 2008 and I'm unsure how, i'm very new to postgresql, npgsql, and .net infact! (3rd week). There are multiple keys and values in the dictionary, to keep things simple.

Expand|Select|Wrap|Line Numbers
  1. Dictionary<string, object> d = new Dictionary<string, object>();
  2. d.Add("value1", "data1");
  3. d.Add("value2", "data2");
I have no idea how to set the parameters and execute the query, assume sql is a query string.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tablea VALUES (:value1, :value2)
and "conn" is my connection string. I have already opened to connection in the example below, works with all basic queries.
How do I set up the parameters? Do I need to use NpgsqlParameterCollection? What is the syntax, or do I have to loop through and load each value.
Finally How do I execute the query, will it work as below.

Expand|Select|Wrap|Line Numbers
  1. using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
  2. {
  3.  
  4. // Set parameters somehow
  5. command.Parameters.Add(new NpgsqlParameter(d.Keys.ToString(), DbType.Object));
  6.  
  7. //Load values somehow
  8. command.Parameters[0].Value = d.Values;
  9.  
  10. // Run the query
  11. int rowsaffected = 0;
  12. rowsaffected = command.ExecuteNonQuery();
  13.  
  14. conn.Close();
  15. return rowsaffected;
  16. }
Hope this is enough information.

Also if anyone can recommend a good manual for Npgsql to download that would be good, the HTML one I have is very basic.

Thanks!
Reply


Similar PostgreSQL Database bytes