我尝试使用带C#的ASP.NET从Excel导入200,000条记录到SQL Server 2005。但是当我尝试浏览并导入文件时,只插入了少量记录(仅限100条记录)。我无法得到任何解决方案。
这是我的示例代码
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
{
}
}
}
答案 0 :(得分:1)
我希望你不建议你只打开一次连接,而不是每次插入一行
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;
}
}
答案 1 :(得分:0)
将for循环代码替换为此循环代码以跟踪导致异常的所有行。并在循环后添加断点以检查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;
}
答案 2 :(得分:0)
似乎Excel OLE DB驱动程序只查找电子表格的前100行。这可能是由于各种原因造成的。例如,这是one user who found that the Excel print area affected the driver。我建议您将Excel数据剪切并粘贴到一个包含最少格式的新电子表格中,并查看情况是否有所改善。