Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nuZfP-00066k-Is for pgsql-bugs@arkaria.postgresql.org; Fri, 27 May 2022 13:06:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nuZfO-0007aj-3J for pgsql-bugs@arkaria.postgresql.org; Fri, 27 May 2022 13:06:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nuZfN-0007aa-RD for pgsql-bugs@lists.postgresql.org; Fri, 27 May 2022 13:06:09 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nuZfG-0003uR-VK for pgsql-bugs@lists.postgresql.org; Fri, 27 May 2022 13:06:09 +0000 Received: from bruce by momjian.us with local (Exim 4.94.2) (envelope-from ) id 1nuZfC-00FDau-59; Fri, 27 May 2022 09:05:58 -0400 Date: Fri, 27 May 2022 09:05:58 -0400 From: Bruce Momjian To: bhguo@163.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17500: Insert data with ODBC driver in VB.net failed Message-ID: References: <17500-36dc046dbbad9858@postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <17500-36dc046dbbad9858@postgresql.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk You should contact the ODBC maintainers: https://odbc.postgresql.org/faq.html#1.6 pgsql-odbc@postgresql.org --------------------------------------------------------------------------- On Fri, May 27, 2022 at 11:01:57AM +0000, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 17500 > Logged by: bob guo > Email address: bhguo@163.com > PostgreSQL version: 10.0 > Operating system: win7 > Description: > > Dear Sirs, > > I need to write data to Postgresql with ODBC driver in VB.net, all work fine > until today's problem. > > I have simplified the scene with a simple table, it has only two fields: id > and birthday, and I write only two rows of simple data, they are: > 2 null > 1 2022-1-2 > > When I load the data into a datatable dt and use adapter.Update(dt) to write > Postgresql, the program will raise an exception, it says in chinese: > 无法将类型为“System.DateTime”的对象强制转换为类型“System.Char[]” > > On the contrary, If I exchange the order of rows like this: > 1 2022-1-2 > 2 null > The program will work well. > > I can't image what's wrong with it. I have tried it with other database, > such as SQL Server, Access, MySQL, they all work fine. So I guess there is > some bug in Postgresql ODBC driver. Please check it, thanks a lot. > > Attach files: > > The create script of table: > CREATE TABLE schema1.table1 > ( > id bigint, > birthday timestamp without time zone > ) > WITH ( > OIDS = FALSE > ) > TABLESPACE pg_default; > > The simplified vb.net code: > Private Sub Button5_Click(sender As Object, e As EventArgs) Handles > Button5.Click > Try > > Dim conn As New OdbcConnection("Driver={PostgreSQL > Unicode};Port=5432;Server=127.0.0.1;Database=test;Uid=postgres;Pwd=123456;") > conn.Open() > > '1. Build a datatable with 2 rows of data > Dim dt As New DataTable > dt.Columns.Add("id", GetType(String)) > dt.Columns.Add("birthday", GetType(DateTime)) > dt.Rows.Add(New Object() {2}) > dt.Rows.Add(New Object() {1, New DateTime(2022, 1, 2)}) > > '2. Build a OdbcCommand > Dim com As New OdbcCommand("insert into schema1.table1 > (id,birthday) values(?,?)", conn) > Dim p1 As New OdbcParameter, p2 As New OdbcParameter > p1.SourceVersion = DataRowVersion.Current > p1.SourceColumn = "id" > p2.SourceVersion = DataRowVersion.Current > p2.SourceColumn = "birthday" > com.Parameters.Add(p1) > com.Parameters.Add(p2) > > '3. Perform insert action > Using adapter As New OdbcDataAdapter() > adapter.InsertCommand = com > adapter.Update(dt) > End Using > > '4. Close > dt.Clear() > conn.Close() > > Catch ex As Exception > 'An exception will raise, in chinese word it says: > 无法将类型为“System.DateTime”的对象强制转换为类型“System.Char[]” > MsgBox(ex.Message) > Finally > MsgBox("Finished!") > End Try > > End Sub > -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Indecision is a decision. Inaction is an action. Mark Batterson