简体   繁体   中英

importing excel to sql c#

I have been trying to import an excel table into my sql database. I have tried this example:

     void importdata(string excelfilepath)
     {
        //declare variables - edit these based on your particular situation
        string ssqltable = "tTableExcel";
        // make sure your sheet name is correct, here sheet name is sheet1,      so you can change your sheet name if have
        string myexceldataquery = "select student,rollno,course from [sheet1$]";
        try
        {
           //create our connection strings
           string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelfilepath +
           ";extended properties=" + "\"excel 8.0;hdr=yes;\"";
           string ssqlconnectionstring = "server=mydatabaseservername;userid=dbuserid;password=dbuserpassword;database=databasename;connection reset=false";


           string sclearsql = "delete from " + ssqltable;
           SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
           SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
           sqlconn.Open();
           sqlcmd.ExecuteNonQuery();
           sqlconn.Close();
           //series of commands to bulk copy data from the excel file into our sql table

           OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
           OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
           oledbconn.Open();
           OleDbDataReader dr = oledbcmd.ExecuteReader();
           SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
           bulkcopy.DestinationTableName = ssqltable;
           while (dr.Read())
           {
              bulkcopy.WriteToServer(dr);
           }

           oledbconn.Close();
        }
        catch (Exception ex)
        {
           //handle exception
        }
     }

My programming is not very good and I cannot make it work. There is nothing wrong with the SQL connection (the db is cleared after running the program). But I am not able to get any information into the database. My excel file is called test.xlsx and is stored in D:\\Users\\Haners. My database tabe is called Test. How can I make my program import the information in the excel file to the database???

Previous Code :

       SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
       bulkcopy.DestinationTableName = ssqltable;
       while (dr.Read())
       {
          bulkcopy.WriteToServer(dr);
       }

New Code :

       SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
       bulkcopy.DestinationTableName = ssqltable;
       bulkcopy.BatchSize=100;
       bulkcopy.WriteToServer(dr);

Hope this help to resolve your issue. Happy coding. cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM