2016-03-15 114 views
0

我正在開發一個asp.net web應用程序,我想導入一個更好的excel到postgresql。我使用這段代碼,但它給了我這個錯誤。你可以幫我嗎? 說明:編譯服務此請求所需的資源時發生錯誤。請查看以下具體的錯誤細節並適當修改您的源代碼。語法錯誤在或接近pgsl?

編譯器錯誤信息:CS1056:意外的字符 '$'

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; 
 
     } 
 
    } 
 
}

+0

我也全光照它的東西但他不能讀表[sheet1 $] !!!!!!! – welliam

回答

0

我會建議嘗試這樣

 //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(); 
     } 
+0

我也是用它,但他不能讀表[sheet1 $] !!!!!!! – welliam

+0

說明:編譯服務此請求所需的資源時發生錯誤。請查看以下具體的錯誤細節並適當修改您的源代碼。 編譯器錯誤消息:CS1056:意外字符 '$' 源錯誤: 72行:NpgsqlDataReader讀取器; 第73行:cnx.Open(); 第74行:quer =「插入到poi(num_route,pk,geom)select num_route,pk_debut from」Sheet1 $「」; 第75行:NpgsqlCommand cm = new NpgsqlCommand(quer,cnx); 第76行:reader = cm.ExecuteReader(); – welliam

+0

@welliam似乎表「sheet1」在服務器上不存在 –

相關問題