我正在开发一个asp.net Web应用程序,我想将一个fichier excel导入postgresql。我使用此代码但它给了我这个错误。你能帮助我吗? 描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并相应地修改源代码。
编译器错误消息:CS1056:意外字符' $'
第74行:quer =" INSERT INTO poi(num_route,pk,geom) 从[Sheet1 $]"中选择num_route,pk_debut; 第75行:NpgsqlCommand cm = new NpgsqlCommand(quer,cnx);大号 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 :(得分: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();
}