By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,984 Members | 2,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,984 IT Pros & Developers. It's quick & easy.

using a dictionary with Npgsql

P: 2
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!
Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.