I'm new to c# and didn't write any c# code yet. I'm using postgres database. I want to connect to postgres and retriew some data to test my database connectivity. This is my code.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Npgsql;
namespace PostgreSQLTEst
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
// PostgeSQL-style connection string
string connstring = String.Format("Server=xxx;Port=xxx;User Id=xxx;Password=xxx;Database=xxx;");
// Making connection with Npgsql provider
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
string sql = "SELECT * FROM simple_table";
conn.Close();
}
catch (Exception msg)
{
MessageBox.Show(msg.ToString());
//throw;
}
}
}
}
Comment