Beispiel #1
0
        public void Update()
        {
            var commandString = "UPDATE Calibration SET Date=@clDate, " +
                                "Comment=@desc, CalibrationType=@type WHERE (IDCalibration = @iDCalibration)";
            SqlCommand     updateCommand = new SqlCommand(commandString, connection);
            SqlTransaction sqlTran       = null;

            updateCommand.Parameters.AddRange(new SqlParameter[] {
                new SqlParameter("clDate", CalibrationDate),
                new SqlParameter("desc", Description),
                new SqlParameter("type", CalibrationType.ToString()),
                new SqlParameter("iDCalibration", CalibrationID)
            });
            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error opening connection!", MessageBoxButton.OK);
            }

            if (connection.State != ConnectionState.Open)
            {
                return;
            }

            try
            {
                sqlTran = connection.BeginTransaction("ChangeOfCalibration");
                updateCommand.Transaction = sqlTran;
                updateCommand.ExecuteNonQuery();
                sqlTran.Commit();

                DataTable dataTable = new DataTable("TempCalibrationData");
                dataTable.Columns.AddRange(new DataColumn[]
                {
                    new DataColumn("IDCalibration", typeof(int)),
                    new DataColumn("IDCalibrationData", typeof(int)),
                    new DataColumn("Diapason", typeof(int)),
                    new DataColumn("Concentration", typeof(decimal)),
                    new DataColumn("Value", typeof(decimal))
                });
                LinearCalibrationData[0].ToList().ForEach(p =>
                                                          dataTable.Rows.Add(CalibrationID, 0, 1, p.Concentration, p.Value));
                LinearCalibrationData[1].ToList().ForEach(p =>
                                                          dataTable.Rows.Add(CalibrationID, 0, 2, p.Concentration, p.Value));

                SqlCommand command = new SqlCommand("UpdateCalibrationData", connection);
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter parameter = new SqlParameter("@tmp", SqlDbType.Structured);
                parameter.Value = dataTable;
                command.Parameters.Add(parameter);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                try { sqlTran.Rollback("ChangeOfCalibration"); } catch { }
                throw new Exception(ex.Message, ex);//Bubble up the original exception
            }
            finally
            {
                connection.Close(); //Close connection even if the querry failed
            }
        }
 void RpcSetCalibrationType(CalibrationType calibrationType)
 {
     this.calibrationType = calibrationType;
     this.infoDisplayInstance.SetText(calibrationType.ToString());
 }
Beispiel #3
0
        public void Insert()
        {
            var commandString = "INSERT INTO Calibration ([Date], [Comment], [CalibrationType])" +
                                "OUTPUT INSERTED.IDCalibration VALUES (@date, @comment, @type)";
            SqlCommand     insertCommand = new SqlCommand(commandString, connection);
            SqlTransaction sqlTran       = null;

            insertCommand.Parameters.AddRange(new SqlParameter[]
            {
                new SqlParameter("date", CalibrationDate),
                new SqlParameter("comment", Description),
                new SqlParameter("type", CalibrationType.ToString())
            });

            try
            {
                connection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error opening connection!", MessageBoxButton.OK);
            }

            if (connection.State != ConnectionState.Open)
            {
                return;
            }

            try
            {
                sqlTran = connection.BeginTransaction();
                insertCommand.Transaction = sqlTran;
                int resID = (int)insertCommand.ExecuteScalar();
                sqlTran.Commit();

                DataTable dataTable = new DataTable("TempCalibrationData");
                dataTable.Columns.AddRange(new DataColumn[]
                {
                    new DataColumn("IDCalibration", typeof(int)),
                    new DataColumn("IDCalibrationData", typeof(int)),
                    new DataColumn("Diapason", typeof(int)),
                    new DataColumn("Concentration", typeof(decimal)),
                    new DataColumn("Value", typeof(decimal))
                });
                LinearCalibrationData[0].ToList().ForEach(p =>
                                                          dataTable.Rows.Add(resID, 0, 1, p.Concentration, p.Value));
                LinearCalibrationData[1].ToList().ForEach(p =>
                                                          dataTable.Rows.Add(resID, 0, 2, p.Concentration, p.Value));

                SqlCommand command = new SqlCommand("UpdateCalibrationData", connection);
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter parameter = new SqlParameter("@tmp", SqlDbType.Structured);
                parameter.Value = dataTable;
                command.Parameters.Add(parameter);
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);//Bubble up the original exception
            }
            finally
            {
                connection.Close(); //Close connection even if the querry failed
            }
        }