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.
- Dictionary<string, object> d = new Dictionary<string, object>();
-
d.Add("value1", "data1");
-
d.Add("value2", "data2");
I have no idea how to set the parameters and execute the query, assume sql is a query string.
- 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.
- using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
-
{
-
-
// Set parameters somehow
-
command.Parameters.Add(new NpgsqlParameter(d.Keys.ToString(), DbType.Object));
-
-
//Load values somehow
-
command.Parameters[0].Value = d.Values;
-
-
// Run the query
-
int rowsaffected = 0;
-
rowsaffected = command.ExecuteNonQuery();
-
-
conn.Close();
-
return rowsaffected;
-
}
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!