Only small record can I insert into the SQL Server database

advertisements

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