将Excellogging插入MS SQL数据库

我试图插入Excel数据到数据库,MS SQL。

目前我打开了excellogging并插入。 花了太长时间。

有没有办法将Excellogging插入数据库一次?

感谢致敬,

这是我的代码:

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); } 

如果数据很大,可以在SqlBulkCopy的帮助下做到这一点。

请检查以下post了解更多详情:

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); }