Click here to Skip to main content
15,888,205 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

How to import excel sheet data into PostgreSQL database table in C# in Windows
application.

thanks advance...
Posted
Comments
[no name] 3-Mar-15 8:54am    
Please post your question either here or in a discussion forum but not both. In the discussion forum you already got a response from me. Did you not like it?
Maciej Los 3-Mar-15 9:12am    
What have you done till now? Where are you stuck?
sandy reddy 5-Mar-15 1:20am    
please post the total source code
Member 15190313 12-May-21 9:20am    
did you got the source code for How to import excel sheet data into PostgreSQL database table in C#
sandy reddy 5-Mar-15 3:37am    
I wrote below code but i am getting like this,,,,ERROR: 22021: invalid byte sequence for encoding "UTF8": 0x00



NpgsqlConnection conn = new NpgsqlConnection("Server=XXX.XX.X.XX; Port=5432; User Id=XXXX; Password=XXXXX; Database=DB_xxxx ;SyncNotification=true");
conn.Open();
NpgsqlCommand command = new NpgsqlCommand("COPY tbl_medicines(s_no,product_id,medicine_name,type_of_medicine,strength,created_date) FROM 'D:\\shared\\MedicinesNew2.csv' WITH DELIMITER ',' CSV HEADER ENCODING 'UTF8' ", conn); //WITH DELIMITER ';' CSV HEADER ENCODING 'UTF8' //with delimiter ',' csv header encoding 'WIN1252'
NpgsqlCopyIn cin = new NpgsqlCopyIn( command, conn );

Stream inStream = Console.OpenStandardInput();
// Encoding inEncoding = System.Text.Encoding.ASCII;
Encoding inEncoding = System.Text.Encoding.ASCII ;

Encoding serverEncoding = System.Text.Encoding.BigEndianUnicode; // example assumption

try
{
cin.Start();
Stream copyInStream = cin.CopyStream;
byte[] buf = new byte[9];
int i;
while( (i = inStream.Read(buf,0,buf.Length)) > 0 )
{

buf = System.Text.Encoding.Convert( inEncoding, serverEncoding, buf, 0, i );
copyInStream.Write( buf, 0, i );
}
copyInStream.Close(); // or cin.End(), if you wish
}
catch(Exception e)
{
try
{
cin.Cancel("Undo copy"); // Sends CopyFail to server
}
catch(Exception e2)
{
// we should get an error in response to our cancel request:
if( ! (""+e2).Contains("Undo copy") )
{
throw new Exception("Failed to cancel copy: " + e2 + " upon failure: " + e);
}
}
throw e;
}

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900