📄 myormframework.cs
字号:
throw new ArgumentNullException("ObjectType");
}
this.CheckBindInfo( ObjectType , false );
TableBindInfo table = this.GetBindInfo( ObjectType );
string strSQL = "Select " + table.FieldNameList + " From " + FixTableName( table.TableName );
return ReadObjects( strSQL , ObjectType , 0 );
}
/// <summary>
/// 使用指定的SQL查询语句查询数据并读取一条数据库记录对象
/// </summary>
/// <param name="strSQL">SQL查询语句</param>
/// <param name="ObjectType">要读取的对象类型</param>
/// <returns>读取的对象</returns>
public object ReadObject( string strSQL , Type ObjectType )
{
// 检查参数
if( strSQL == null )
{
throw new ArgumentNullException("strSQL");
}
if( ObjectType == null )
{
throw new ArgumentNullException("ObjectType");
}
this.CheckBindInfo( ObjectType , false );
object[] objs = ReadObjects( strSQL , ObjectType , 1 );
if( objs != null && objs.Length == 1 )
return objs[ 0 ] ;
else
return null;
}
/// <summary>
/// 使用指定的SQL查询语句查询数据库并读取多条数据库记录对象
/// </summary>
/// <param name="strSQL">SQL查询语句</param>
/// <param name="ObjectType">要读取的对象类型</param>
/// <returns>读取的对象组成的数组</returns>
public object[] ReadObjects( string strSQL , Type ObjectType)
{
if( ObjectType == null )
{
throw new ArgumentNullException("ObjectType");
}
// 检查数据库映射信息
this.CheckBindInfo( ObjectType , false );
return ReadObjects( strSQL , ObjectType , 0 );
}
/// <summary>
/// 使用指定的SQL查询语句查询数据库并读取多条数据库记录对象
/// </summary>
/// <param name="strSQL">SQL查询语句</param>
/// <param name="ObjectType">要读取的对象类型</param>
/// <param name="MaxObjectCount">最多读取的对象个数</param>
/// <returns>读取的对象组成的数组</returns>
public object[] ReadObjects( string strSQL , Type ObjectType , int MaxObjectCount )
{
// 检查参数
if( strSQL == null )
{
throw new ArgumentNullException("strSQL");
}
if( ObjectType == null )
{
throw new ArgumentNullException("ObjectType");
}
// 检查数据库映射信息
this.CheckBindInfo( ObjectType , false );
// 检查数据库连接
this.CheckConnetion();
// 创建SQL命令对象
using( System.Data.IDbCommand cmd = myConnection.CreateCommand())
{
// 执行SQL查询,获得一个数据读取器
cmd.CommandText = strSQL ;
System.Data.IDataReader reader = cmd.ExecuteReader(
MaxObjectCount == 1 ?
System.Data.CommandBehavior.SingleRow :
System.Data.CommandBehavior.SingleResult );
System.Collections.ArrayList list = new System.Collections.ArrayList();
TableBindInfo table = this.GetBindInfo( ObjectType );
lock( table )
{
// 设置字段序号,提高性能
foreach( FieldBindInfo field in table.Fields )
{
field.FieldIndex = - 1 ;
}
for( int iCount = 0 ; iCount < reader.FieldCount ; iCount ++ )
{
string name = reader.GetName( iCount );
foreach( FieldBindInfo field in table.Fields )
{
if( EqualsFieldName( name , field.FieldName ))
{
field.FieldIndex = iCount ;
}
}
}
while( reader.Read())
{
// 根据对象类型创建对象实例
object obj = System.Activator.CreateInstance( ObjectType );
// 读取对象属性值
if( InnerReadValues( obj , table , reader ) > 0 )
{
list.Add( obj );
}
if( MaxObjectCount > 0 || list.Count == MaxObjectCount )
{
break;
}
}//while
}//lock
reader.Close();
// 返回读取的对象数组
return list.ToArray();
}//using
}
#endregion
#region 插入数据库记录的接口 *****************************************
/// <summary>
/// 将一个对象插入到数据库中
/// </summary>
/// <param name="obj">对象</param>
/// <returns>插入的数据库记录个数</returns>
public int InsertObject( object obj )
{
if( obj == null )
{
throw new ArgumentNullException("obj");
}
this.CheckBindInfo( obj.GetType() ,false );
return InsertObject( obj , null );
}
/// <summary>
/// 将一个对象插入到指定的数据表中
/// </summary>
/// <param name="obj">对象</param>
/// <param name="TableName">指定的数据表,若未指定则为默认数据表名</param>
/// <returns>插入的数据库记录的个数</returns>
public int InsertObject( object obj , string TableName )
{
if( obj == null )
{
throw new ArgumentNullException("obj");
}
this.CheckBindInfo( obj.GetType() , false );
return InsertObjects( new object[]{ obj } , TableName );
}
/// <summary>
/// 将若干个对象插入到数据库中
/// </summary>
/// <param name="Objects">对象列表</param>
/// <param name="TableName">制定的数据表,若未指定则使用默认的数据表名</param>
/// <returns>插入的数据库记录的个数</returns>
public int InsertObjects( System.Collections.IEnumerable Objects , string TableName )
{
if( Objects == null )
{
throw new ArgumentNullException("Objects");
}
this.CheckBindInfo( Objects , false );
System.Collections.ArrayList list = new System.Collections.ArrayList();
foreach( object obj in Objects )
{
list.Add( obj );
}
if( list.Count == 0 )
{
return 0 ;
}
this.CheckConnetion();
// 上一次执行的SQL语句
string strLastSQL = null ;
int InsertCount = 0 ;
using( System.Data.IDbCommand cmd = myConnection.CreateCommand())
{
foreach( object obj in list )
{
TableBindInfo table = this.GetBindInfo( obj.GetType());
string TableName2 = TableName ;
if( TableName2 == null || TableName.Trim().Length == 0 )
{
TableName2 = table.TableName ;
}
System.Collections.ArrayList values = new System.Collections.ArrayList();
// 拼凑SQL语句
System.Text.StringBuilder myStr = new System.Text.StringBuilder();
System.Text.StringBuilder myFields = new System.Text.StringBuilder();
foreach( FieldBindInfo field in table.Fields )
{
if( field.Property.CanRead == false )
{
throw new Exception("属性 " + field.Property.Name + " 是不可写的");
}
object v = field.Property.GetValue( obj , null );
if( v == null || DBNull.Value.Equals( v ))
{
continue ;
}
values.Add( field.ToDataBase( v ));
if( myStr.Length > 0 )
{
myStr.Append(" , ");
myFields.Append( " , " );
}
myStr.Append(" ? " );
myFields.Append( FixFieldName( field.FieldName ));
}//foreach
myStr.Insert( 0 , "Insert Into " + FixTableName( TableName2 )
+ " ( " + myFields.ToString() + " ) Values ( " );
myStr.Append( " ) " );
string strSQL = myStr.ToString();
if( strSQL != strLastSQL )
{
// 重新设置SQL命令对象
strLastSQL = strSQL ;
cmd.Parameters.Clear();
cmd.CommandText = strSQL ;
for( int iCount = 0 ; iCount < values.Count ; iCount ++ )
{
cmd.Parameters.Add( cmd.CreateParameter());
}
}
// 填充SQL命令参数值
for( int iCount = 0 ; iCount < values.Count ; iCount ++ )
{
( ( System.Data.IDbDataParameter ) cmd.Parameters[ iCount ]).Value = values[ iCount ] ;
}
// 执行SQL命令向数据表新增记录
InsertCount += cmd.ExecuteNonQuery();
}//foreach
}//using
return InsertCount ;
}
#endregion
/// <summary>
/// 修正数据表名
/// </summary>
/// <param name="TableName">旧的数据表名</param>
/// <returns>修正后的数据表名</returns>
/// <remarks>
/// 在某些情况下,程序指定的数据表名是不合法的,需要对其进行修正。比如对于Access则在表的两边加上方括号。
/// </remarks>
protected virtual string FixTableName( string TableName )
{
return TableName ;
}
/// <summary>
/// 修正数据字段名
/// </summary>
/// <param name="FieldName">旧的数据字段名</param>
/// <returns>修正后的字段名</returns>
/// <remarks>
/// 在某些情况下,程序指定的字段名不合法,需要对其进行修正,比如对于Access则在表的两边加上方括号。
/// </remarks>
protected virtual string FixFieldName( string FieldName )
{
return FieldName ;
}
/// <summary>
/// 销毁对象
/// </summary>
public void Dispose()
{
if( myConnection != null )
{
if( myConnection.State == System.Data.ConnectionState.Open )
{
myConnection.Close();
}
myConnection = null ;
}
}
#region 内部私有成员 **************************************************
/// <summary>
/// 检查数据库连接是否可用
/// </summary>
private void CheckConnetion()
{
if( myConnection == null || myConnection.State != System.Data.ConnectionState.Open )
{
throw new InvalidOperationException("数据库连接无效");
}
}
/// <summary>
/// 根据对象数值创建查询条件子SQL语句
/// </summary>
/// <param name="obj">对象</param>
/// <param name="values">SQL参数值列表</param>
/// <returns>创建的SQL语句字符串</returns>
private string BuildCondition( object obj , System.Collections.ArrayList values )
{
TableBindInfo table = this.GetBindInfo( obj.GetType() );
// 拼凑查询条件SQL语句
System.Text.StringBuilder mySQL = new System.Text.StringBuilder();
foreach( FieldBindInfo field in table.Fields )
{
if( field.Attribute.Key )
{
object v = field.Property.GetValue( obj , null );
if( v == null || DBNull.Value.Equals( v ))
{
throw new Exception("关键字段属性 " + field.Property.Name + " 未指定值" ) ;
}
if( mySQL.Length > 0 )
{
mySQL.Append(" And " );
}
mySQL.Append( FixFieldName( field.FieldName ));
mySQL.Append( " = ? " );
values.Add( field.ToDataBase( v ));
}
}//foreach
if( mySQL.Length == 0 )
{
throw new Exception("类型 " + obj.GetType().FullName + " 未能生成查询条件");
}
return mySQL.ToString();
}
/// <summary>
/// 从数据读取器中读取数据并填充到一个对象中
/// </summary>
/// <param name="ObjInstance">对象实例</param>
/// <param name="info">数据库绑定信息对象</param>
/// <param name="reader">数据读取器</param>
private int InnerReadValues( object ObjInstance , TableBindInfo info , System.Data.IDataReader reader )
{
// 检查参数
if( ObjInstance == null )
{
throw new ArgumentNullException("ObjectInstance");
}
if( info == null )
{
throw new ArgumentNullException("info");
}
if( reader == null )
{
throw new ArgumentNullException("reader");
}
int FieldCount = 0 ;
// 依次读取各个属性值
foreach( FieldBindInfo field in info.Fields )
{
// 绑定的属性是只读的
if( field.Property.CanWrite == false )
{
continue ;
}
// 没有找到绑定的字段
if( field.FieldIndex < 0 )
{
continue ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -