Example : Connect to a Cluster by Using .NET
The following example connects to a cluster and runs a sample query that returns system tables. It does
not show server authentication. It is not necessary to have data in your database to use this example.
This example uses
Npgsql, a .NET data provider for PostgreSQL
.
using System;
using System.Data;
using Npgsql; // http://npgsql.projects.pgfoundry.org/
namespace redshift.amazon.com.docsamples
{
class ConnectToClusterExample
{
public static void Main(string[] args)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
//Server, e.g. "examplecluster.xyz.us-east-1.redshift.amazonaws.com"
String Server = "***provide server name part of connection
string****";
//Port, e.g. "5439"
String Port = "***provide port***";
//MasterUserName, e.g. "'masteruser'". Note use of single quotes.
String MasterUsername = "***provide master user name***";
//MasterUserPassword, e.g. "'mypassword'". Note use of single
quotes.
String MasterUserPassword = "***provide master user password***";
//DBName, e.g. "dev"
String DBName = "***provide name of database***";
String query = "select * from information_schema.tables;";
try
{
// Create a PostgeSQL connection string.
string connstring = String.Format(
"Server={0};Database={1};" +
"UID={2};PWD={3};Port={4};SSL=true;Sslmode=Require",
Server, DBName, MasterUsername,
MasterUserPassword, Port);
// Make a connection using the Npgsql provider.
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
// Try a simple query.
string sql = query;
NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
API Version 2012-12-01
111
Amazon Redshift Management Guide
Connecting to a Cluster by Using .NET