简体   繁体   中英

Only little record can I insert in to SQL Server database

I tried to import 200,000 records from Excel to SQL Server 2005 using ASP.NET with C#. But when I try to browse and import the file, only few records are inserted (100 records only). I could not get any solution.

Here is my sample code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;



public partial class UploadExcel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
///***when the button press, this code will execute***
    protected void Upload_Click(object sender, EventArgs e)
    {

        int count = 0;          
        try
        {
            string path = string.Concat(Server.MapPath("~/TempFiles/"), UploadExcel1.FileName);
            //Save File as Temp then you can delete it if you want
            UploadExcel1.SaveAs(path);
            DataTable dtExcel = new DataTable();
            string SourceConstr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0",path);
            OleDbConnection con1 = new OleDbConnection(SourceConstr);
            string query = "Select * from [Sheet1$]";
            OleDbDataAdapter dataadapt = new OleDbDataAdapter(query,con1);
            dataadapt.Fill(dtExcel);
            ///connection string with in the database below
            string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["zena"].ToString();
            //all the excel file is stored in the data table here
            for (int i = 0; i < dtExcel.Rows.Count;  i++)
            {
               try
               {
                  SqlConnection sconn = new SqlConnection(sqlconn);
                  sconn.Open();
                  string insert_query12 = "insert into FCROperation (CallingNumber,OperatorId,OperatorName,CallReason) values("+dtExcel.Rows[i][0]+","+dtExcel.Rows[i][1]+",'"+dtExcel.Rows[i][2]+"','"+dtExcel.Rows[i][3]+"')";

                  SqlCommand cmd = new SqlCommand(insert_query12,sconn);
                  count+=cmd.ExecuteNonQuery();            
                }
                catch (Exception ex)
                {
                   //continue;             
                }
                //Label1.Text = ex.Message;
             }
            if (count == dtExcel.Rows.Count)
            {
               Label1.Text = "Success" + dtExcel.Rows.Count + count;
            }
            else
            {
                Label1.Text = "Failed" + dtExcel.Rows.Count + count;
                //Label1 = dtExcel.Rows.Count;
            }
        }
        catch (Exception ex)
        {
           Label1.Text = ex.Message;
           //throw ex;        
        }
        finally
        {              
        }
    }
}

I want ot recommend you to open connection only one time, not each time when you insert a row

using (SqlConnection sconn = new SqlConnection(sqlconn))
{
  sconn.Open();
  for (int i = 0; i < dtExcel.Rows.Count;  i++)
            {
               try
               {                                 
                  string insert_query12 = "insert into FCROperation (CallingNumber,OperatorId,OperatorName,CallReason) values("+dtExcel.Rows[i][0]+","+dtExcel.Rows[i][1]+",'"+dtExcel.Rows[i][2]+"','"+dtExcel.Rows[i][3]+"')";
                  SqlCommand cmd = new SqlCommand(insert_query12,sconn);
                  count+=cmd.ExecuteNonQuery();            
                }
             catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
               {
                errorMessages.Append("Index #" + i + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "Procedure: " + ex.Errors[i].Procedure + "\n");
               }
               Console.WriteLine(errorMessages.ToString());
            }
                catch (Exception ex)
                {
                   //continue;             
                }
                //Label1.Text = ex.Message;
             }
}

Replace your for loop code with this one to track all the rows causing exception. And add a breakpoint after the loop to inspect errors

Dictonary<int, Exception> errors = new Dictonary<int, Exception>();
for (int i = 0; i < dtExcel.Rows.Count; i++)
   {
      try
      {
          SqlConnection sconn = new SqlConnection(sqlconn);
          sconn.Open();
          string insert_query12 = "insert into FCROperation (CallingNumber,OperatorId,OperatorName,CallReason) values("+dtExcel.Rows[i][0]+","+dtExcel.Rows[i][1]+",'"+dtExcel.Rows[i][2]+"','"+dtExcel.Rows[i][3]+"')";

          SqlCommand cmd = new SqlCommand(insert_query12,sconn);
          count+=cmd.ExecuteNonQuery();

      }
      catch (Exception ex)
      {
            errors.Add(i, ex);
            //continue;

      }
      //Label1.Text = ex.Message;
   }

It seems that the Excel OLE DB driver is only finding the first 100 rows of the spreadsheet. This could be due to a variety of reasons. For example, here is one user who found that the Excel print area affected the driver . I suggest you cut and paste the Excel data into a fresh spreadsheet with minimum formatting and see if the situation improves.

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