【问题标题】:how to read a select statement into a c# datatable如何将select语句读入c#数据表
【发布时间】:2017-01-31 04:12:38
【问题描述】:

我正在制作一个 asp.net 网站,它显示来自上传的 excel 文件的数据表

我想将 excel 列“Ciudad”和“ClienteProveedr”中的名称与具有相同名称的表(这些表来自 sql server 数据库)进行比较,所以我想显示两个数据表中的 id数据表区域。

这是我正在制作的示例代码。

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack && Upload.HasFile)
        {
            if (Path.GetExtension(Upload.FileName).Equals(".xlsx"))
            {
                var excel = new ExcelPackage(Upload.FileContent);
                var Facturas = excel.ToDataTable();

                //fix ClienteProveedor 
                Facturas.Columns.Add("Proveedor", typeof(Int32));
                Facturas.Columns.Add("MasivoFacturacion_Solicitantes", typeof(Int32));
                Facturas.Columns.Add("MasivoFacturacion_Empresa", typeof(Int32));
                Facturas.Columns.Add("MasivoFacturacion_EmpresaMensajeria", typeof(Int32));
                Facturas.Columns.Add("idCiudad", typeof(Int32));
                Facturas.Columns.Add("MasivoFacturacion_ClienteProveedor", typeof(Int32));
                foreach (DataRow rw in Facturas.Rows)
                {
                    rw["Proveedor"] = Responsable.SelectedIndex;
                    rw["MasivoFacturacion_Solicitantes"] = DropDownListSolicitantes.SelectedIndex + 1;
                    rw["MasivoFacturacion_Empresa"] = DropDownListEmpresa.SelectedIndex + 1;
                    rw["MasivoFacturacion_EmpresaMensajeria"] = DropDownListEmpresaMensajeria.SelectedIndex + 1;
                    rw["MasivoFacturacion_ClienteProveedor"] = /*row for IdClienteProveedor*/;
                    rw["MasivoFacturacion_ClienteProveedor"] = /*row for IdCIudad*/;
                }
                gvData.DataSource = Facturas;
                gvData.DataBind();
                ViewState["dtFacturas"] = Facturas;

            }
        }
    }

【问题讨论】:

  • excel.ToDataTable 返回什么?它实际上是一个数据表吗?如果是这样,您不需要手动添加列并遍历行。只需设置 DataSource 并绑定它。
  • 是的,excel.ToDataTable方法将数据从exce文件导入到数据表中

标签: c# asp.net sql-server


【解决方案1】:
private DataTable ReadExcelFile(string sheetName, string path)
{

using (OleDbConnection conn = new OleDbConnection())
{
DataTable dt = new DataTable();
string Import_FileName = path;
string fileExtension = Path.GetExtension(Import_FileName);
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Import_FileName + ";" + "Extended Properties='Excel 12.0 Xml;HDR=YES;'";
using (OleDbCommand comm = new OleDbCommand())
{
    comm.CommandText = "Select * from [" + sheetName + "$]";

    comm.Connection = conn;

    using (OleDbDataAdapter da = new OleDbDataAdapter())
    {
        da.SelectCommand = comm;
        da.Fill(dt);
        return dt;
    }

}
}

您可以使用上述代码将excel读取到数据表中,并从数据表中获取所需的列。

【讨论】:

    【解决方案2】:

    由于您的方法返回一个 DataTable,您可以使这变得更简单。这样的东西应该非常接近您的需要。

    protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack && Upload.HasFile)
            {
                if (Path.GetExtension(Upload.FileName).Equals(".xlsx"))
                {
                    DataTable Facturas = new ExcelPackage(Upload.FileContent).ToDataTable();
                    gvData.DataSource = Facturas;
                    gvData.DataBind();
                    ViewState["dtFacturas"] = Facturas;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2019-12-10
      • 2020-10-11
      • 2013-12-25
      • 2016-09-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      相关资源
      最近更新 更多