Пример #1
0
 public DeviceInfo(FullDeviceInfo fullDI)
 {
     this.DatabaseId = fullDI.DatabaseId;
     this.ModelName = fullDI.ModelName;
     this.UIIdion = fullDI.UIIdion;
     this.SpecificHWVersion = fullDI.SpecificHWVersion;
     this.OSName = fullDI.OSName;
     this.OSVersion = fullDI.OSVersion;
 }
        public int AddDevice(FullDeviceInfo deviceInfo)
        {
            int databaseId = -1;

            System.Data.Odbc.OdbcConnection conn = createSQLConnection();
            System.Data.Odbc.OdbcTransaction transaction = null;
            try
            {
                string selectQuery = string.Format("SELECT `DatabaseId` FROM `PerformanceAppDeviceInfo` WHERE `UniqueId` = '{0}'", deviceInfo.UniqueId);
                conn.Open();
                System.Data.Odbc.OdbcCommand selectCommand = new System.Data.Odbc.OdbcCommand(selectQuery, conn);
                try
                {
                    databaseId = (int)selectCommand.ExecuteScalar();
                }
                catch (Exception)
                {

                }

                if (databaseId == -1)
                {

                    string myInsertQuery =
                        string.Format(@"INSERT INTO `billholmes54`.`PerformanceAppDeviceInfo`
                                        (`DatabaseId`, `UniqueId`, `SystemName`, `ModelName`, `UIIdion`, `SpecificHWVersion`, `OSName`, `OSVersion`, `OwnerName`)
                                        VALUES (NULL, '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}');",
                        deviceInfo.UniqueId, deviceInfo.SystemName, deviceInfo.ModelName, deviceInfo.UIIdion, deviceInfo.SpecificHWVersion,
                        deviceInfo.OSName, deviceInfo.OSVersion, deviceInfo.OwnerName);

                    transaction = conn.BeginTransaction();

                    System.Data.Odbc.OdbcCommand insertCommand = new System.Data.Odbc.OdbcCommand(myInsertQuery, conn, transaction);
                    insertCommand.ExecuteNonQuery();

                    insertCommand.CommandText = "select last_insert_id();";
                    databaseId = Convert.ToInt32(insertCommand.ExecuteScalar());

                    transaction.Commit();
                }
                else
                {
                    string myInsertQuery =
                        string.Format(@"UPDATE `billholmes54`.`PerformanceAppDeviceInfo`
                                            SET `SystemName` = '{0}', `ModelName` = '{1}',
                                            `UIIdion` = '{2}', `SpecificHWVersion` = '{3}',
                                            `OSName` = '{4}', `OSVersion` = '{5}', `OwnerName` = '{6}'
                                        WHERE `PerformanceAppDeviceInfo`.`DatabaseId` = {7} LIMIT 1;",
                        deviceInfo.SystemName, deviceInfo.ModelName, deviceInfo.UIIdion, deviceInfo.SpecificHWVersion,
                        deviceInfo.OSName, deviceInfo.OSVersion, deviceInfo.OwnerName, databaseId);

                    System.Data.Odbc.OdbcCommand updateCommand = new System.Data.Odbc.OdbcCommand(myInsertQuery, conn);
                    updateCommand.ExecuteNonQuery();
                }

            }
            catch (Exception)
            {
                if (transaction != null)
                    transaction.Rollback();

                throw;
            }
            finally
            {
                conn.Close();
            }

            return databaseId;
        }