19 Mar 2009

Timestamp With time zone and without time zone


TIMESTAMP without time zone is displayed at the same format as DATETIME,and its format is 'YYYY-MM-DD HH:MM:SS'.

TIMESTAMP with time zone is displayed datetime and time zone ,and its is 'YYYY-MM-DD HH:MM:SS ZZZ'.


How to create timestamp in Postgre Sql ?

1.With time zone

CREATE TABLE MyProject."Employee"

(

IDX bigint NOT NULL,

Name character varying(64) NOT NULL,

RegisterDate timestamp with time zone NOT NULL,

CONSTRAINT Employee_pkey PRIMARY KEY (idx)

)

ALTER TABLE MyProject."Employee"OWNER TO postgres;


2.Without time zone

CREATE TABLE MyProject."Employee"

(

IDX bigint NOT NULL,

Name character varying(64) NOT NULL,

RegisterDate timestamp without time zone NOT NULL,

CONSTRAINT Employee_pkey PRIMARY KEY (idx)

)

ALTER TABLE MyProject."Employee"OWNER TO postgres;


How to insert timestamp column in Postgre Sql(database) with C#.NET 2005?
1.With time zone

NpgsqlConnection NpgCon = new NpgsqlConnection(String.Format("Server={0};Port={1};" +
"User Id={2};Password={3};Database= postgres;",
txtServerName.Text,
txtPort.Text, txtUserID.Text, txtPassword.Text););

NpgsqlConnection NpgCon = new NpgsqlConnection(NpgCon );
try
{
NpgCon.Open();

string txtCommand;

txtCommand="INSERT INTO MyProject."Employee"(IDX , Name, RegisterDate )VALUES (" + txtEmpID.Text + ",'" + txtEmpName.Text + "','" + DTEnrollmentdate.Value.ToString("yyyy-MM-dd hh:mm:ss zzz") + "')";
NpgsqlCommand cmd = new NpgsqlCommand(txtCommand, NpgCon);
cmd.ExecuteNonQuery();
}
catch (System.Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
return;
}
finally
{
if (NpgCon.State == ConnectionState.Open)
NpgCon.Close();
}


2.Without time zone

NpgsqlConnection NpgCon = new NpgsqlConnection(String.Format("Server={0};Port={1};" +
"User Id={2};Password={3};Database= postgres;",
txtServerName.Text,
txtPort.Text, txtUserID.Text, txtPassword.Text););

NpgsqlConnection NpgCon = new NpgsqlConnection(NpgCon );
try
{
NpgCon.Open();

string txtCommand;

txtCommand="INSERT INTO MyProject."Employee"(IDX , Name, RegisterDate )VALUES (" + txtEmpID.Text + ",'" + txtEmpName.Text + "','" + DTEnrollmentdate.Value.ToString("yyyy-MM-dd hh:mm:ss") + "')";
NpgsqlCommand cmd = new NpgsqlCommand(txtCommand, NpgCon);
cmd.ExecuteNonQuery();
}
catch (System.Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
return;
}
finally
{
if (NpgCon.State == ConnectionState.Open)
NpgCon.Close();
}

16 Mar 2009

Connection string for Postgre SQL in C#.NET 2005


Connection string for Postgre SQL in C#.NET 2005 by Npsql

Npgsql

Type: .NET Framework Class Library
Usage: Npgsql.NpgsqlConnection

The Npgsql is based to exist this library (original version) in your project references.
Then define it in your class as next step:

using Npgsql;

How to use that

try
{
NpgsqlConnection NpgCon = new NpgsqlConnection(String.Format("Server={0};Port={1};" +
"User Id={2};Password={3};Database= postgres;",
txtServerName.Text,
txtPort.Text, txtUserID.Text, txtPassword.Text););

NpgCon.Open();
.
.
.
NpgCon.Close();

}
catch (Exception msg)
{
MessageBox.Show(msg.Message);
throw;

}