我制作了一个简单的Windows窗体,可以上传文件,然后显示在可以查看的网格视图中,然后将文件插入到数据库表中。这是我以前没有尝试过的新事物。我最初尝试使用gridview插入文件,它很快显示,但我只能插入分页,然后它只会插入显示的前10个。我试图关闭插入的分页,但我只是在插入时得到这个: HTTP错误404.13 - 未找到 请求过滤模块配置为拒绝超过请求内容长度的请求
excel文件大约有60,000条记录。我尝试切换到数据表来进行插入,一切都变慢了。我将此添加到网络配置但仍然没有运气。我尝试了一个只有13条记录的文件,当它加载到表中时,它似乎仍然很慢,最初加载并填充更多的页面而不是数据。理想情况下,gridview中的2个页面将继续显示超过数据的10个以上空白页面。所以我想知道是否涉及减速。以下是我的代码。使用gridview的原始方式已被注释掉。这里有什么我做错了还是可以改进的?任何帮助,将不胜感激。谢谢!
protected void LoadFile(object sender, EventArgs e)
{
if(RehireFormUpload.HasFile)
{
string FileName = Path.GetFileName(RehireFormUpload.PostedFile.FileName);
string Extension = Path.GetExtension(RehireFormUpload.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + FileName);
RehireFormUpload.SaveAs(FilePath);
Import_To_Grid(FilePath, Extension, "Yes");
mainPanel.Update();
}
}
private void Import_To_Grid(string FilePath, string Extension, string isHDR)
{
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03Constring"].ConnectionString;
break;
case ".xlsx": //Excel 07 and on
conStr = ConfigurationManager.ConnectionStrings["Excel07Constring"].ConnectionString;
break;
}
conStr = String.Format(conStr, FilePath, isHDR);
OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
//Get the name of the first sheet
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
//Read the data from first sheet
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();
//dt.Rows.Remove(dt.Rows[0]);
//dt.Rows.Remove(dt.Rows[0]);
DataTable dtCloned = dt.Clone();
dtCloned.Columns[0].DataType = typeof(String);
foreach (DataRow row in dt.Rows)
{
dtCloned.ImportRow(row);
}
//Bind Data to gridview
GridView1.Caption = Path.GetFileName(FilePath);
GridView1.DataSource = dtCloned;
ViewState["DataTable"] = dtCloned;
GridView1.DataBind();
}
protected void pageIndexChanging(object sender, GridViewPageEventArgs e)
{
string folderPath = ConfigurationManager.AppSettings["FolderPath"];
string fileName = GridView1.Caption;
string Extension = Path.GetExtension(fileName);
string FilePath = Server.MapPath(folderPath + fileName);
Import_To_Grid(FilePath, Extension, "Yes");
GridView1.PageIndex = e.NewPageIndex;
DataTable dt = (DataTable)ViewState["DataTable"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Submit(object sender, EventArgs e)
{
string employeeID;
string firstName;
string lastName;
string termCode;
string dateofBirth;
string eligibleForRehire;
string ssn;
string terminationDate;
SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InternalDB"].ConnectionString);
//BindingSource bs = (BindingSource)GridView1.DataSource;
//DataTable bdt = (DataTable)bs.DataSource;
DataTable thisData = (DataTable)ViewState["DataTable"];
foreach (DataRow row in thisData.Rows)
{
employeeID = row[0].ToString(); //Convert.ToInt32(row.Cells[0].ToString());
if (employeeID != " " && employeeID != "" && !SearchRecord(employeeID))
{
firstName = row[1].ToString();
lastName = row[2].ToString();
termCode = row[3].ToString();
dateofBirth = row[4].ToString().Substring(0, 8);
eligibleForRehire = row[5].ToString();
ssn = row[6].ToString();
terminationDate = row[7].ToString();
InsertFile(employeeID, firstName, lastName, termCode, dateofBirth, eligibleForRehire, ssn, terminationDate);
}
else if (employeeID != " " && employeeID != "" && SearchRecord(employeeID))
{
firstName = row[1].ToString();
lastName = row[2].ToString();
termCode = row[3].ToString();
dateofBirth = row[4].ToString().Substring(0, 8);
eligibleForRehire = row[5].ToString();
ssn = row[6].ToString();
terminationDate = row[7].ToString();
UpdateRecord(employeeID, firstName, lastName, termCode, dateofBirth, eligibleForRehire, terminationDate);
}
else
{
//Create Message Upload done
//MessageBox.Show("End of Records");
}
}
//foreach (GridViewRow row in GridView1.Rows)
//{
// employeeID = row.Cells[0].Text.ToString(); //Convert.ToInt32(row.Cells[0].ToString());
// if (employeeID != " " && !SearchRecord(employeeID))
// {
// firstName = row.Cells[1].Text.ToString();
// lastName = row.Cells[2].Text.ToString();
// termCode = row.Cells[3].Text.ToString();
// dateofBirth = row.Cells[4].Text.Substring(0, 8).ToString();
// eligibleForRehire = row.Cells[5].Text.ToString();
// ssn = row.Cells[6].Text.ToString();
// terminationDate = row.Cells[7].Text.ToString();
// InsertFile(employeeID, firstName, lastName, termCode, dateofBirth, eligibleForRehire, ssn, terminationDate);
// }
// else if (employeeID != " " && SearchRecord(employeeID))
// {
// firstName = row.Cells[1].Text.ToString();
// lastName = row.Cells[2].Text.ToString();
// termCode = row.Cells[3].Text.ToString();
// dateofBirth = row.Cells[4].Text.Substring(0, 8).ToString();
// eligibleForRehire = row.Cells[5].Text.ToString();
// ssn = row.Cells[6].Text.ToString();
// terminationDate = row.Cells[7].Text.ToString();
// UpdateRecord(employeeID, firstName, lastName, termCode, dateofBirth, eligibleForRehire, terminationDate);
// }
// else
// {
// //Empty Column
// }
//}
MessageBox.Show("Record Insert Complete");
}
private void InsertFile(string employeeID, string firstName, string lastName, string termCode, string dateofBirth, string eligibleForRehire, string ssn, string terminationDate)
{
SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InternalDB"].ConnectionString);
DateTime InsertedDate = DateTime.Now;
conn1.Open();
String query = "INSERT INTO [EMPLOYEE_REHIRES](EmployeeID, FirstName, LastName, TermCode, DateofBirth, EligibleforRehire, SSN, TerminationDate, INSERTED_DATE) VALUES ('" + employeeID + "','" + firstName + "', '" + lastName + "', '" + termCode + "', '" + dateofBirth + "', '" + eligibleForRehire + "' , '" + ssn + "' , '" + terminationDate + "', '" + InsertedDate + "');";
SqlCommand newCommand = new SqlCommand(query, conn1);
newCommand.ExecuteNonQuery();
conn1.Close();
}
private Boolean SearchRecord(string employeeID)
{
SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InternalDB"].ConnectionString);
Boolean employeeInserted = true;
conn1.Open();
SqlCommand newCommand2 = new SqlCommand();
String isEmployeeInserted = "Select * FROM [EMPLOYEE_REHIRES] with (nolock) WHERE EmployeeID=" + employeeID;
newCommand2 = new SqlCommand(isEmployeeInserted, conn1);
SqlDataReader reader1 = newCommand2.ExecuteReader();
string[] employees = new string[1];
while (reader1.Read())
{
employees[0] = reader1[0].ToString();
}
conn1.Close();
if (employees[0] == null)
{
employeeInserted = false;
}
return employeeInserted;
}
private void UpdateRecord(string employeeID, string firstName, string lastName, string termCode, string dateofBirth, string eligibleForRehire, string terminationDate)
{
SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["InternalDB"].ConnectionString);
conn1.Open();
DateTime lastUpdatedDate = DateTime.Now;
String query = "UPDATE [EMPLOYEE_REHIRES] SET FirstName='" + firstName + "', LastName='" + lastName + "', TermCode='" + termCode + "', DateofBirth='" + dateofBirth + "', EligibleforRehire='" + eligibleForRehire + "', TerminationDate='" + terminationDate + "', LAST_UPDATED_DATE='" + lastUpdatedDate + "' WHERE EmployeeID='" + employeeID + "';";
SqlCommand newCommand = new SqlCommand(query, conn1);
newCommand.ExecuteNonQuery();
conn1.Close();
}
答案 0 :(得分:0)
所以为此我最终在我的页面索引更改中添加了Session并修复了我的速度问题,对于我的插入,我将所有内容都更改为SQLBulkCopy并将其加载到临时表中。然后,我创建了一个存储过程,用于从舞台插入或更新生产表。
DataTable dt = (DataTable)Session["employeeData"];