如何使用 c# OracleClient 将当前日期时间插入到 oracle 数据库中的日期列中?

How to insert the current datetime into a date column in a oracle database using c# OracleClient?

我尝试使用 c# 将用户单击按钮时的当前时间插入到 oracle 数据库中,using System.Data.OracleClient;我尝试将日期时间转换为字符串并将其转换回日期数据输入 Oracle。

这是数据助手class:


public int AddAMachineRecord(int machineNr, double currentITime, double cycleTime, int nrOfLinesPerCm, double heightOfLamallae, 
           int machineType, int isActive, int isDisplayed, string reasonOfChange, string changedBy, string changeTime)
       {
           string queryString = "INSERT INTO RO_MACHINE_MANAGEMENT_HISTORY(MACHINE_NR, CURRENT_I_TIME, CYCLE_TIME, NR_OF_LINES_PER_CM, HEIGHT_OF_LAMALLAE, MACHINE_TYPE," +
               " ISACTIVE, ISDISPLAYED, REASON_OF_CHANGE, CHANGED_BY, PR_CHANGE_TIME) VALUES (:machine_nr, :currentTime, :cycleTime, :nrOfLinesPerCm, :heightOfLamallae, :machineType," +
               ":isActive, :isDisplayed, :reason, :changedBy, TO_DATE(:changeTime, 'MM/DD/YYYY HH24:MI:SS.FF'))";
           using (OracleConnection connection = new OracleConnection(connectionString))
           using (OracleCommand command = new OracleCommand(queryString, connection))
           {
               try
               {
                   
                   OracleDataAdapter da = new OracleDataAdapter();
                   connection.Open();
                   command.Parameters.AddWithValue("machine_nr", machineNr);
                   command.Parameters.AddWithValue("currentTime", currentITime);
                   command.Parameters.AddWithValue("cycleTime", cycleTime);
                   command.Parameters.AddWithValue("nrOfLinesPerCm", nrOfLinesPerCm);
                   command.Parameters.AddWithValue("heightOfLamallae", heightOfLamallae);
                   command.Parameters.AddWithValue("machineType", machineType);
                   command.Parameters.AddWithValue("isActive", isActive);
                   command.Parameters.AddWithValue("isDisplayed", isDisplayed);
                   command.Parameters.AddWithValue("reason", reasonOfChange);
                   command.Parameters.AddWithValue("changedBy", changedBy);
                   command.Parameters.AddWithValue("changeTime", changeTime);

                   da.InsertCommand = command;
                   int nrOfRecordsChanged = command.ExecuteNonQuery();
                   return nrOfRecordsChanged;
               }
               catch
               {
                   return -1; //which means the try-block was not executed succesfully, so  . . .
               }
               finally
               {
                   connection.Close();
               }
           }
       }

和windows形式的代码:


private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int machineNr = Convert.ToInt32(tbCharacteristicsMachineNr.Text);
                double currentITime = Convert.ToDouble(tbCurrentITime.Text);
                double cycleTime = Convert.ToDouble(tbCycleTime.Text);
                int nrOfLinesPerCm = Convert.ToInt32(tbNrOfLinesPerCm.Text);
                double heightOfLamallae = Convert.ToDouble(tbHeightOfLamallae.Text);
                int machineType = -1;
                if (rbLTB.Checked)
                {
                    machineType = 2;
                }
                else if (rbSTB.Checked)
                {
                    machineType = 1;
                }
                int isActive = 0;
                int isDisplayed = 1;
                string reason = tbReason.Text;
                string empName = Name;
                string now = DateTime.Now.ToString("dd/M/yyyy hh:mm:ss tt");
               if( dh.AddAMachineRecord(machineNr, currentITime, cycleTime, nrOfLinesPerCm, heightOfLamallae, machineType,
                    isActive, isDisplayed, reason, empName, now) != -1)
                {
                    MessageBox.Show("Recorded successfully!");
                }
                else
                {
                    MessageBox.Show("Record was not successful!");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }

string now = DateTime.Now.ToString("dd/M/yyyy hh:mm:ss tt");显然不能用TO_DATE(:changeTime, 'MM/DD/YYYY HH24:MI:SS.FF')记录。我该如何解决这个问题?非常感谢您!

如果你想插入当前date/time,直接在数据库中计算应该更简单和更有效:

INSERT INTO RO_MACHINE_MANAGEMENT_HISTORY (
    MACHINE_NR, 
    CURRENT_I_TIME, 
    CYCLE_TIME, 
    NR_OF_LINES_PER_CM, 
    HEIGHT_OF_LAMALLAE, 
    MACHINE_TYPE,
    ISACTIVE, 
    ISDISPLAYED, 
    REASON_OF_CHANGE, 
    CHANGED_BY, 
    PR_CHANGE_TIME
) 
VALUES (
    :machine_nr, 
    :currentTime, 
    :cycleTime, 
    :nrOfLinesPerCm, 
    :heightOfLamallae, 
    :machineType, 
    :isActive, 
    :isDisplayed, 
    :reason, 
    :changedBy, 
    sysdate      --> current date/time
)

如果 PR_CHANGE_TIMEtimestamp 数据类型而不是 date,请使用 systimestamp 而不是 sysdate