【发布时间】:2020-08-26 11:34:26
【问题描述】:
我的项目有问题,
我想从 excel 中读取我的数据并插入到数据库中, 但在此之前,
但我在某些情况下,我在我的 excel 中输入了错误的日期时间格式,所以它在我的程序中出错, 所以我想我想在将数据处理到数据库之前验证我的数据,
这是我的代码,
if (FileUpload1.HasFile)
{
if (ddlTest.SelectedValue.Length != 0)
{
string filename = "Schedule" + DateTime.Now.ToString("dddd_dd_MMMM_yyyy") + ".xls";
FileUpload1.SaveAs(temp_file + filename);
string tempfile = temp_file + filename;
DataTable table = new DataTable();
FileInfo existingFile = new FileInfo(tempfile); //+ FileUpload1.FileName);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
table.Columns.Add("Emp_NIK", typeof(string));
table.Columns.Add("ID_Shift", typeof(string));
table.Columns.Add("Schedule_Date", typeof(DateTime));
for (int i = 1; i < rowCount; i++)
{
try
{
DateTime d;
bool validate = DateTime.TryParseExact(
worksheet.Cells[i + 1, 3].Value.ToString(),
"M/dd/yyyy h:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out d);
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
if (validate == true)
{
string labelmonth = ddlTest.SelectedValue.ToString();
string shift = worksheet.Cells[i + 1, 2].Value.ToString();
string employee_id = worksheet.Cells[i + 1, 1].Value.ToString();
DateTime schedule_date = DateTime.Parse(worksheet.Cells[i + 1, 3].Value.ToString());
string user = Session["LogedUserID"].ToString();
bool validatemonth = ddlTest.SelectedValue.ToString() == DateTime.Parse(worksheet.Cells[i + 1, 3].Value.ToString()).ToString("MM");
if (validatemonth == false)
{
cell1.Text = worksheet.Cells[i + 1, 1].Value.ToString() + " " + worksheet.Cells[i + 1, 2].Value.ToString() + " " + worksheet.Cells[i + 1, 3].Value.ToString() + " Baris ke " + i + " Bulan Tidak Sesuai";
row.Cells.Add(cell1);
mytable.Rows.Add(row);
}
else
{
schedule.InsertData(employee_id, shift, schedule_date, user);
}
}
else
{
cell1.Text = worksheet.Cells[i + 1, 1].Value.ToString() + " " + worksheet.Cells[i + 1, 2].Value.ToString() + " " + worksheet.Cells[i + 1, 3].Value.ToString() + " Baris ke " + i + "Format Tanggal salah";
row.Cells.Add(cell1);
mytable.Rows.Add(row);
}
}
catch (Exception err)
{
Response.Write("<script>window.alert('File Excel Kosong')</script>");
}
}
}
}
else
{
Response.Write("<script>window.alert('Pilih Bulan Terlebih dahulu')</script>");
}
}
else
{
Response.Write("<script>window.alert('File Belum Di Upload')</script>");
}
例如,我的 excel 中有 5 行数据,
1行无效,4行有效,
我希望当 i 行无效时,另外 4 行无法插入数据库,
但当前情况是当一行无效时,另一行仍插入数据库
我该如何解决这个问题?
【问题讨论】:
-
你可以使用
Transaction来做:Submit()如果成功,Rollback()如果出现任何问题。 -
你能告诉我,如何在技术上实现它?
-
我需要
schedule.InsertData()的代码。您可以选择这样做: 1. 初始化一个 SqlTransaction 实例,例如:transactionfirst;然后2,将transaction传递给schedule.InsertData(); 3、如果所有行都有效,则做transaction.Commit();,否则,调用`transaction.Rollback()'; -
public void InsertData(string sEmpNIK, string sIDShift, DateTime dScheduleDate,string sUser) { conn = objDBConnector.GetConn(); cmd = objDBConnector.GetCommand(); SqlDataReader rdr = null; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "插入数据"; cmd.Parameters.AddWithValue("@Emp_NIK", sEmpNIK); cmd.Parameters.AddWithValue("@ID_Shift", sIDShift); cmd.Parameters.AddWithValue("@Schedule_Date", dScheduleDate); cmd.Parameters.AddWithValue("@User", sUser); }
-
这是您需要的代码,stackoverflow 的注释字符有限,如果我的文字看起来不整洁,我很抱歉