0

I am converting a WCF service with a SQL Server backend to PostgreSQL. I am using Npgsql but it does not have a bulk copy option. test is the dataset here.

This is my sql code,

        DataSet ds = DuobaseHandler.ToDataSet(test);
        DataTable dt = ds.Tables[0];
        string TableNameOrig = "CDA." + TableName;
        DataTable dataTable = dt; 
        try
        {
            ConnectionStringSettings mConString = ConfigurationManager.ConnectionStrings["CommonDataConnection"];
            string[] columnNames = dt.Columns.Cast<System.Data.DataColumn>()
                     .Select(x => x.ColumnName)
                     .ToArray();
            string[] ColumnsFinal = columnNames.Take(columnNames.Count() - 2).ToArray();
            using (SqlConnection ConnectionSQL = new SqlConnection(mConString.ConnectionString))
            {
                ConnectionSQL.Open();
                // Delete old entries
                SqlCommand truncate = new SqlCommand("TRUNCATE TABLE" + " " + "CDA." + TableName, ConnectionSQL);
                truncate.ExecuteNonQuery();


            }
            SqlBulkCopy bulkCopy = new SqlBulkCopy(mConString.ConnectionString, SqlBulkCopyOptions.TableLock)
            {
                DestinationTableName = TableNameOrig,
                BatchSize = 100000,
                BulkCopyTimeout = 360
            };
            foreach (var item in ColumnsFinal)
            {
                bulkCopy.ColumnMappings.Add(item.ToString(), item.ToString());
            }

            bulkCopy.WriteToServer(dataTable);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return true;
3
  • The product is called PostgreSQL (see postgresql.org) - and NOT "posegresql" as you keep insisting on misspelling it .... Please stop undoing my fixes to you post - I'm trying to make it more useful and readable here Commented Jul 20, 2014 at 16:49
  • @marc_s Yes, i wrongly rejected your edit, it came in the middle while reviewing others posts sorry for that. you can edit now Commented Jul 20, 2014 at 16:51
  • nPgSQL does have a bulk copy feature, it just doesn't have the same interface. See NpgsqlCopyIn. It would be interesting to write a wrapper to support the MS SQL Server interfaces instead, but it doesn't look like anyone's done it. Commented Jul 21, 2014 at 1:54

1 Answer 1

2

nPgSQL does have a bulk copy feature, it just doesn't have the same interface. See NpgsqlCopyIn.

It would be interesting to write a wrapper to support the MS SQL Server interfaces instead, but it doesn't look like anyone's done it, so you'll need to convert to the nPgSQL interface or write a compatibility wrapper.

This blog might be useful.

Sign up to request clarification or add additional context in comments.

1 Comment

Ya i know that npsqlcopyin would work, but it needs a columns of type string, will try it anyways. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.