PostgreSQL: VB.NET ODBC UPDATE not working properly - vb.net

VB.NET 2019
Postgre 10
Postgre ODBC
Source data in DBeaver:
https://i86.fastpic.ru/big/2019/0716/b9/_8741664f3e9495646d995a9d96b846b9.png
Tried to change SQL state, change parameters passing:
'Dim command As New Odbc.OdbcCommand("UPDATE public.v8users SET Data = #data WHERE ID = #id", Connection) 'MSSQL syntax
'Dim command As New Odbc.OdbcCommand("UPDATE public.v8users SET data = ?, name = ? WHERE id = ?", Connection)
'Dim command As New Odbc.OdbcCommand("UPDATE public.v8users SET data = ?data WHERE id = ?id", Connection) 'MySQL syntax
'Dim command As New Odbc.OdbcCommand("UPDATE public.v8users SET data = ? WHERE id = decode(encode(?, 'hex'), 'hex')", Connection) 'PostgreSQL syntax
'command.Parameters.Add(New Odbc.OdbcParameter("#id", SQLUser.ID))
'command.Parameters.Add(New Odbc.OdbcParameter("#data", NewBytes))
'command.Parameters.Add(New Odbc.OdbcParameter("id", SqlDbType.Binary)).Value = SQLUser.ID
'command.Parameters.Add(New Odbc.OdbcParameter("data", SqlDbType.Binary)).Value = NewBytes
'command.Parameters.Add(New Odbc.OdbcParameter("name", Odbc.OdbcType.Binary)).Value = NewBytes
Structure SQLUser
Dim ID As Byte()
Dim IDStr As String
Dim Name As String
Dim Descr As String
Dim Data As Byte()
Dim DataStr As String
Dim PassHash As String
Dim PassHash2 As String
Dim AdmRole As String
Dim KeySize As Integer
Dim KeyData As Byte()
End Structure
Dim a
Dim Connection = New Odbc.OdbcConnection("Driver={PostgreSQL Unicode};Server=192.168.2.10;Database=TestPurp;Uid=<correct>;Pwd=<correct>;UseServerSidePrepare=1;ReadOnly=0")
Dim command As New Odbc.OdbcCommand("UPDATE public.v8users SET data = ? WHERE id = ?", Connection)
command.Parameters.Clear()
command.Parameters.Add(New Odbc.OdbcParameter("id", Odbc.OdbcType.Binary)).Value = SQLUser.ID
command.Parameters.Add(New Odbc.OdbcParameter("data", Odbc.OdbcType.Binary)).Value = NewBytes
a = command.ExecuteNonQuery()
a = 0, expect a = 1

Try adding the Parameters in the same order as they appear in the Update statement. First add data then id.
command.Parameters.Add(New Odbc.OdbcParameter("data", Odbc.OdbcType.Binary)).Value = NewBytes
command.Parameters.Add(New Odbc.OdbcParameter("id", Odbc.OdbcType.Binary)).Value = SQLUser.ID
This works the same way in Access with the OleDb provider.

Related

parameter is not valid for image.fromstream()

Dim strsql As String
get data from access database in vb.net
Dim stream As System.IO.MemoryStream
Dim img As Image
strsql = " select HotelName,HotelAddress,HotelPhoneNum,HotelPicture,HotelDesc from Hotel_3Star where ID = " & intIndex & ";"
Dim cmd As New OleDbCommand(strsql, connection)
Dim myreader As OleDbDataReader
myreader = cmd.ExecuteReader
myreader.Read()
Dim nameHotel As String = myreader("HotelName")
Dim phoneNumHotel As String = myreader("HotelPhoneNum")
Dim descHotel As String = myreader("HotelDesc")
Dim PictHotel = myreader("HotelPicture")
stream = New System.IO.MemoryStream(CType(PictHotel, Byte()))
it says that the parameter is not valid
img = Image.FromStream(stream) 'here the error detected
lblHotelName.Text = nameHotel
lblPhoneNum.Text = phoneNumHotel
lblHotelDesc.Text = descHotel
pcbHotel.Image = img
connection.Close()

Added collection of images from SQL datareader on ItextSharp

I need to get a collection of images from a datareader or even from a datatable (if recommended to use this) of sql.
My intent is to populate the cell with the variable "myimage" (and I succeed perfectly)
In the variable "myimage2" I would like to insert the collection of images obtained from a datareader or a datatable, what can I do?
Currently I can insert only one image using the "getvalue" method as written in the code.
Dim connessione As New SqlConnection
Dim comando As New SqlCommand
connessione.ConnectionString = stringa_database
connessione.Open()
comando = connessione.CreateCommand()
comando.CommandType = CommandType.Text
comando.CommandText = "SELECT u.id_nota, u.immagine From immagini_aggiuntive u INNER JOIN note q ON q.id = id_nota where id_nota= " & id
Dim lettura As SqlDataReader = comando.ExecuteReader()
lettura.Read()
Dim imageByte2 As Byte() = CType(lettura.GetValue(1), Byte())
Dim ms2 As New MemoryStream(imageByte2, 0, imageByte2.Length)
Dim myImage2 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(ms2)
'fine blocco
Dim imageByte As Byte() = CType(row.Cells(11).Value, Byte())
Dim ms As New MemoryStream(imageByte, 0, imageByte.Length)
Dim myImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(ms)
Dim cellimg As PdfPCell = New PdfPCell()
cellimg.AddElement(myImage)
cellimg.AddElement(myImage2)
pdfTable.AddCell(cellimg)
lettura.Close()
connessione.Close()

vb.net: column names using recordset as Odbc.OdbcDataReader

I'm creating a Web Service that retrive data from postgres DB.
Here my code:
Dim sql As String
Dim recordSet As Odbc.OdbcDataReader
Dim command As Odbc.OdbcCommand
Dim ret As New DataOutput
Dim dataLst As New List(Of Data)
Dim element As Dati
sql = "SELECT * from demo.table"
command = New Odbc.OdbcCommand(sql, odbcConn)
Try
recordSet = command.ExecuteReader
While recordSet.Read()
element = New Data
element.id = recordSet(0)
element.name = recordSet(1)
element.description = recordSet(2)
element.address = recordSet(3)
dataLst.Add(element)
End While
ret.arrDati = dataLst
This only show me rows value but I need column names too.
How can i do this?
You can access the column names with the GetName method:
Dim sql As String
Dim recordSet As Odbc.OdbcDataReader
Dim command As Odbc.OdbcCommand
Dim ret As New DataOutput
Dim dataLst As New List(Of Data)
Dim element As Dati
sql = "SELECT * from demo.table"
command = New Odbc.OdbcCommand(sql, odbcConn)
Try
recordSet = command.ExecuteReader
While recordSet.Read()
element = New Data
element.id = recordSet(0) 'column name: recordSet.GetName(0)
element.name = recordSet(1) 'column name: recordSet.GetName(1)
element.description = recordSet(2) 'column name: recordSet.GetName(2)
element.address = recordSet(3) 'column name: recordSet.GetName(3)
dataLst.Add(element)
End While
ret.arrDati = dataLst

Autogenerate alphanumeric ID

Need help with autogenerate ID. I have this code that autogenerates ID but is only good up to 10 increments. It starts with PO0001 and countinues up to PO0010 only, once it gets to PO0010 the ID gets stuck in PO0001.
here is the code I used:
Public Function newPOID(prefix As String, storedProcedure As String) As String
Dim newId As String = prefix + "0001"
Dim adpt As New SqlDataAdapter
Dim ds As New DataSet
Dim dr As SqlDataReader
Dim conn As New SqlConnection
conn.ConnectionString = conString
utos = New SqlCommand(storedProcedure, conn)
utos.CommandType = CommandType.StoredProcedure
conn.Open()
dr = utos.ExecuteReader
If dr.Read Then
If dr.IsDBNull(0) Then
Dim num As Integer = 1
'Dim prefix As String = "PO"
Dim append As String = prefix + num.ToString().PadLeft(4, "000")
newId = append
Else
Dim POstring As String = dr(0).ToString.Substring(0, 3)
Dim POID As Integer = dr(0).ToString.Substring(5) + 1
Dim append As String = POstring + POID.ToString().PadLeft(3, "000")
newId = append
End If
conn.Close()
End If
Return newId
End Function
Replace num.ToString().PadLeft(4, "000") with Format(num,"000#)

Provider type could not be represented as a .NET type OracleTypeException

So I have an application that is writing to an oracle database, then another that is reading the data from the same database.
When I get to the line Dim msgTime As TimeSpan = reader.GetTimeSpan(2), I get an exception (see below).
The Oracle Documentation says that INTERVAL DAY TO SECOND (which is how I'm storing the data in the DB) can be converted to timespan (see here)
Does anyone know what causes this exception, and how to avoid it?
Thanks.
Exception:
Oracle.DataAccess.Types.OracleTypeException
Provider type could not be represented as a .NET type
at Oracle.DataAccess.Types.TimeSpanConv.GetTimeSpan(OpoITLValCtx* pValCtx, OracleDbType oraType)
at Oracle.DataAccess.Client.OracleDataReader.GetTimeSpan(Int32 i)
at MyProgram.pollDatabase(Object sender, DoWorkEventArgs e)
Write to DB code:
Dim oCommand As New OracleCommand("INSERT INTO LOGTABLE(PK, MID,MDATE,MTIME,STATUS,SEVERITY,ORIGQ,MESSAGE) VALUES (:pk, :msgid, :msgdate, :msgtime, :status, :severity, :message)")
oCommand.Parameters.Add("pk", OracleDbType.Varchar2, Guid.NewGuid().ToString().Substring(0, 12), ParameterDirection.Input)
oCommand.Parameters.Add("msgid", OracleDbType.Varchar2, message.MessageID, ParameterDirection.Input)
oCommand.Parameters.Add("msgdate", OracleDbType.Date, putDateSQL, ParameterDirection.Input)
oCommand.Parameters.Add("msgtime", OracleDbType.IntervalDS, putTimeSQL, ParameterDirection.Input)
oCommand.Parameters.Add("status", OracleDbType.Varchar2, "NEW", ParameterDirection.Input)
oCommand.Parameters.Add("severity", OracleDbType.Varchar2, messageSeverity, ParameterDirection.Input)
oCommand.Parameters.Add("message", OracleDbType.Clob, clob, ParameterDirection.Input)
Read from DB Code:
Dim conn As OracleConnection = New OracleConnection(oradb)
Dim oCommand As New OracleCommand("SELECT MID,MDATE,MTIME,STATUS,SEVERITY, ORIGQ, MESSAGE FROM LOGTABLE")
oCommand.CommandType = CommandType.Text
oCommand.Connection = conn
oCommand.Connection.Open()
Dim reader As Oracle.DataAccess.Client.OracleDataReader = oCommand.ExecuteReader()
If reader.HasRows Then
While reader.Read()
Try
Dim messageID As String = reader.GetString(0)
Dim msgDate As Date = reader.GetDateTime(1)
If Not reader.IsDBNull(2) Then
Dim msgTime As TimeSpan = reader.GetTimeSpan(2)
End If
Dim msgStatus As String = reader.GetString(3)
Dim msgSeverity As String = reader.GetString(4)
Dim msgOrigin As String = reader.GetString(5)
Dim msgContent As String = reader.GetString(6)
Catch ex As Exception
Console.Out.WriteLineAsync(ex.Message)
End Try
End While
End If
I needed to use Dim msgTimeInterval As Oracle.DataAccess.Types.OracleIntervalDS = reader.GetOracleIntervalDS(2) instead of Dim msgTime As TimeSpan = reader.GetTimeSpan(2)