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 pgSqlConnectionString)
{
// If the connection string is empty, use default.
if(pgSqlConnectionString == "")
{
pgSqlConnectionString =
"host=server;database=test;user id=postgres;";
}
PgSqlConnection myConn = new PgSqlConnection(pgSqlConnectionString);
string myInsertQuery = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')";
PgSqlCommand pgSqlCommand = new PgSqlCommand(myInsertQuery);
pgSqlCommand.Connection = myConn;
myConn.Open();
try
{
pgSqlCommand.ExecuteNonQuery();
}
finally
{
myConn.Close();
}
} |
|
|---|
Public Sub InsertRow(pgSqlConnectionString As String)
' If the connection string is empty, use default.
If pgSqlConnectionString = "" Then
pgSqlConnectionString = _
"host=server;database=test;user id=postgres;"
End If
Dim myConn As New PgSqlConnection(pgSqlConnectionString)
Dim myInsertQuery As String = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')"
Dim pgSqlCommand As New PgSqlCommand(myInsertQuery)
pgSqlCommand.Connection = myConn
myConn.Open()
Try
pgSqlCommand.ExecuteNonQuery()
Finally
myConn.Close()
End Try
End Sub |