private void btnImport_Click(object sender, EventArgs e) { List <PROTravellerModel> proTras = new List <PROTravellerModel>(); PROTravellerModel item = new PROTravellerModel(); DataTable dt = Util.ExcelToTable(txtPROtravllerLoc.Text); dataGridViewImport.DataSource = dt; foreach (DataRow dr in dt.Rows) { string startDateValue = dr["Start date"].ToString(); string endDateValue = dr["End date"].ToString(); string startDate = DateTime.FromOADate(Convert.ToInt32(startDateValue)).ToString("yyyyMMdd"); string endDate = DateTime.FromOADate(Convert.ToInt32(endDateValue)).ToString("yyyyMMdd"); dr["Start date"] = startDate; dr["End date"] = endDate; } try { foreach (DataRow dr in dt.Rows) { item.Line = dr["Line"].ToString(); item.Year = dr["Year"].ToString(); item.OrderNumber = dr["Order Number"].ToString(); item.TotalQuantity = dr["Total quantity"].ToString(); item.Model = dr["Model"].ToString(); item.StartDate = dr["Start date"].ToString(); item.EndDate = dr["End date"].ToString(); item.Description = dr["description"].ToString(); item.CreateTime = DateTime.Now.ToString().Substring(0, 9); item.Status = "Not started"; da.InsertResult(item); } MessageBox.Show("导入成功"); } catch (Exception e1) { MessageBox.Show(e1.Message); } finally { txtPROtravllerLoc.Text = String.Empty; dataGridViewImport.Visible = false; } }
public void InsertResult(PROTravellerModel result) { string insert1 = "insert into PRO(Line,Year,OrderNumber,TotalQuantity,Model,StartDate,EndDate,Description,CreateTime,Status) values ("; string insert2 = " \'" + result.Line + "\' ,"; string insert3 = " \'" + result.Year + "\',\'" + result.OrderNumber + "\',\'" + result.TotalQuantity + "\',\'" + result.Model + "\',\'" + result.StartDate + " \',"; string insert4 = " \'" + result.EndDate + "\',\'" + result.Description + "\',\'" + result.CreateTime + "\',\'" + result.Status + "\' )"; string insert = insert1 + insert2 + insert3 + insert4; try { SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["PROTravellerConnectionString"].ConnectionString); myConnection.Open(); SqlCommand MyCommand = new SqlCommand(insert, myConnection); MyCommand.ExecuteNonQuery(); myConnection.Close(); } catch (Exception ex) { throw ex; } }