Skip to content
Browse files

No commit message

  • Loading branch information...
1 parent 0e0b1df commit 11e5feb1d9100d0c214f53442ab4a00355958930 @Ivony committed
View
9 Sources/Ivony.Data/EntityExtensions.cs
@@ -52,6 +52,15 @@ public static T Entity<T>( this DbUtility dbUtility, IDbExpression expression )
/// <returns>实体</returns>
public static T ToEntity<T>( this DataRow dataItem ) where T : new()
{
+ if ( dataItem == null )
+ {
+
+ if ( typeof( T ).IsValueType )
+ throw new ArgumentNullException( "dataItem" );
+
+ else
+ return default( T );//等同于return null
+ }
if ( dataItem.Table.Columns.Count == 1 )
{
View
428 Sources/Ivony.Data/SqlDbUtility.cs
@@ -1,206 +1,222 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-using System.Data.SqlClient;
-using System.Data;
-using System.Runtime.Serialization;
-using System.Security.Permissions;
-using System.Collections;
-
-namespace Ivony.Data
-{
- /// <summary>
- /// 用于操作 SQL Server 的数据库访问工具
- /// </summary>
- [Serializable]
- public class SqlDbUtility : DbUtility
- {
-
- string _connectionString;
-
- TransactionUtility _transaction;
-
-
- /// <summary>
- /// 创建 SqlDbUtility 对象
- /// </summary>
- /// <param name="connectionString">数据库连接字符串</param>
- public SqlDbUtility( string connectionString )
- {
- _connectionString = connectionString;
- }
-
- private SqlConnection CreateConnection()
- {
- if ( _transaction != null )
- throw new NotSupportedException( "在事务中执行时,禁止创建新连接" );
-
- //这里不能关闭原来的连接,因为它有可能正在被DataReader所使用。
- return InternalCreateConnection();
- }
-
- /// <summary>
- /// 创建数据库连接
- /// </summary>
- /// <returns>数据库连接</returns>
- protected virtual SqlConnection InternalCreateConnection()
- {
- return new SqlConnection( _connectionString );
- }
-
- /// <summary>
- /// 获取查询表达式解析器
- /// </summary>
- /// <returns>查询表达式解析器</returns>
- protected override IDbExpressionParser GetExpressionParser()
- {
- return new SqlServerExpressionParser( this );
- }
-
-
-
- internal SqlCommand CreateCommand()
- {
- if ( _transaction != null )
- return _transaction.CreateCommand();
-
- return CreateConnection().CreateCommand();
- }
-
-
- /// <summary>
- /// 创建一个事务
- /// </summary>
- /// <returns>事务管理器</returns>
- public override ITransactionUtility CreateTransaction()
- {
- return new TransactionUtility( this );
- }
-
- protected virtual SqlDbUtility InternalClone()
- {
- SqlDbUtility dbUtility = new SqlDbUtility( _connectionString );
- return dbUtility;
-
- }
-
- protected SqlDbUtility Clone()
- {
- return InternalClone();
- }
-
-
- private class TransactionUtility : ITransactionUtility<SqlDbUtility>
- {
- private SqlDbUtility _dbUtility;
- private SqlConnection _connection;
- private SqlTransaction _transaction;
- private bool _disposed = false;
-
- public TransactionUtility( SqlDbUtility origin )
- {
- _dbUtility = origin.Clone();
- _dbUtility._transaction = this;
- }
-
- /// <summary>
- /// 开始事务
- /// </summary>
- public void Begin()
- {
- if ( _disposed )
- throw new InvalidOperationException();
-
- if ( _transaction == null )
- {
- _connection = new SqlConnection( _dbUtility._connectionString );
- _connection.Open();
- _transaction = _connection.BeginTransaction();
- }
- }
-
- /// <summary>
- /// 提交事务
- /// </summary>
- public void Commit()
- {
- if ( _disposed )
- throw new InvalidOperationException();
-
- if ( _transaction == null )
- throw new InvalidOperationException();
-
- _transaction.Commit();
- _connection.Close();
- _disposed = true;
- }
-
- /// <summary>
- /// 回滚事务
- /// </summary>
- public void Rollback()
- {
- if ( _disposed )
- throw new InvalidOperationException();
-
- if ( _transaction == null )
- throw new InvalidOperationException();
-
- _transaction.Rollback();
- _connection.Close();
- _disposed = true;
- }
-
- /// <summary>
- /// 用于操作数据库的 DbUtility 对象
- /// </summary>
- public SqlDbUtility DbUtility
- {
- get { return _dbUtility; }
- }
-
- DbUtility ITransactionUtility.DbUtility
- {
- get { return DbUtility; }
- }
-
-
- /// <summary>
- /// 销毁事务对象
- /// </summary>
- public void Dispose()
- {
-
- if ( _connection != null )
- _connection.Dispose();
-
- if ( _transaction != null )
- _transaction.Dispose();
-
- _disposed = true;
- }
-
-
- /// <summary>
- /// 创建命令对象
- /// </summary>
- /// <returns></returns>
- internal SqlCommand CreateCommand()
- {
- if ( _disposed )
- throw new InvalidOperationException();
-
- if ( _transaction == null )
- Begin();
-
-
- SqlCommand command = _connection.CreateCommand();
- command.Transaction = _transaction;
-
- return command;
- }
-
- }
-
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Data.SqlClient;
+using System.Data;
+using System.Runtime.Serialization;
+using System.Security.Permissions;
+using System.Collections;
+using System.Configuration;
+
+namespace Ivony.Data
+{
+ /// <summary>
+ /// 用于操作 SQL Server 的数据库访问工具
+ /// </summary>
+ [Serializable]
+ public class SqlDbUtility : DbUtility
+ {
+
+ string _connectionString;
+
+ TransactionUtility _transaction;
+
+
+ /// <summary>
+ /// 创建 SqlDbUtility 对象
+ /// </summary>
+ /// <param name="connectionString">数据库连接字符串</param>
+ public SqlDbUtility( string connectionString )
+ {
+ _connectionString = connectionString;
+ }
+
+ private SqlConnection CreateConnection()
+ {
+ if ( _transaction != null )
+ throw new NotSupportedException( "在事务中执行时,禁止创建新连接" );
+
+ //这里不能关闭原来的连接,因为它有可能正在被DataReader所使用。
+ return InternalCreateConnection();
+ }
+
+ /// <summary>
+ /// 创建数据库连接
+ /// </summary>
+ /// <returns>数据库连接</returns>
+ protected virtual SqlConnection InternalCreateConnection()
+ {
+ return new SqlConnection( _connectionString );
+ }
+
+ /// <summary>
+ /// 获取查询表达式解析器
+ /// </summary>
+ /// <returns>查询表达式解析器</returns>
+ protected override IDbExpressionParser GetExpressionParser()
+ {
+ return new SqlServerExpressionParser( this );
+ }
+
+
+
+ internal SqlCommand CreateCommand()
+ {
+ if ( _transaction != null )
+ return _transaction.CreateCommand();
+
+ return CreateConnection().CreateCommand();
+ }
+
+
+ /// <summary>
+ /// 创建一个事务
+ /// </summary>
+ /// <returns>事务管理器</returns>
+ public override ITransactionUtility CreateTransaction()
+ {
+ return new TransactionUtility( this );
+ }
+
+ protected virtual SqlDbUtility InternalClone()
+ {
+ SqlDbUtility dbUtility = new SqlDbUtility( _connectionString );
+ return dbUtility;
+
+ }
+
+ protected SqlDbUtility Clone()
+ {
+ return InternalClone();
+ }
+
+
+ private class TransactionUtility : ITransactionUtility<SqlDbUtility>
+ {
+ private SqlDbUtility _dbUtility;
+ private SqlConnection _connection;
+ private SqlTransaction _transaction;
+ private bool _disposed = false;
+
+ public TransactionUtility( SqlDbUtility origin )
+ {
+ _dbUtility = origin.Clone();
+ _dbUtility._transaction = this;
+ }
+
+ /// <summary>
+ /// 开始事务
+ /// </summary>
+ public void Begin()
+ {
+ if ( _disposed )
+ throw new InvalidOperationException();
+
+ if ( _transaction == null )
+ {
+ _connection = new SqlConnection( _dbUtility._connectionString );
+ _connection.Open();
+ _transaction = _connection.BeginTransaction();
+ }
+ }
+
+ /// <summary>
+ /// 提交事务
+ /// </summary>
+ public void Commit()
+ {
+ if ( _disposed )
+ throw new InvalidOperationException();
+
+ if ( _transaction == null )
+ throw new InvalidOperationException();
+
+ _transaction.Commit();
+ _connection.Close();
+ _disposed = true;
+ }
+
+ /// <summary>
+ /// 回滚事务
+ /// </summary>
+ public void Rollback()
+ {
+ if ( _disposed )
+ throw new InvalidOperationException();
+
+ if ( _transaction == null )
+ throw new InvalidOperationException();
+
+ _transaction.Rollback();
+ _connection.Close();
+ _disposed = true;
+ }
+
+ /// <summary>
+ /// 用于操作数据库的 DbUtility 对象
+ /// </summary>
+ public SqlDbUtility DbUtility
+ {
+ get { return _dbUtility; }
+ }
+
+ DbUtility ITransactionUtility.DbUtility
+ {
+ get { return DbUtility; }
+ }
+
+
+ /// <summary>
+ /// 销毁事务对象
+ /// </summary>
+ public void Dispose()
+ {
+
+ if ( _connection != null )
+ _connection.Dispose();
+
+ if ( _transaction != null )
+ _transaction.Dispose();
+
+ _disposed = true;
+ }
+
+
+ /// <summary>
+ /// 创建命令对象
+ /// </summary>
+ /// <returns></returns>
+ internal SqlCommand CreateCommand()
+ {
+ if ( _disposed )
+ throw new InvalidOperationException();
+
+ if ( _transaction == null )
+ Begin();
+
+
+ SqlCommand command = _connection.CreateCommand();
+ command.Transaction = _transaction;
+
+ return command;
+ }
+
+ }
+
+
+ /// <summary>
+ /// 创建数据库访问工具
+ /// </summary>
+ /// <param name="name">连接字符串名称</param>
+ /// <returns>数据库访问工具</returns>
+ public static SqlDbUtility Create( string name )
+ {
+ var setting = ConfigurationManager.ConnectionStrings[name];
+ if ( setting == null )
+ throw new InvalidOperationException();
+
+ return new SqlDbUtility( setting.ConnectionString );
+ }
+
+ }
+}
View
3 Sources/Ivony.Data/SqlServerExpressionParser.cs
@@ -30,9 +30,6 @@ internal SqlServerExpressionParser( SqlDbUtility dbUtility )
/// <returns>解析后创建的命令对象</returns>
public IDbCommand Parse( IDbExpression expression )
{
-
- var command = _dbUtility.CreateCommand();
-
var template = expression as TemplateExpression;
if ( template != null )
return ParseTemplate( template );

0 comments on commit 11e5feb

Please sign in to comment.
Something went wrong with that request. Please try again.