将excel记录插入MS SQL数据库

时间:2014-07-16 07:57:40

标签: sql-server excel

我尝试将excel数据插入数据库MS SQL。

目前我循环使用excel记录并插入。花了太长时间。

有没有办法将excel记录一次插入数据库?

谢谢和问候,

这是我的代码:

User user = new User();
cmd_obj = new OleDbCommand("SELECT * FROM [Sheet1$]", con_obj);
OleDbDataReader dr = cmd_obj.ExecuteReader();

while (dr.Read())
{
   int blnBadSyntax = 0;
   int blnBadDomain = 0;
   int blnBadSMTP = 0;
   int blnGreylisted = 0;
   int blnBadMailbox = 0;
   bool blnIsValid = false;

   string key = "2CH3W-7ENLC-FWLZ4-WEUVY-JRQ11-AU69U-W63V5-ULF1C-DA5RC-RU7XS-XK6JY-6JT5U-MYLX";
   MXValidate.LoadLicenseKey(key);
   MXValidate mx = new MXValidate();
   mx.LogInMemory = true;
   mx.CheckLiteralDomain = true;
   mx.CheckGreylisting = true;
   try
   {
      MXValidateLevel level = mx.Validate(user.StrEmailId, MXValidateLevel.Mailbox);
      switch (level)
      {
         case MXValidateLevel.NotValid:
         blnBadSyntax = 1;
         break;

         case MXValidateLevel.Syntax:
         blnBadDomain = 1;
         break;

         case MXValidateLevel.MXRecords:
         blnBadSMTP = 1;
         break;

         case MXValidateLevel.SMTP:
         blnGreylisted = 1;
         blnIsValid = true;
         break;

         case MXValidateLevel.Greylisted:
         blnBadMailbox = 1;
         blnIsValid = true;
         break;

         case MXValidateLevel.Mailbox:
         blnIsValid = true;
         break;
     }

     user.BlnBadSyntax = blnBadSyntax;
     user.BlnBadDomain = blnBadDomain;
     user.BlnBadSMTP = blnBadSMTP;
     user.BlnGraylisted = blnGreylisted;
     user.BlnBadMailBox = blnBadMailbox;
     if (blnIsValid)
     {
        user.StrStatus = "Valid";
     }
     else
     {
        user.StrStatus = "InValid";
        logFile.writeLog(mx.GetLog());
     }
   }
   catch (DnsException ex)
   {
      logFile.writeLog(mx.GetLog());
   }
InsertuserDetails(user);
}

1 个答案:

答案 0 :(得分:1)

如果数据很大,您可以在SqlBulkCopy的帮助下完成此操作。

请查看以下帖子了解更多详情:

http://technico.qnownow.com/bulk-copy-data-from-excel-to-destination-db-using-sql-bulk-copy/

// Connection String to Excel Workbook
            string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Book1.xls;ExtendedProperties=""Excel 8.0;HDR=YES;""";

            // Create Connection to Excel Workbook
            using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
            {
                OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Data$]", connection);

                connection.Open();

                // Create DbDataReader to Data Worksheet
                using (DbDataReader dr = command.ExecuteReader())
                {
                    // SQL Server Connection String
                    string sqlConnectionString = "Data Source=.;Initial Catalog=Test;Integrated Security=True";

                    // Bulk Copy to SQL Server
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                    {
                        bulkCopy.DestinationTableName = "ExcelData";
                        bulkCopy.WriteToServer(dr);
                    }