Represents an open connection to PostgreSQL.
The following example creates a
PgSqlCommand and a
PgSqlConnection. The
PgSqlConnection is opened and set as the
Connection property. The example then calls
ExecuteNonQuery method, and closes the connection. To accomplish this, the
ExecuteNonQuery is passed a connection string and a query string that is SQL INSERT statement.
public void InsertRow(string pgConnectionString)
{
// If the connection string is empty, use default.
if(pgConnectionString == "")
{
pgConnectionString =
"host=server;database=test;user id=postgres;";
}
PgSqlConnection myConn = new PgSqlConnection(pgConnectionString);
string myInsertQuery = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')";
PgSqlCommand pgCommand = new PgSqlCommand(myInsertQuery);
pgCommand.Connection = myConn;
myConn.Open();
try
{
pgCommand.ExecuteNonQuery();
}
finally
{
myConn.Close();
}
}
Public Sub InsertRow(pgConnectionString As String)
' If the connection string is empty, use default.
If pgConnectionString = "" Then
pgConnectionString = _
"host=server;database=test;user id=postgres;"
End If
Dim myConn As New PgSqlConnection(pgConnectionString)
Dim myInsertQuery As String = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')"
Dim pgCommand As New PgSqlCommand(myInsertQuery)
pgCommand.Connection = myConn
myConn.Open()
Try
pgCommand.ExecuteNonQuery()
Finally
myConn.Close()
End Try
End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2