简体   繁体   English

在Oracle 11g中未使用C#导入空值

[英]Null value is not imported with C# in Oracle 11g

I am currently creating an application to import data from DataGridView into Oracle 11g. 我目前正在创建一个应用程序,以将数据从DataGridView导入Oracle 11g。 My problem is if for example the column REPORTING_OUTBOUND_DATE is empty then I get error message System.FormatException: "The string was not recognized as valid DateTime". 我的问题是,例如,如果列REPORTING_OUTBOUND_DATE为空,则会收到错误消息System.FormatException:“该字符串未被识别为有效的DateTime”。 How can I fix these errors and AM IMPORTANT at all ? 我该如何解决这些错误,并且根本不重要? Is there any way to program everything better? 有什么办法可以更好地编程一切吗? Thank you 谢谢

private void Btn_SAVE_IN_DATABASE_VEUPEN_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView2.Rows.Count - 1; i++) //Schleife für INSERT Befehl
        {

            OracleConnection con = new OracleConnection("Data Source=(***********************");
            con.Open();

            string sql = "INSERT INTO AFTERSALES.INPUT_BOARDLEVEL_REPAIR_VEUPEN (BLR_REPORT_DATE, MONTH_OF_REPAIR_END, PCB_COUNTER, MANUFACTURER, REPORTING_OUTBOUND_DATE,  EMPTY,  QTY)"
                       + "VALUES (:BLR_REPORT_DATE, :MONTH_OF_REPAIR_END, :PCB_COUNTER, :MANUFACTURER, :REPORTING_OUTBOUND_DATE, :EMPTY, :QTY)";

            OracleCommand cmd = new OracleCommand(sql, con);
            //cmd.CommandText = sql;
            cmd.Parameters.Add(":BLR_REPORT_DATE", Convert.ToDateTime(dataGridView2.Rows[i].Cells[0].Value).ToString("dd.MM.yyyy"));
            cmd.Parameters.Add(":MONTH_OF_REPAIR_END", dataGridView2.Rows[i].Cells[1].Value);
            cmd.Parameters.Add(":PCB_COUNTER", dataGridView2.Rows[i].Cells[2].Value);
            cmd.Parameters.Add(":MANUFACTURER", dataGridView2.Rows[i].Cells[3].Value);
            cmd.Parameters.Add(":REPORTING_OUTBOUND_DATE", Convert.ToDateTime(dataGridView2.Rows[i].Cells[4].Value).ToString("dd.MM.yyyy"));
            cmd.Parameters.Add(":EMPTY", dataGridView2.Rows[i].Cells[5].Value);
            cmd.Parameters.Add(":QTY", dataGridView2.Rows[i].Cells[6].Value);       
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

Try checking for null before converting. 尝试在转换前检查null。 Something like this: 像这样:

dataGridView2.Rows[i].Cells[0].Value == null? null : Convert.ToDateTime(dataGridView2.Rows[i].Cells[0].Value).ToString("dd.MM.yyyy")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM