我正在尝试从Excel插入数据库,并且我从Excel获得了所有值。当我插入我的数据库时,我得到了Factal Exception。如何从此异常中恢复?
MySqlCommand commd = new MySqlCommand();
commd.Connection = OpenConnection();
commd.CommandText = "INSERT INTO sqa_tracking(IID,STARTDATE,ENDDATE,WEEK,SUPPLIER,LINENO,ORDER_ID,BRCARID,PAGE_FIRST,PAGE_LAST,PAGE_COUNT,ARTICLE_NO,COUNT_LINENO,TOTAL_NR_OF_ITEMS,CAR_SUPPLIER,CONTENT_PROVIDER_NAME,MANIFESTATION,FACTOR,UNITS,DATE_OF_SUBMISSION,UNITS_KEY,COUNT_UNITS_KEY,TOTAL_NR_OF_UNITS,ERRORS,KPI_ERRORS,OBII_ELEMENT,CAR_FIELD_NAME,ERROR_TYPE,WRONG_CAPTURE_IN_FILE,CORRECT_CAPTURE_WOULD_BE,REPEATING_IN_CAR,SOURCE_FILE_TYPE,FULL_AUTOM_CONV,ERROR_IN_SOURCE_FILE,ROOT_CAUSE_OF_THE_ERROR,PREVENTIVE_MEASURES,DATE_OF_IMPLEMENTATION,DATE_OF_TRANSMISSION,VALIDATED) VALUES (@IID,@STARTDATE,@ENDDATE,@WEEK,@SUPPLIER,@LINENO,@ORDER_ID,@BRCARID,@PAGE_FIRST,@PAGE_LAST,@PAGE_COUNT,@ARTICLE_NO,@COUNT_LINENO,@TOTAL_NR_OF_ITEMS,@CAR_SUPPLIER,@CONTENT_PROVIDER_NAME,@MANIFESTATION,@FACTOR,@UNITS,@DATE_OF_SUBMISSION,@UNITS_KEY,@COUNT_UNITS_KEY,@TOTAL_NR_OF_UNITS,@ERRORS,@KPI_ERRORS,@OBII_ELEMENT,@CAR_FIELD_NAME,@ERROR_TYPE,@WRONG_CAPTURE_IN_FILE,@CORRECT_CAPTURE_WOULD_BE,@REPEATING_IN_CAR,@SOURCE_FILE_TYPE,@FULL_AUTOM_CONV,@ERROR_IN_SOURCE_FILE,@ROOT_CAUSE_OF_THE_ERROR,@PREVENTIVE_MEASURES,@DATE_OF_IMPLEMENTATION,@DATE_OF_SUBMISSION,@VALIDATED) ";
commd.Parameters.AddWithValue("@STARTDATE", StartDate);
commd.Parameters.AddWithValue("@ENDDATE", EndDate);
commd.Parameters.AddWithValue("@WEEK", Week);
commd.Parameters.AddWithValue("@SUPPLIER", Supplier);
commd.Parameters.AddWithValue("@LINENO", LineNo);
commd.Parameters.AddWithValue("@ORDER_ID", ORDERID);
commd.Parameters.AddWithValue("@BRCARID", "");
commd.Parameters.AddWithValue("@PAGE_FIRST", PageFirst);
commd.Parameters.AddWithValue("@PAGE_LAST", Pagelast);
commd.Parameters.AddWithValue("@PAGE_COUNT", Pagecount);
commd.Parameters.AddWithValue("@ARTICLE_NO", ArticleNo);
commd.Parameters.AddWithValue("@COUNT_LINENO", COUNTLineNo);
commd.Parameters.AddWithValue("@TOTAL_NR_OF_ITEMS", Totalnrofitems);
commd.Parameters.AddWithValue("@CAR_SUPPLIER", CARSupplier);
commd.Parameters.AddWithValue("@CONTENT_PROVIDER_NAME", ContentProvidename);
commd.Parameters.AddWithValue("@MANIFESTATION", Manifestation);
commd.Parameters.AddWithValue("@FACTOR", AU);
commd.Parameters.AddWithValue("@UNITS", Units);
commd.Parameters.AddWithValue("@DATE_OF_SUBMISSION", Dateoftransmission);
commd.Parameters.AddWithValue("@UNITS_KEY", unitskey);
commd.Parameters.AddWithValue("@COUNT_UNITS_KEY", COUNTunitskey);
commd.Parameters.AddWithValue("ERRORS", Errors);
commd.Parameters.AddWithValue("@KPI_ERRORS", KPIErrors);
commd.Parameters.AddWithValue("@OBII_ELEMENT", OBIIElement);
commd.Parameters.AddWithValue("@CAR_FIELD_NAME", CARFieldname);
commd.Parameters.AddWithValue("@ERROR_TYPE", Errortype);
commd.Parameters.AddWithValue("@IID", IID);
commd.Parameters.AddWithValue("@WRONG_CAPTURE_IN_FILE", Wrongcaptureinfile);
commd.Parameters.AddWithValue("@CORRECT_CAPTURE_WOULD_BE", Correctcapturewouldbe);
commd.Parameters.AddWithValue("@REPEATING_IN_CAR", RepeatinginCAR);
commd.Parameters.AddWithValue("@SOURCE_FILE_TYPE", Sourcefiletype);
commd.Parameters.AddWithValue("@FULL_AUTOM_CONV", FulAutomConv);
commd.Parameters.AddWithValue("@ERROR_IN_SOURCE_FILE", Errorinsourcefile);
commd.Parameters.AddWithValue("@ROOT_CAUSE_OF_THE_ERROR", RootCauseoftheError);
commd.Parameters.AddWithValue("@PREVENTIVE_MEASURES", PreventiveMeasures);
commd.Parameters.AddWithValue("@DATE_OF_IMPLEMENTATION", DateofImplementation);
commd.Parameters.AddWithValue("@VALIDATED", Validated);
commd.ExecuteNonQuery();`
我使用了内联查询,并从DataTable中获取了参数值。
答案 0 :(得分:1)
始终记住,仅在命令执行期间发生了致命错误,因为参数值名称不正确。
所以更正参数值以避免在命令执行期间出现胎儿错误 您没有为TOTAL_NR_OF_UNITS&提供参数值。 DATE_OF_TRANSMISSION。
另外,请按照插入查询中的顺序进行操作,并在添加参数时更容易进行故障排除。
commd.Parameters.AddWithValue("@TOTAL_NR_OF_UNITS",TotalNrOfUnits);
commd.Parameters.AddWithValue("@DATE_OF_TRANSMISSION", DateOfTransmission);
答案 1 :(得分:0)
要么这个。 。
SqlCommand cmd = new SqlCommand("insert into tbl_insert values (@id,@name,@Email,@City)");
cmd.Parameters.AddWithValue("@id", Convert.ToInt32(txtId.Text));
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@City", txtboxCity.Text);
......或者......
con.Open();
SqlCommand cmd = new SqlCommand(@"insert into tbl_insert values(@name,@email,@add)", con);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@email", txtemail.Text);
cmd.Parameters.AddWithValue("@add", txtadd.Text);
cmd.ExecuteNonQuery();
con.Close();
......应该可以正常工作。