private static AvailabilityGroup GetGroupFromDAtaRow(DataRow row, DataSet set)
        {
            AvailabilityGroup db = new AvailabilityGroup(
                new Guid(row[(int)AvailabilityGroupColumns.GroupId].ToString()),
                row[(int)AvailabilityGroupColumns.Name].ToString(),
                row[(int)AvailabilityGroupColumns.ResourceId].ToString(),
                row[(int)AvailabilityGroupColumns.ResourceGroupId].ToString(),
                int.Parse(row[(int)AvailabilityGroupColumns.FailureConditionLevel].ToString()),
                int.Parse(row[(int)AvailabilityGroupColumns.HealthchecktTmeout].ToString()),
                byte.Parse(row[(int)AvailabilityGroupColumns.AutomatedBbackupPreference].ToString()),
                row[(int)AvailabilityGroupColumns.AutomatedBackupPreferenceDesc].ToString(),
                int.Parse(row[(int)AvailabilityGroupColumns.SnapshotId].ToString()),
                int.Parse(row[(int)AvailabilityGroupColumns.ServergroupId].ToString()));

            if (set.Tables.Count > 1)
            {
                DataRow[] replicRows = set.Tables[1].Select(string.Format("groupid = '{0}'",
                                                                          row[(int)AvailabilityGroupColumns.GroupId].ToString()));

                foreach (DataRow dataRow in replicRows)
                {
                    AvailabilityGroupReplica rep = new AvailabilityGroupReplica();

                    rep.AvailabilityMode =
                        byte.Parse(dataRow[(int)AvailabilityGroupReplicaColumns.AvailabilityMode].ToString());
                    rep.CreateDate           = DateTime.Parse(dataRow[(int)AvailabilityGroupReplicaColumns.CreateDate].ToString());
                    rep.AvailabilityModeDesc = dataRow[(int)AvailabilityGroupReplicaColumns.AvailabilityModeDesc].ToString();
                    rep.EndpointUrl          = dataRow[(int)AvailabilityGroupReplicaColumns.EndpointUrl].ToString();
                    rep.FailoverMode         = byte.Parse(dataRow[(int)AvailabilityGroupReplicaColumns.FailoverMode].ToString());
                    rep.FailoverModeDesc     = dataRow[(int)AvailabilityGroupReplicaColumns.FailoverModeDesc].ToString();
                    rep.GroupId           = new Guid(dataRow[(int)AvailabilityGroupReplicaColumns.GroupId].ToString());
                    rep.ModifyDate        = DateTime.Parse(dataRow[(int)AvailabilityGroupReplicaColumns.ModifyDate].ToString());
                    rep.Ownersid          = dataRow[(int)AvailabilityGroupReplicaColumns.Ownersid].ToString();
                    rep.ReplicaServerName = dataRow[(int)AvailabilityGroupReplicaColumns.ReplicaServerName].ToString();
                    rep.Replicaid         = new Guid(dataRow[(int)AvailabilityGroupReplicaColumns.Replicaid].ToString());
                    rep.ServerreplicaId   = int.Parse(dataRow[(int)AvailabilityGroupReplicaColumns.ServerreplicaId].ToString());
                    rep.SnapshotId        = int.Parse(dataRow[(int)AvailabilityGroupReplicaColumns.SnapshotId].ToString());
                    rep.OwnerName         = dataRow[(int)AvailabilityGroupReplicaColumns.OwnerName].ToString();

                    db.Replicas.Add(rep);
                }
            }
            return(db);
        }
        public static List <AvailabilityGroup> GetAvailabilityGroups(int snapshotid)
        {
            List <AvailabilityGroup> groupList = new List <AvailabilityGroup>();

            try
            {
                // Retrieve list of snapshot dbs.
                using (SqlConnection connection = new SqlConnection(Program.gController.Repository.ConnectionString)
                       )
                {
                    // Connect to repository.
                    connection.Open();

                    // Create parameter.
                    SqlParameter paramSnapshotid = new SqlParameter(ParamSnapshotid, snapshotid);

                    // Query for
                    //using (SqlDataReader rdr = Sql.SqlHelper.ExecuteReader(connection, null, CommandType.Text,
                    //    QueryAvailabilityGroups, new SqlParameter[] { paramSnapshotid }))
                    SqlCommand cmd = new SqlCommand(QueryAvailabilityGroups + @" ; " + QueryGetReplicas, connection);
                    cmd.Parameters.Add(paramSnapshotid);
                    using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd))
                        using (DataSet set = new DataSet())
                        {
                            sqlAdapter.Fill(set);
                            foreach (DataRow row in set.Tables[0].Rows)
                            {
                                AvailabilityGroup db = GetGroupFromDAtaRow(row, set);

                                groupList.Add(db);
                            }
                        }
                }
            }
            catch (Exception ex)
            {
                logX.loggerX.Error("Error Retrieving Availability Group  list for Snapshot", ex);
                groupList.Clear();
            }



            return(groupList);
        }
        public static AvailabilityGroup GetAvailabilityGroup(int snapshotid, Guid groupid)
        {
            AvailabilityGroup group = null;

            try
            {
                // Retrieve list server objects.
                using (SqlConnection connection = new SqlConnection(Program.gController.Repository.ConnectionString))
                {
                    // Connect to repository.
                    connection.Open();

                    // Create parameter.
                    SqlParameter paramSnapshotid = new SqlParameter(ParamSnapshotid, snapshotid);
                    SqlParameter paramGroupId    = new SqlParameter(ParamGroupId, groupid.ToString());

                    // Query for
                    //using (SqlDataReader rdr = Sql.SqlHelper.ExecuteReader(connection, null, CommandType.Text,
                    //    QueryAvailabilityGroups, new SqlParameter[] { paramSnapshotid }))
                    SqlCommand cmd = new SqlCommand(QueryAvailabilityGroup + @" ; " + QueryGetReplica, connection);
                    cmd.Parameters.Add(paramSnapshotid);
                    cmd.Parameters.Add(paramGroupId);
                    using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd))
                        using (DataSet set = new DataSet())
                        {
                            sqlAdapter.Fill(set);
                            foreach (DataRow row in set.Tables[0].Rows)
                            {
                                group = GetGroupFromDAtaRow(row, set);
                            }
                        }
                }
            }
            catch (Exception ex)
            {
                logX.loggerX.Error("Error Retrieving Availability Group   for Snapshot", ex);
            }

            return(group);
        }