如何在DataColumn中将两个00添加到“人员主机密钥”之后才会映射到数据库表?
我曾尝试对数据进行隐蔽,但它就像我无法在数据表中格式化它。还尝试为每个循环添加额外的00到字段,但无法使其工作。
public partial class PageHours : System.Web.UI.Page
{
protected void Upload(object sender, EventArgs e)
{
//Upload and save the file
string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(excelPath);
string conString = string.Empty;
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
switch (extension)
{
case ".xls": //Excel 97-03
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07 or higher
conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
break;
}
conString = string.Format(conString, excelPath);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
excel_con.Open();
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
//[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
dtExcelData.Columns.AddRange
(new DataColumn[28]
{
new DataColumn("Name of Staff", typeof(string)),
new DataColumn("StaffID", typeof(string)),
new DataColumn("Host Key of Staff", typeof(string)),
new DataColumn("Name of Department",typeof(string)),
new DataColumn("Description of Preferred Zone of Staff",typeof(string)),
new DataColumn("User Text 2 of Staff",typeof(string)),
new DataColumn("Name of Programmes of Study of Module",typeof(string)),
new DataColumn("Description of Programmes of Study of Module",typeof(string)),
new DataColumn("Host Key of Locations",typeof(string)),
new DataColumn("Host Key of Module",typeof(string)),
new DataColumn("Description of Module",typeof(string)),
new DataColumn("Name",typeof(string)),
new DataColumn("Scheduled Start Date",typeof(string)),
new DataColumn("Teaching week pattern of Scheduled Activities as end date",typeof(string)),
new DataColumn("Section ID",typeof(string)),
new DataColumn("Description of Type",typeof(string)),
new DataColumn("Scheduled Start as day name",typeof(string)),
new DataColumn("Scheduled Start as start time",typeof(string)),
new DataColumn("Scheduled Finish as end time",typeof(string)),
new DataColumn("Duration as duration",typeof(string)),
new DataColumn("Part Time of Staff",typeof(string)),
new DataColumn("Taught Periods as duration",typeof(string)),
new DataColumn("Taught Periods Distinct as duration",typeof(string)),
new DataColumn("Teaching week pattern as week ranges",typeof(string)),
new DataColumn("Teaching week pattern as number of weeks",typeof(string)),
new DataColumn("Exported Weeks as week label range",typeof(string)),
new DataColumn("Scheduled by",typeof(string)),
new DataColumn("Date of Scheduling",typeof(string))
}
);
using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
{
oda.Fill(dtExcelData);
}
excel_con.Close();
string consString = ConfigurationManager.ConnectionStrings["PayrollPlusConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "dbo.Data";
//[OPTIONAL]: Map the Excel columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("Name of Staff", "Staff");
sqlBulkCopy.ColumnMappings.Add("StaffID", "StaffID");
sqlBulkCopy.ColumnMappings.Add("Host Key of Staff", "HostKey");
sqlBulkCopy.ColumnMappings.Add("Name of Department", "Department");
sqlBulkCopy.ColumnMappings.Add("Description of Preferred Zone of Staff", "Campus");
sqlBulkCopy.ColumnMappings.Add("User Text 2 of Staff", "Grade");
sqlBulkCopy.ColumnMappings.Add("Name of Programmes of Study of Module", "AOS");
sqlBulkCopy.ColumnMappings.Add("Description of Programmes of Study of Module", "AOSDescription");
sqlBulkCopy.ColumnMappings.Add("Host Key of Locations", "Room");
sqlBulkCopy.ColumnMappings.Add("Host Key of Module", "ModuleAOS");
sqlBulkCopy.ColumnMappings.Add("Description of Module", "ModuleDescription");
sqlBulkCopy.ColumnMappings.Add("Name", "Activity");
sqlBulkCopy.ColumnMappings.Add("Scheduled Start Date", "StartDate");
sqlBulkCopy.ColumnMappings.Add("Teaching week pattern of Scheduled Activities as end date", "EndDate");
sqlBulkCopy.ColumnMappings.Add("Section ID", "SectionID");
sqlBulkCopy.ColumnMappings.Add("Description of Type", "Type");
sqlBulkCopy.ColumnMappings.Add("Scheduled Start as day name", "Day");
sqlBulkCopy.ColumnMappings.Add("Scheduled Start as start time", "StartTime");
sqlBulkCopy.ColumnMappings.Add("Scheduled Finish as end time", "EndTime");
sqlBulkCopy.ColumnMappings.Add("Duration as duration", "Duration");
sqlBulkCopy.ColumnMappings.Add("Part Time of Staff", "PartTime");
sqlBulkCopy.ColumnMappings.Add("Taught Periods as duration", "TaughtPeriodsAsDuration");
sqlBulkCopy.ColumnMappings.Add("Taught Periods Distinct as duration", "TaughtDistinct");
sqlBulkCopy.ColumnMappings.Add("Teaching week pattern as week ranges", "WeekRanges");
sqlBulkCopy.ColumnMappings.Add("Teaching week pattern as number of weeks", "TotalNoOfWeeks");
sqlBulkCopy.ColumnMappings.Add("Exported Weeks as week label range", "WeeksThisRun");
sqlBulkCopy.ColumnMappings.Add("Scheduled by", "ScheduledBy");
sqlBulkCopy.ColumnMappings.Add("Date of Scheduling", "DateOfScheduling");
con.Open();
sqlBulkCopy.WriteToServer(dtExcelData);
con.Close();
lblStatus.Text = "Upload Successful";
}
}
}
}
protected void DeleteData(object sender, EventArgs e)
{
SqlConnection con2 = new SqlConnection();
con2.ConnectionString = ConfigurationManager.ConnectionStrings["PayrollPlusConnectionString"].ConnectionString;
con2.Open();
SqlCommand com = new SqlCommand();
com.Connection = con2;
com.CommandText = "DELETE FROM [Data]";
SqlDataReader data = com.ExecuteReader();
con2.Close();
lblStatus.Text = "Data Deleted Successful, please upload for new month";
}
}
}
答案 0 :(得分:1)
首先 - 最好将Host Key of Staff
的{{1}}列的类型从dtExcelData
更改为int
,否则您将无法将“前置”零添加到它持有的数据。
第二 - 在你的循环中,你只从DataRow获取数据,但实际上并没有改变它。这个循环应该像
string
答案 1 :(得分:1)
我会创建一个如下所示的扩展方法
document.getElementById("showHide").onclick = function () {
if ($("#showHide").text() == " show") {
document.getElementById("showHide").innerHTML = "<span class=\"glyphicon glyphicon-eye-close\"></span> hide";
document.getElementById("showHide").blur();
} else {
document.getElementById("showHide").innerHTML = "<span class=\"glyphicon glyphicon-eye-open\"></span> show";
document.getElementById("showHide").blur();
}
}
然后(我已将它用于数据时间并粘贴我的代码)
public static void Modify<T>(this DataColumn dataColumn, Func<object, T> toModify)
{
foreach(DataRow dataRow in dataColumn.Table.Rows)
{
dataRow[dataColumn] = toModify(dataRow[dataColumn]);
}
}