public string SqlQueryInsertIntoAlarms(Alarm_UDT Alarm, int pointer) { return("INSERT INTO Alarms (" + "UniqueID" + "," + "RobotName" + "," + "AlarmID" + "," + "AlarmNo" + "," + "AlarmSeverity" + "," + "AlarmTimestamp" + "," + "Ticks" + "," + "Pointer" + "," + ")" + " Values (" + "\'" + Alarm.Alarm_ID.Value + "-" + Alarm.Alarm_Timestamp.Value.ToString("s", new CultureInfo("en-US")) + "\'" + "," + Alarm.Robot_Name.Value + "," + Alarm.Alarm_ID.Value + "," + Alarm.Alarm_No.Value + "," + Alarm.Alarm_Severity.Value + "," + // date format in SQL DB 09/13/2019 9:17:56 AM - US // date format from PLC 13/09/2019 9:17:56 AM - British "\'" + Alarm.Alarm_Timestamp.Value.ToString("G", new CultureInfo("en-US")) + "\'" + "," + Alarm.Alarm_Timestamp.Value.Ticks + "," + pointer + "," + ")"); }
public void writeAlarmToDB(Alarm_UDT Alarms, SqlCommand command, int Pointer, SqlConnection cnn) { // Write everything from PLC buffer to DB except for anything with AlarmID= 0 if (Alarms.Alarm_ID.Value != 0) { Console.WriteLine("Writing Alarm with ID:{0} to the database.", Alarms.Alarm_ID.Value); //command is use to perform read and write operations in the database //Command object, which is used to execute the SQL statement against the database command = new SqlCommand(SqlQueryInsertIntoAlarms(Alarms, Pointer), cnn); //DataAdapter object is used to perform specific SQL operations such as insert, delete and update commands SqlDataAdapter adapter = new SqlDataAdapter(); //we now associate the insert SQL command to our adapter adapter.InsertCommand = new SqlCommand(SqlQueryInsertIntoAlarms(Alarms, Pointer), cnn); //then issue the ExecuteNonQuery method which is used to execute the Insert statement against our database adapter.InsertCommand.ExecuteNonQuery(); //Dispose of all temp objects created adapter.Dispose(); command.Dispose(); } }