| using System; |
| using System.Collections; |
| using System.Data.Linq.Mapping; |
| using System.Reflection; |
| using System.Text.RegularExpressions; |
| using System.Web; |
| using System.Web.UI.WebControls; |
| |
| public partial class LinqMyAdmin : System.Web.UI.Page |
| { |
| // -------------------------------------------------------------------- |
| // CHANGE THE FOLLOWING LINE TO REFER TO YOUR LinqDataContext CLASS |
| // -------------------------------------------------------------------- |
| string LinqSQLClass = "Intranet.Data.NorthwindDataContext, App_Code"; |
| Type LinqContextType; |
| |
| /// <summary> |
| /// Executed upon Page Load. |
| /// </summary> |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| string TableName = Request.QueryString["table"]; |
| |
| // Display a list of available Tables |
| LinqContextType = System.Type.GetType(LinqSQLClass, true); |
| TablesRepeater.DataSource = GetAllTables(LinqContextType); |
| TablesRepeater.DataBind(); |
| |
| // If we have a "table" value in our querystring, then display the table. |
| if (TableName != null) |
| { |
| DisplayTable(TableName); |
| } |
| } |
| |
| /// <summary> |
| /// Gets a list of all Tables found in a LinqContext |
| /// </summary> |
| /// <param name="_Type">The LinqContext Type</param> |
| /// <returns>list of table names.</returns> |
| private ArrayList GetAllTables(Type _Type) |
| { |
| ArrayList _Tables = new ArrayList(); |
| PropertyInfo[] _AllProperties = _Type.GetProperties(); |
| foreach (var _PropInfo in _AllProperties) |
| { |
| // LINQ Tables are Generics and will be declared as type System.Data.Linq.Table<TEntity> |
| // I worked a long time to create a generic Table Type comparison. Nothing worked. |
| // So I ended up just converting the type to a string and searching for a pattern. :( |
| if (Regex.IsMatch(_PropInfo.PropertyType.ToString(), @"^System.Data.Linq.Table") == true) |
| { |
| _Tables.Add(_PropInfo.Name); |
| } |
| } |
| return _Tables; |
| } |
| |
| /// <summary> |
| /// |
| /// </summary> |
| /// <param name="TableName"></param> |
| private void DisplayTable(string TableName) |
| { |
| // We don't know the ContextTypeName or TableName in advance. |
| // Create a LinqDataSource control dynamically and add it to our |
| // PlaceHolder control. |
| LinqDataSource _linqSource = new LinqDataSource(); |
| _linqSource.ID = "LinqSource"; |
| _linqSource.ContextTypeName = LinqSQLClass; |
| _linqSource.TableName = TableName; |
| _linqSource.EnableInsert = true; |
| _linqSource.EnableDelete = true; |
| _linqSource.EnableUpdate = true; |
| _linqSource.AutoPage = true; |
| _linqSource.AutoSort = true; |
| _linqSource.EnableViewState = true; |
| LinqPlaceHolder.Controls.Add(_linqSource); |
| |
| // Fetch the Primary Keys associated with the current table. |
| string[] _ID = GetTableKeys(TableName); |
| |
| // Attach our LinqDataSource to our DataGrid. |
| TableHeader.InnerHtml = "Table -> " + TableName; |
| TableGrid.DataSourceID = "LinqSource"; |
| TableGrid.DataKeyNames = _ID; |
| } |
| |
| /// <summary> |
| /// Get all properties associated with our LinqContext Table. |
| /// </summary> |
| /// <param name="TableName">The TableName</param> |
| /// <returns>The properties associated with this table</returns> |
| private PropertyInfo[] GetTableProperties(string TableName) |
| { |
| // All we currently have is a table name; "Customers" (plural). |
| |
| // What we want to fetch are the properties associated with the |
| // model that is associated with our table. That model, example, |
| // would be called "Customer" (singular). |
| |
| // We find the connection between table & model by using |
| // reflection on the table. The table will have have a type |
| // of System.Data.Linq.Table<Customer>. The "Customer Type" is |
| // retrievable by looking at the Generic Arguments. |
| |
| // Clear as mud? Great, let's get started... |
| |
| Type _TableType = LinqContextType.GetProperty(TableName).PropertyType; |
| Type _ModelType; |
| PropertyInfo[] _ReturnProperties = null; |
| |
| // Make sure we have a Generic Type |
| if (_TableType.IsGenericType == true) |
| { |
| Type[] _GenericArgs = _TableType.GetGenericArguments(); |
| |
| // Ensure that we have some Generic Arguments |
| if (_GenericArgs.Length > 0) |
| { |
| // Our model type will be the first Arg. |
| _ModelType = _GenericArgs[0]; |
| _ReturnProperties = _ModelType.GetProperties(); |
| } |
| } |
| return _ReturnProperties; |
| } |
| |
| /// <summary> |
| /// Gets the primary keys associated with a LinqContext Table |
| /// </summary> |
| /// <param name="TableName">The Table Name</param> |
| /// <returns>A list of primary keys associated with the table</returns> |
| private string[] GetTableKeys(string TableName) |
| { |
| ArrayList _Keys = new ArrayList(); |
| bool _PrimaryKey = false; |
| |
| // Loop through each property found in the Table Model. |
| foreach (var _PropInfo in GetTableProperties(TableName)) |
| { |
| // The Primary Key flag is a Custom Attribute. Get all Custom Attributes |
| // associated with the current property. |
| Object[] _Objects = _PropInfo.GetCustomAttributes(true); |
| |
| // Reset our Primary Key flag. |
| _PrimaryKey = false; |
| |
| // Loop through each Custom Attribute. |
| foreach (Object _obj in _Objects) |
| { |
| // The attribute we're looking for is "System.Data.Linq.Mapping.ColumnAttribute" |
| if (_obj.GetType() == typeof(System.Data.Linq.Mapping.ColumnAttribute)) |
| { |
| ColumnAttribute _Attribute = (ColumnAttribute)_obj; |
| if (_Attribute.IsPrimaryKey == true) |
| { |
| _PrimaryKey = true; |
| } |
| } |
| } |
| |
| if (_PrimaryKey == true) |
| { |
| _Keys.Add(_PropInfo.Name); |
| } |
| } |
| |
| return _Keys.ToArray(typeof(string)) as string[]; |
| } |
| } |