private ObjectDirectory GetObjectDirectory() { ObjectDirectory toReturn = null; if (this.cbType.SelectedIndex == 0 && !string.IsNullOrEmpty(this.tbName.Text)) { toReturn = new ObjectDirectory(); toReturn.MapObjects = new Collections.MapObjects(); toReturn.ObjectDirectories = new Collections.ObjectDirectories(); toReturn.Name = this.tbName.Text; } return toReturn; }
/// <summary> /// Gets the object directory with the given ID. /// </summary> /// <param name="id">The ID of the object directory to get.</param> /// <returns>The object directory with the given ID</returns> public static ObjectDirectory GetObjectDirectory(long id) { ObjectDirectory toReturn = null; DataTable dt = null; SqlCommand cmd = new SqlCommand(); string query = "SELECT * FROM [ObjectDirectory] WHERE ID = @ID"; cmd.CommandType = CommandType.Text; cmd.CommandText = query; cmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = id; dt = DatabaseHelper.ExecuteQuery(GetConnectionString(), cmd); if (dt != null && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { toReturn = new ObjectDirectory(); toReturn.ID = DatabaseHelper.GetValidValueFromObject(dr["ID"], (long)0); toReturn.Name = DatabaseHelper.GetValidValueFromObject(dr["Name"], string.Empty); } } return toReturn; }
/// <summary> /// Inserts or Updates an object directory, based on the given object directory's ID /// </summary> /// <param name="o">The object directory to insert/update</param> /// <param name="parent">The parent of the object directory</param> private static void InsertUpdateObjectDirectory(ObjectDirectory o, long parent) { string query = string.Empty; SqlCommand cmd = null; long id = 0; if (o != null) { cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; //Update if (o.ID > 0) { query = "UPDATE [ObjectDirectory] SET Name = @Name, Parent = @Parent WHERE ID = @ID; SELECT SCOPE_IDENTITY();"; cmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = o.ID; } //Insert else { query = "INSERT INTO [ObjectDirectory] (Name, Parent) VALUES (@Name, @Parent); SELECT SCOPE_IDENTITY();"; } cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = o.Name; cmd.Parameters.Add("@Parent", SqlDbType.BigInt).Value = parent; cmd.CommandText = query; id = DatabaseHelper.ExecuteCommandContainingGetIdentity(GetConnectionString(), cmd); if (id > 0) { InsertUpdateObjectDirectories(o.ObjectDirectories, id); InsertUpdateMapObjects(o.MapObjects, id); } } }
/// <summary> /// Gets all object directores with the given parent /// </summary> /// <param name="parent">The parent id of the object directories to get</param> /// <returns>All object directories with the given parent</returns> public static ObjectDirectories GetObjectDirectories(long parent) { ObjectDirectories toReturn = new ObjectDirectories(); SqlCommand cmd = new SqlCommand(); ObjectDirectory temp = null; DataTable dt = null; string query = "SELECT * FROM [ObjectDirectory] WHERE Parent = @Parent"; cmd.CommandType = CommandType.Text; cmd.CommandText = query; cmd.Parameters.Add(new SqlParameter("@Parent", SqlDbType.BigInt)).Value = parent; dt = DatabaseHelper.ExecuteQuery(GetConnectionString(), cmd); if (dt != null && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { temp = new ObjectDirectory(); temp.ID = DatabaseHelper.GetValidValueFromObject(dr["ID"], (long)0); temp.Name = DatabaseHelper.GetValidValueFromObject(dr["Name"], string.Empty); toReturn.Add(temp); } } return toReturn; }