Represents a PostgreSQL large object.
The following example shows how to create and manage large objects with
PgSqlLargeObject.
public static void Main(string[] args) {
PgSqlConnection connection = new PgSqlConnection("user id=postgres;password=postgres;port=5432;host=localhost;");
connection.Open();
int oid = CreateLargeObject(connection);
WriteToLargeObject(connection, oid);
ReadFromLargeObject(connection, oid);
Console.ReadLine();
}
public static int CreateLargeObject(PgSqlConnection connection) {
PgSqlLargeObject lo = new PgSqlLargeObject(connection);
// Create a new large object in the database.
lo.Create();
Console.WriteLine("Created new large object with OID = {0}", lo.Oid);
return lo.Oid;
}
public static void WriteToLargeObject(PgSqlConnection connection, int oid) {
// All operations with large objects require an active transaction.
connection.BeginTransaction();
string str = "Hello, world!";
PgSqlLargeObject lo = new PgSqlLargeObject(connection, oid);
lo.Open();
if (lo.CanWrite) {
byte[] buffer = Encoding.UTF8.GetBytes(str);
lo.Write(buffer, 0, buffer.Length);
}
lo.Close();
Console.WriteLine("Written to the large object : {0}", str);
connection.Commit();
}
public static void ReadFromLargeObject(PgSqlConnection connection, int oid) {
// All operations with large objects require an active transaction.
connection.BeginTransaction();
string str = string.Empty;
PgSqlLargeObject lo = new PgSqlLargeObject(connection, oid);
lo.Open();
if (lo.CanWrite) {
byte[] buffer = new byte[lo.Length];
lo.Read(buffer, 0, buffer.Length);
str = Encoding.UTF8.GetString(buffer);
}
lo.Close();
Console.WriteLine("Read from the large object : {0}", str);
connection.Commit();
}
Public Sub Main()
Dim connection As New PgSqlConnection("user id=postgres;password=postgres;port=5432;host=localhost;")
connection.Open()
Dim oid As Integer = CreateLargeObject(connection)
WriteToLargeObject(connection, oid)
ReadFromLargeObject(connection, oid)
Console.ReadLine()
End Sub
Public Function CreateLargeObject(ByVal connection As PgSqlConnection) As Integer
Dim lo As New PgSqlLargeObject(connection)
' Create a new large object in the database.
lo.Create()
Console.WriteLine("Created new large object with OID = {0}", lo.Oid)
Return lo.Oid
End Function
Public Sub WriteToLargeObject(ByVal connection As PgSqlConnection, ByVal oid As Integer)
' All operations with large objects require an active transaction.
connection.BeginTransaction()
Dim str As String = "Hello, world!"
Dim lo As New PgSqlLargeObject(connection, oid)
lo.Open()
If lo.CanWrite Then
Dim buffer As Byte() = Encoding.UTF8.GetBytes(str)
lo.Write(buffer, 0, buffer.Length)
End If
lo.Close()
Console.WriteLine("Written to the large object : {0}", str)
connection.Commit()
End Sub
Public Sub ReadFromLargeObject(ByVal connection As PgSqlConnection, ByVal oid As Integer)
' All operations with large objects require an active transaction.
connection.BeginTransaction()
Dim str As String = String.Empty
Dim lo As New PgSqlLargeObject(connection, oid)
lo.Open()
If lo.CanWrite Then
Dim buffer As Byte() = New Byte(lo.Length - 1) {}
lo.Read(buffer, 0, buffer.Length)
str = Encoding.UTF8.GetString(buffer)
End If
lo.Close()
Console.WriteLine("Read from the large object : {0}", str)
connection.Commit()
End Sub