Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old July 1st, 2008, 12:59 AM
Newbie
 
Join Date: Jun 2008
Posts: 2
Default PostgreSQL object datatype

Hi,

I'm having a problem, I'm using PostgreSQL V8.3.3-1, Npgsql and Visual studio 2008 c#.

I'm passed a dictionary of key, value (key been a string, value been an object). The code works fine with key and value both been strings but postgresql doesn't seem to like it when the value is an object. How can I correct my code to work when passed an object? I realise that rowsaffected is currently only storing the first value, that's fine. I just want to be able to run a query using an object datatype for now.

current error is: "InvalidCastException is unhandled by user code"

sql is a query
Code:
"SELECT username FROM users WHERE age = :Value1"
Code:
        public static string GetSql(string sql, Dictionary<string, object> d)
        {
            string connection = System.Configuration.ConfigurationManager.ConnectionStrings["POSTGRESQL_LOCAL"].ConnectionString;
            
            using (NpgsqlConnection conn = new NpgsqlConnection(connection))
            {
                conn.Open();
                using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
                {
                    int i = 0;

                    foreach (KeyValuePair<string, object> kvp in d)
                    {
                        command.Parameters.Add(new NpgsqlParameter(kvp.Key, DbType.Object));
                        command.Parameters[i].Value = kvp.Value;
                        i++;
                    }
                    string rowsaffected = "error: No data";
                    using (NpgsqlDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            rowsaffected = dr[0].ToString();
                        }
                    }

                    conn.Close();
                    return rowsaffected;

                }

            }
            
        }
I hope I placed this on the correct forum. Most of the code is c# but I feel the problem lies with Postgresql...

Thanks
Reply
Reply

Bookmarks


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

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles