网站开发及推广是怎么回事,企业模板,禹城网站建设价格,青岛网站设计选哪家1.自定义 表名属性 TableNameAttribute2.自定义 主键属性 PrimaryKeyAttribute3.自定义 列名属性 TableColumnAttribute 4.数据表person对应的实体类person.cs5.获得person.cs类型实体 对应的表名及字段名 下面的属性代码文件 都直接建立在App_Code下 以方便使用 1.自定义 表名…1.自定义 表名属性 TableNameAttribute2.自定义 主键属性 PrimaryKeyAttribute3.自定义 列名属性 TableColumnAttribute 4.数据表person对应的实体类person.cs5.获得person.cs类型实体 对应的表名及字段名 下面的属性代码文件 都直接建立在App_Code下 以方便使用 1.自定义 表名属性 TableNameAttribute------------------------------------ using System;/**//// summary/// TableNameAttribute 的摘要说明/// /summary[AttributeUsage( AttributeTargets.Class, AllowMultiplefalse, Inheritedtrue )]public sealed class TableNameAttribute : Attribute{ private string name; private string schema; //private CacheStrategy cacheStrategy GentleSettings.DefaultCacheStrategy; /**//// summary /// The constructor for the TableName attribute. /// /summary /// param namenameThe name of the database table used to store instances of this class./param public TableNameAttribute( string name ) { this.name name; } /**//// summary /// The constructor for the TableName attribute. /// /summary /// param namenameThe name of the database table used to store instances of this class./param /// param namestrategyThe cache stratgey to use for instances of this type. see /// crefCacheStrategy/ for a list of available options./param //public TableNameAttribute( string name, CacheStrategy strategy ) //{ // this.name name; // this.cacheStrategy strategy; //} /**//// summary /// The name of the database table used to store instances of this class. /// /summary public string Name { get { return name; } } /**//// summary /// The optional schema name with which to prefix the table name in queries. /// This value overrides the default schema definition (if present) in the /// configuration file. Note: this property is currently unused. /// /summary public string Schema { get { return schema; } set { schema value; } } /**////// summary ///// The cache behavior for objects of this type. see crefCacheStrategy/ ///// for a list of available options. ///// /summary //public CacheStrategy CacheStrategy //{ // get { return cacheStrategy; } // set { cacheStrategy value; } //}} 2.自定义 主键属性 PrimaryKeyAttribute ------------------------------------ using System;/**//// summary/// PrimaryKeyAttribute 的摘要说明/// /summary[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple true, Inherited true)]public sealed class PrimaryKeyAttribute : Attribute{ private bool autoGenerated false; /**//// summary /// Set this property to true for primary keys that are automatically assigned /// by the database on insert (identity columns in SQL server terminology). /// /summary public bool AutoGenerated { get { return autoGenerated; } set { autoGenerated value; } }} 3.自定义 列名属性 TableColumnAttribute -------------------------------------- using System;using System.Data;/**//// summary/// TableColumnAttribute 的摘要说明/// /summary[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field, AllowMultiplefalse, Inheritedtrue )]public class TableColumnAttribute : Attribute{ private string name; private bool notNull; private int size; private bool hasDbType false; // true when DbType property has been set private long dbType; private object nullValue null; private bool handleEnumAsString false; private bool isReadOnly false; private bool isUpdateAfterWrite false; /**//// summary /// Constructor for table columns that are named after their property counterpart /// and whose value cannot be null. /// /summary public TableColumnAttribute() : this(null, true) { } /**//// summary /// Constructor for table columns that are named after their property counterpart. /// /summary /// param namenotNullA boolean telling whether null values are allowed in the database/param public TableColumnAttribute(bool notNull) : this(null, notNull) { } /**//// summary /// Constructor for table columns whose value cannot be null. /// /summary /// param namenameThe name of the database column/param public TableColumnAttribute(string name) : this(name, true) { } /**//// summary /// Constructor for table columns. /// /summary /// param namenameThe name of the database column/param /// param namenotNullA boolean telling whether null values are allowed in the database/param public TableColumnAttribute(string name, bool notNull) { this.name name; this.notNull notNull; } /**//// summary /// The name of the database column for storing the property decorated with this attribute. /// /summary public string Name { get { return name; } } /**//// summary /// This property (defaults to true) can be used to specify whether NULL values are /// allowed in the database. This allows the framework to fail early if a constraint /// is violated. /// /summary public bool NotNull { get { return notNull; } set { notNull value; } } /**//// summary /// The database type of the field in the database. Beware that the DbType enumeration /// values are NOT the ones used by the individual providers. Gentle does NOT convert /// the DbType to a best match for the provider. It is therefore recommended that /// you use the DatabaseType below until a better type definition system is available. /// /summary [Obsolete(Please use DatabaseType instead.)] public DbType DbType { get { return (DbType)dbType; } set { hasDbType true; dbType (long)value; } } /**//// summary /// The database type of the field in the database. Convert the actual database type /// enumeration to a long by casting it in the declaration. /// /summary public long DatabaseType { get { return dbType; } set { hasDbType true; dbType value; } } /**//// summary /// The size or length of the field in the database. String properties will be clipped /// to fit. /// This feature will obsoleted once Gentle is capable of extracting type and size /// information directly from the database. If specified, the values must match /// those extracted from the database (when implemented). /// /summary public int Size { get { return size; } set { size value; } } /**//// summary /// This property indicates whether a DbType was specified. This construct is necessary /// because the DbType enum has no value for undefined. /// /summary public bool HasDbType { get { return hasDbType; } } /**//// summary /// Obsolete, use NullValue instead. /// /summary [Obsolete(Use NullValue instead.)] public object MagicValue { get { return nullValue; } set { nullValue value; } } /**//// summary /// This value of this property is used when a column is NotNull and the property value /// is null. If this is undefined the framework will throw an error for NotNull columns /// whose values are null. /// /summary public object NullValue { get { return nullValue; } set { nullValue value; } } NullValue Helper Properties for VB.NET Users#region NullValue Helper Properties for VB.NET Users /**//// summary /// This property allows type-safe setting of the NullValue for VB users. /// /summary public int NullValue_int { set { NullValue value; } } /**//// summary /// This property allows type-safe setting of the NullValue for VB users. /// /summary public NullOption NullValue_opt { set { NullValue value; } } #endregion /**//// summary /// This value indicates that the column should not be set on insert and update. It is /// primarily useful for columns that are set internally by the database. /// /summary public bool IsReadOnly { get { return isReadOnly; } set { isReadOnly value; } } /**//// summary /// This value indicates that the column must be read after each insert and update. It is /// primarily useful for columns that are set internally by the database. Note that using /// this feature (by setting this to true for any column) will significantly impact /// performance for the worse, as for every update/insert another select will be /// performed. Also, fields will be updated using reflection after select, which is also /// quite slow (depending on the number of columns). /// /summary public bool IsUpdateAfterWrite { get { return isUpdateAfterWrite; } set { isUpdateAfterWrite value; } } /**//// summary /// If member which has this attribute attached is enum then this property /// indicates wheter framework saves it as string or as integer. /// Default is false, ie enums are saved as integers /// /summary public bool HandleEnumAsString { get { return handleEnumAsString; } set { handleEnumAsString value; } }}public enum NullOption{ /**//// summary /// lt;typegt;.MinValue will be stored as NULL, and NULL will be read as lt;typegt;.MinValue. /// /summary MinValue, /**//// summary /// lt;typegt;.MaxValue will be stored as NULL, and NULL will be read as lt;typegt;.MaxValue. /// /summary MaxValue, /**//// summary /// 0 (or the equivalent for other numeric types) will be stored as NULL, and NULL will be read as 0. /// /summary This value can only be used with numeric types (such as decimal). Zero, /**//// summary /// Guid.Empty will be stored as NULL, and NULL will be read as Guid.Empty. This value can only be /// used with Guid fields. /// /summary EmptyGuid} 4.数据表person对应的实体类person.cs------------------------------------ using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;/**//// summary/// person 的摘要说明/// /summary[TableName(person)]public class person{ public person() { } //[TableColumn(personID, NotNull true), PrimaryKey] protected int _personID; //[TableColumn(personName, NotNull true)] protected string _personName String.Empty; [TableColumn(personID, NotNull true), PrimaryKey] public int PersonID { get { return _personID; } set { _personID value; } } [TableColumn(personName, NotNull true)] public string PersonName { get { return _personName; } set { _personName value; } }} 5.获得person.cs类型实体 对应的表名及字段名------------------------------------------ protected void Button1_Click(object sender, EventArgs e){ person aPerson new person(); aPerson.PersonID 22; aPerson.PersonName zhangsan; Type aType aPerson.GetType(); //类实体公开属性对应的列名属性 PropertyInfo[] aPropertyInfos aType.GetProperties(); string strPublicProperty ; for (int i 0; i aPropertyInfos.Length; i) { PropertyInfo aPropertyInfo (PropertyInfo)aPropertyInfos[i]; strPublicProperty i.ToString() : 实体属性名 aPropertyInfo.Name; strPublicProperty 对应值 aPropertyInfo.GetValue(aPerson, null).ToString() ; object[] attrs aPropertyInfo.GetCustomAttributes(typeof(TableColumnAttribute), true); for (int m 0; m attrs.Length; m) { strPublicProperty 对应列名 ((TableColumnAttribute)attrs[m]).Name; } } this.TextBox1.Text strPublicProperty; //FieldInfo[] aFieldInfos aType.GetFields(); //string strPublicField ; //for (int j 0; j aFieldInfos.Length; j) //{ // FieldInfo aFieldInfo (FieldInfo)aFieldInfos[j]; // strPublicField j.ToString() : aFieldInfo.Name ; //} //this.TextBox2.Text strPublicField; //类实体对应的表名属性 string strTablePKName ; object[] attrsAA aType.GetCustomAttributes(typeof(TableNameAttribute), true); for (int n 0; n attrsAA.Length; n) { strTablePKName ((TableNameAttribute)attrsAA[n]).Name; } //类实体对应的主键属性 MemberInfo[] aMemberInfos aType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); string strMemberInfo ; for (int k 0; k aMemberInfos.Length; k) { MemberInfo aM aMemberInfos[k]; object[] attrs aM.GetCustomAttributes(typeof(PrimaryKeyAttribute), true); for (int m 0; m attrs.Length; m) { strTablePKName 主键 aM.Name; } } this.TextBox2.Text strTablePKName; }