Quantcast
Channel: Oracle, MySQL, Sybase, Informix and other databases
Viewing all articles
Browse latest Browse all 1350

Save data from excel to database

$
0
0

i have a excel sheet where total data is 150000 .

when i insert in to database it inserted but the problem is only 20000 data insert rest data not insert how to solve the problem this is my code

private void saveformxls()
{
lblmsg.Text = "";
try
{

int bankid = 0, stateid = 0, distid = 0, cityid = 0, flag = 1, id = 0, retval = 0;
DataTable dtbank = new DataTable();
DataSet ds = new DataSet();
if (fildetails.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(fildetails.FileName);

if (fileExtension == ".xls" || fileExtension == ".xlsx")
{
string fileLocation = Server.MapPath("/BankDetails/") + fildetails.FileName;

if (System.IO.File.Exists(fileLocation))
{


}
fildetails.SaveAs(fileLocation);
string excelConnectionString = string.Empty;
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
//connection String for xls file format.
if (fileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
//connection String for xlsx file format.
else if (fileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
//Create Connection to Excel work book and add oledb namespace
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
DataTable dt = new DataTable();

dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return;
}

String[] excelSheets = new String[dt.Rows.Count];
int t = 0;
//excel data saves in temp file here.
foreach (DataRow row in dt.Rows)
{
string x = row["TABLE_NAME"].ToString();
if (x != "Sheet1$_" && x != "Sheet2$_" && x != "Sheet3$_" && x != "Sheet4$_" && x != "Sheet5$_")
{
excelSheets[t] = row["TABLE_NAME"].ToString();
t++;
}
}
OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);

int totalsheet = excelSheets.Length;
for (int i = 0; i < totalsheet; i++)
{
string query = string.Format("Select * from [{0}]", excelSheets[i]);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
{
dataAdapter.Fill(ds);
}
}
}
if (fileExtension.ToString().ToLower().Equals(".xml"))
{
string fileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}

Request.Files["FileUpload"].SaveAs(fileLocation);
XmlTextReader xmlreader = new XmlTextReader(fileLocation);
// DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
}
for (int j = 0; j < ds.Tables.Count; j++)
{
for (int i = 0; i < ds.Tables[j].Rows.Count; i++)
{
////here check duplicate data if exist then  return the id else insert and return id  ///
if (!DBNull.Value.Equals(ds.Tables[j].Rows[i][0]))
{
dtbank = objbankbl.findbank(4, ds.Tables[j].Rows[i][0].ToString(), "", "", "");
if (dtbank.Rows.Count > 0)
{
bankid = Convert.ToInt32(dtbank.Rows[0]["ID"]);
}
else
{
string bankname = ds.Tables[j].Rows[i][0].ToString();
if (bankname != " " || bankname != null)
{
bankid = objbankbl.Insert(1, ds.Tables[j].Rows[i][0].ToString(), "", "", 0, "", 0);
}
}
}

////here check duplicate data if exist then  return the id  else insert and return id ///
if (!DBNull.Value.Equals(ds.Tables[j].Rows[i][8]))
{
dtbank = objbankbl.findbank(1, "", ds.Tables[j].Rows[i][8].ToString(), "", "");
if (dtbank.Rows.Count > 0)
{
stateid = Convert.ToInt32(dtbank.Rows[0]["ID"]);
}
else
{
stateid = objbankbl.Insert(2, "", ds.Tables[j].Rows[i][8].ToString(), "", 0, "", 0);
}
}

////here check duplicate data if exist then  return the id  else insert and return id////
if (!DBNull.Value.Equals(ds.Tables[j].Rows[i][7]))
{
dtbank = objbankbl.findbank(2, "", "", ds.Tables[j].Rows[i][7].ToString(), "");
if (dtbank.Rows.Count > 0)
{
distid = Convert.ToInt32(dtbank.Rows[0]["ID"]);
}
else
{
distid = objbankbl.Insert(3, "", "", ds.Tables[j].Rows[i][7].ToString(), stateid, "", 0);
}
}

////here check duplicate data if exist then  return the id  else insert and return id////
if (!DBNull.Value.Equals(ds.Tables[j].Rows[i][6]))
{
dtbank = objbankbl.findbank(3, "", "", "", ds.Tables[j].Rows[i][6].ToString());
if (dtbank.Rows.Count > 0)
{
cityid = Convert.ToInt32(dtbank.Rows[0]["ID"]);
}
else
{
cityid = objbankbl.Insert(4, "", "", "", stateid, ds.Tables[j].Rows[i][6].ToString(), distid);
}
}
////here check duplicate data if exist then  return the id  else insert and return id////
if (!DBNull.Value.Equals(ds.Tables[j].Rows[i][3]))
{
dtbank = objbankbl.findbank(5, ds.Tables[j].Rows[i][1].ToString(), "", "", "");
if (dtbank.Rows.Count > 0)
{
flag = 2;
id = Convert.ToInt32(dtbank.Rows[0]["ID"]);
retval = objbankbl.bankbranchInsertupdate(flag, id, bankid, stateid, distid, cityid, ds.Tables[j].Rows[i][3].ToString(), "", "", ds.Tables[j].Rows[i][1].ToString(), ds.Tables[j].Rows[i][2].ToString(), "", ds.Tables[j].Rows[i][4].ToString(), ds.Tables[j].Rows[i][5].ToString(), 0, "", false, "", "", "", "", "" ,"","","","","");
flag = 1;
id = 0;
bankid = 0;
stateid = 0;
distid = 0;
cityid = 0;
}
else
{
retval = objbankbl.bankbranchInsertupdate(flag, id, bankid, stateid, distid, cityid, ds.Tables[j].Rows[i][3].ToString(), "", "", ds.Tables[j].Rows[i][1].ToString(), ds.Tables[j].Rows[i][2].ToString(), "", ds.Tables[j].Rows[i][4].ToString(), ds.Tables[j].Rows[i][5].ToString(), 0, "", false, "", "", "", "", "", "", "", "", "", "");
totalbranch(bankid);
}
}
}
}
if (retval > 0)
{
Response.Redirect("ManageBankBranch.aspx");
}
}
}
catch {

}
}

after insert 20000  the page redirect to the ManageBankBranch.aspx  as i code

but only insert  20000 , not insert full data 

how to solve please help me 


Viewing all articles
Browse latest Browse all 1350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>