0

I am developing an asp.net web application and I would like to import a fichier excel to postgresql. I'm using this code but it gives me this error. Can you help me? Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1056: Unexpected character '$'

Line 74: quer = "INSERT INTO poi (num_route, pk, geom) select num_route,pk_debut from [Sheet1$]"; Line 75: NpgsqlCommand cm = new NpgsqlCommand(quer, cnx); L ine 76: reader = cm.ExecuteReader();

using System;
using System.Configuration;
using System.IO;
using System.Data;
using System.Drawing;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Text;
using System.Xml;
using Npgsql;
using System.Collections.Specialized;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = "Please select an excel file first";
        lblMessage.Visible = false;
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if ((txtFilePath.HasFile))
        {
            
            OleDbConnection conn = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();
            OleDbDataAdapter da = new OleDbDataAdapter();
            DataSet ds = new DataSet();
            string query = null;
            string quer = null;
            string connString = "";
            string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
            string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();

            //Check file type
            if (strFileType == ".xls" || strFileType == ".xlsx")
            {
                txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
            }
            else
            {
                lblMessage.Text = "Seuls les fichiers Excel autorisés";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Visible = true;
                return;
            }

            string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);

            //Connection String to Excel Workbook
            if (strFileType.Trim() == ".xls")
            {
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (strFileType.Trim() == ".xlsx")
            {
                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }


            query = "SELECT num_route,pk_debut,pk_fin FROM [Sheet1$]";
            NpgsqlConnection cnx = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=****;");
            NpgsqlDataReader reader;
            cnx.Open();
            quer = "INSERT INTO poi (num_route, pk) select num_route,pk_debut from "Sheet1$"";
            NpgsqlCommand cm = new NpgsqlCommand(quer, cnx);
            reader = cm.ExecuteReader();
        
            //Create the connection object
            conn = new OleDbConnection(connString);
            //Open connection
            if (conn.State == ConnectionState.Closed) conn.Open();
            //Create the command object
            cmd = new OleDbCommand(query, conn);
            da = new OleDbDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);

            grvExcelData.DataSource = ds.Tables[0];
            grvExcelData.DataBind();

            lblMessage.Text = "Les données récupérées avec succès! Total de lignes:" + ds.Tables[0].Rows.Count;
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Visible = true;

            da.Dispose();
            conn.Close();
            conn.Dispose();
        }
        else
        {
            lblMessage.Text = "S'il vous plaît sélectionner un fichier excel";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
        }
    }
}
welliam
  • 13
  • 4

1 Answers1

0

I would suggest trying something like this

        //Connection String to Excel Workbook
        if (strFileType.Trim() == ".xls")
        {
            connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        else if (strFileType.Trim() == ".xlsx")
        {
            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }

        var Builder = new StringBuilder("INSERT INTO poi (num_route, pk) select num_route,pk_debut VALUES ");

        bool first = true;
        using (var conn = new OleDbConnection(connString))
        {
            if (conn.State == ConnectionState.Closed) conn.Open();
            using (var cmd = new OleDbCommand("SELECT num_route,pk_debut,pk_fin FROM [Sheet1$]", conn))
            using (var da = cmd.ExecuteReader())
            {
                while (da.Read())
                {
                    if (!first) Builder.Append(",");
                    Builder.Append("(");
                    Console.WriteLine(da[0]);
                    Builder.Append(",");
                    Console.WriteLine(da[1]);
                    Builder.Append(",");
                    Console.WriteLine(da[2]);
                    Builder.Append(")");
                    first = false;
                }
            }
        }

        using (NpgsqlConnection cnx = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=****;"))
        {
            cnx.Open();
            var quer = Builder.ToString();
            using (NpgsqlCommand cm = new NpgsqlCommand(quer, cnx))
                cm.ExecuteNonQuery();
        }
Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34
  • Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1056: Unexpected character '$' Source Error: Line 72: NpgsqlDataReader reader; Line 73: cnx.Open(); Line 74: quer = "INSERT INTO poi (num_route, pk, geom) select num_route,pk_debut from "Sheet1$""; Line 75: NpgsqlCommand cm = new NpgsqlCommand(quer, cnx); Line 76: reader = cm.ExecuteReader(); – welliam Mar 15 '16 at 01:44
  • @welliam it seems that the table "sheet1" does not exist on the server – Diligent Key Presser Mar 15 '16 at 01:53
  • what you're trying to achieve does not work this way. See http://stackoverflow.com/questions/46324/possible-to-perform-cross-database-queries-with-postgres – Diligent Key Presser Mar 15 '16 at 02:00
  • i whould import the table fom excel to posgresql can you help me! – welliam Mar 15 '16 at 02:04
  • @welliam updated an answer, this approach should work – Diligent Key Presser Mar 15 '16 at 02:39
  • yes i'm trying to using this but i can't have the solurion it can't compiled .......... because the var in the using – welliam Mar 15 '16 at 09:44
  • @welliam post an exact problem please – Diligent Key Presser Mar 15 '16 at 10:41
  • yes i'm not at my pc but it say that we can't try with var cmd, var da in the using for example and it say we can't writing in the console like this Console.WriteLine(da[0]); please if you have any idea save me!!!!!!! – welliam Mar 15 '16 at 11:06