Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#ASP.Net中将表格数据从Excel上传到SQL Server 2008_C#_Asp.net - Fatal编程技术网

如何在C#ASP.Net中将表格数据从Excel上传到SQL Server 2008

如何在C#ASP.Net中将表格数据从Excel上传到SQL Server 2008,c#,asp.net,C#,Asp.net,我几天前就做过了。下面的代码适用于我 protected void btnup_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { if (System.IO.Path.GetExtension(FileUpload1.FileName) == ".xls" || System.IO.Path.GetExtension(FileUpload1.FileName) == ".xlsx")

我几天前就做过了。下面的代码适用于我

protected void btnup_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        if (System.IO.Path.GetExtension(FileUpload1.FileName) == ".xls" || System.IO.Path.GetExtension(FileUpload1.FileName) == ".xlsx")
        {
            FileUpload1.SaveAs(Server.MapPath("~/Excal/sample.xlsx"));
        }
    }
}
protected void Button2_Click(object sender, EventArgs e)
{ 
   string sSourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\sample.xlsx; Extended Properties=""Excel 12.0;HDR=YES;""";
    string sDestConstr = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;

    OleDbConnection sSourceConnection = new OleDbConnection(sSourceConstr);
    using (sSourceConnection)
    {
        string sql = string.Format("Select [ID],[Name],[Designation] FROM [{0}]", "sample$");
        OleDbCommand command = new OleDbCommand(sql, sSourceConnection);
        sSourceConnection.Open();
        using (OleDbDataReader dr = command.ExecuteReader())
        {
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sDestConstr))
            {
                bulkCopy.DestinationTableName = "rr";
                //You can mannualy set the column mapping by the following way.
                //bulkCopy.ColumnMappings.Add("MSISDN", "MSISDN");
                bulkCopy.WriteToServer(dr);
            }
        }
    }
    lblmsg.Text = "Record update";
}

这有什么问题?
   try
        {

           string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                               "Data Source=" + Path of File + ";" +
                              @"Extended Properties=""Excel 12.0 Xml;HDR=Yes""";

            //Create Connection to Excel work book
            OleDbConnection excelConnection =
            new OleDbConnection(excelConnectionString);

            //Create OleDbCommand to fetch data from Excel
            OleDbCommand cmd = new OleDbCommand
            ("Select * from [Sheet1$]",
            excelConnection);

            excelConnection.Open();

            OleDbDataReader dReader;
            dReader = cmd.ExecuteReader();

            SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
            sqlBulk.DestinationTableName = "ExcelTable";  // write your table name

            //sqlBulk.ColumnMappings.Add("ID", "ID");
            //sqlBulk.ColumnMappings.Add("Name", "Name");
            sqlBulk.WriteToServer(dReader);


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }