1
Answer

How to insert large no of excel sheet rows into database table? which is the best method?

Photo of srikanth

srikanth

14y
2.3k
1

Hi,
I have an excel sheet that contains around 30,000 rows and 18 colmns. These no of rows and columns may increase in future.
I need to read all these records from excel sheet and insert into a table in sql database. For reading the excel book I am using Oledbconnections.
The possible solutions I have known as per my knowledge, to insert the data are
1. To insert one record at a time which makes 30,000 database hits.
  -- How will this affect the performance?
2. To use liked servers - bu this is not working for me. So, the only option i have  is the first one.
Could you please suggest any other method considering the performance issues?

Answers (1)

1
Photo of Satyapriya Nayak
NA 53k 8m 13y
Hi Magesh,


Try this...

create table employee (eid int primary key identity,ename varchar(50),eaddress varchar(50))


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_ifnot_exits._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
 
  </div>
  <asp:Button ID="Button1" runat="server" Text="Availability of name from the database"
  BackColor="#FF99FF" Font-Bold="True" Width="329px"
  onclick="Button1_Click" /><br />
  <asp:Label ID="Labelcheck"  Text="Ename" runat="server" BackColor="#FFFF99"
  Width="197px" ForeColor="#FF3300"></asp:Label>
  <asp:TextBox ID="txtUserName" runat="server" Width="197px"></asp:TextBox>
 
  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
  ControlToValidate="txtUserName" ErrorMessage="*Name Required"></asp:RequiredFieldValidator><br />
 
 
  <asp:Label ID="Label1"  Text="Eaddress" runat="server" BackColor="#FFFF99"
  Width="197px" ForeColor="#FF3300"></asp:Label>
  <asp:TextBox ID="txtUserAddress" runat="server" Width="197px"></asp:TextBox>
 
  <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
  ControlToValidate="txtUserAddress" ErrorMessage="*Address Required"></asp:RequiredFieldValidator>
 
  <br />
  <asp:Label ID="lblMessage" runat="server" BackColor="#FF3300"
  ForeColor="Black"></asp:Label>
  </form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.SqlClient;
namespace Insert_data_ifnot_exits
{
  public partial class _Default : System.Web.UI.Page
  {
  string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlCommand com;
  string str = null;
 

  protected void Button1_Click(object sender, EventArgs e)
  {
  namecheck();
 

  }
  public void namecheck()
  {
  SqlConnection con = new SqlConnection(connStr);
  con.Open();
  str = "select count(*)from employee where ename='" + txtUserName.Text + "'";
  com = new SqlCommand(str, con);
  int count = Convert.ToInt32(com.ExecuteScalar());
  con.Close();
  if (count > 0)
  {
  lblMessage.Text = "Sorry! you can't take this username";
  }
  else
  {
  lblMessage.Text = "You can take this username";
  con.Open();
  str = "insert into employee(ename,eaddress) values('" + txtUserName.Text + "','" + txtUserAddress.Text + "')";
  com = new SqlCommand(str, con);
  com.ExecuteNonQuery();
  con.Close();

  }
 
  }
  }
}




Thanks
If this post helps you mark it as answer
1
Photo of Pravin Ghadge
NA 2.5k 358.5k 13y
Mangesh,

First, Check record is already exist or not . If it exist then show message record already exist, else use insert command.

For eg:
USe Select command:
cmd=new Sqlcommand("Select * from tblname where Customer_ID='"+txtCust.Text+"'")
Sqldatareader dr=cmd.ExecuteReader();
if(dr.hasrows)
{
use insert command
}
else
{
MEssbox.show("Record already exist")
}

0
Photo of Pravin Ghadge
NA 2.5k 358.5k 13y
Mangesh,
Just change ur code:

cmd=new Sqlcommand("Select * from tblname where Customer_ID='"+txtCust.Text+"'")
Sqldatareader dr=cmd.ExecuteReader();
if(dr.hasrows)
{
MEssbox.show("Record already exist")
}
else
{


}

0
Photo of magesh manavalan
NA 244 423.6k 13y
Hello sir. i try it your code put it shows the errors that error is:

Violation of PRIMARY KEY constraint 'PK_employee1'. Cannot insert duplicate key in object 'employee1'.
The statement has been terminated.




0
Photo of Pravin Ghadge
NA 2.5k 358.5k 13y
Hi Mangesh,

I can't get u. What u want?