C# Import xls file into sql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • upadhyayanuj
    New Member
    • Dec 2007
    • 5

    C# Import xls file into sql server

    Hi can anybody tell what is error in following code, its running through .Net 2005 editor fine,
    but when running on browser gives error:
    No error message available, result code: E_FAIL(0x800040 05).
    [code=cpp]
    try
    {
    // Create Connection to Excel Workbook

    OleDbConnection connection = new OleDbConnection (excelConnectio nString);

    if (connection.Sta te == ConnectionState .Closed)
    {

    connection.Open ();
    }

    OleDbCommand command = new OleDbCommand("s elect Name,Age,City,P hone FROM [Data$]", connection);

    // Create DbDataReader to Data Worksheet

    OleDbDataReader dr = command.Execute Reader();

    // SQL Server Connection String
    string sqlConnectionSt ring = "Data Source=localhos t;database=demo ;user id=sa;pwd=sa";

    // Bulk Copy to SQL Server

    SqlBulkCopy bulkcopy = new SqlBulkCopy(sql ConnectionStrin g);

    bulkcopy.Destin ationTableName = "ExcelData" ;

    // bulkCopy.WriteT oServer(dr);
    bulkcopy.WriteT oServer(dr);
    bulkcopy.Close( );
    if (!dr.IsClosed)
    dr.Close();

    Response.Write( "Data Imported From Xls file");
    if (connection.Sta te == ConnectionState .Open)
    connection.Clos e();
    }

    catch (Exception ex)
    {
    Response.Write( ex.Message);
    }[/code]
    Last edited by Frinavale; Dec 14 '07, 06:59 PM. Reason: Added [code] tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I think it might have something to do with your connection string.

    See this article. It could really help you out.

    Comment

    • upadhyayanuj
      New Member
      • Dec 2007
      • 5

      #3
      Originally posted by Frinavale
      I think it might have something to do with your connection string.

      See this article. It could really help you out.
      Thanks Frinavale,

      It works,
      I had not put the file in my application folder,
      I had just put the file in my application folder and its worked

      Thanks for help

      Comment

      • divyavk21
        New Member
        • Sep 2008
        • 6

        #4
        I'm currently struggling to upload the Excel sheet data to the SQL Server using .Net. Please help me in provide the source code for it. I'll be very thnakful for the timely help.


        Originally posted by upadhyayanuj
        Thanks Frinavale,

        It works,
        I had not put the file in my application folder,
        I had just put the file in my application folder and its worked

        Thanks for help

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by divyavk21
          I'm currently struggling to upload the Excel sheet data to the SQL Server using .Net. Please help me in provide the source code for it. I'll be very thnakful for the timely help.
          This forum is dedicated to helping you understand your problems so that you can create your own solution. Please attempt to solve the problem before asking for help with it so that we can address any difficulties you face.

          What have you tried so far?
          What problems have you encountered?

          Please post any code that you think might be causing the problem (remember to use code tags when posting code snippets)

          -Frinny

          Comment

          Working...