数据访问层(开发过程的实现代码)

  数据访问层
在应用程序的设计中,数据库的访问是非常重要的,我们通常需要将对数据库的访问集中起来,以保证良好的封装性和可维护性。在.Net中,数据库的访问,对于微软自家的SqlServer和其他数据库(支持OleDb),采用不同的访问方法,这些类分别分布于System.Data.SqlClient和System.Data.OleDb名称空间中。微软后来又推出了专门用于访问Oracle数据库的类库。我们希望在编写应用系统的时候,不因这么多类的不同而受到影响,能够尽量做到数据库无关,当后台数据库发生变更的时候,不需要更改客户端的代码。
  有的时候,为了性能和其他原因,我们也希望提供对数据库访问的缓存,特别是数据库连接的缓存。虽然微软给我们内置了数据库缓存,但是,自己控制缓存,无疑可以提供更大的灵活性和效率。 

None.gif
None.gif
None.gif
public   class  BaseDataAccess : IDisposable
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 数据适配器
InBlock.gif  private SqlDataAdapter dsAdapter;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// 数据更新事务句柄
InBlock.gif  SqlTransaction dsTransaction;
InBlock.gif  
private SqlCommand selectCommand;
InBlock.gif  
private SqlCommand updateCommand;
InBlock.gif  
private SqlCommand insertCommand;
InBlock.gif  
private SqlCommand deleteCommand;
InBlock.gif  
// 数据库连接
InBlock.gif
  private SqlConnection dbConnection;
InBlock.gif  
private DBConfigration dbConfigration = new DBConfigration();
InBlock.gif  
public BaseDataAccess()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 创建数据适配器对象“OldDbDataAdapter”
InBlock.gif   dsAdapter = new SqlDataAdapter();
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 获取数据操纵连接
InBlock.gif   dbConnection = new SqlConnection(DBConfigration.BitSoftConnectionString);   
ExpandedSubBlockEnd.gif  }
  
InBlock.gif  
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   Dispose(
true);
InBlock.gif   GC.SuppressFinalize(
true); 
ExpandedSubBlockEnd.gif  }

InBlock.gif                
protected virtual void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if (! disposing)
InBlock.gif    
return
InBlock.gif   
if (dsAdapter != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if (dsAdapter.SelectCommand != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
if( dsAdapter.SelectCommand.Connection != null)
InBlock.gif     dsAdapter.SelectCommand.Connection.Dispose();
InBlock.gif     dsAdapter.SelectCommand.Dispose();
ExpandedSubBlockEnd.gif    }

InBlock.gif    dsAdapter.Dispose();
InBlock.gif    dsAdapter 
= null;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 检查传入的单个数据表单(DataTable)字段域,自动获得该数据表单的“Update”命令。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="DataTable">单个数据表单</param>
ExpandedSubBlockEnd.gif  
/// <returns>SqlCommand</returns> 

InBlock.gif  private SqlCommand GetUpdateByIdCommand(DataTable oneTable)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
string sqlFirst = " UPDATE ";
InBlock.gif   
string sqlSecond = " WHERE ";
InBlock.gif   sqlFirst 
= sqlFirst + oneTable.TableName + " SET ";   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 创建 SqlCommand 对象
InBlock.gif   if ( updateCommand == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif   updateCommand 
= new SqlCommand(sqlFirst, dbConnection);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// Update 语句的参数表集合
InBlock.gif   SqlParameterCollection updateParams = updateCommand.Parameters;
InBlock.gif   updateCommand.Parameters.Clear();
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 遍历 DataTable 字段,生成 SQL UPDATE 语句
InBlock.gif   bool isFirstColumn = true;
InBlock.gif   
bool isFirstKeyColumn = true;
InBlock.gif   
foreach (BaseDataColumn dc in oneTable.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 忽略无用字段
InBlock.gif    if ( dc.IsValidColumn() )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 获取关键字字段
InBlock.gif     if ( dc.IsKeyColumn() ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
if ( isFirstKeyColumn )
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif      sqlSecond 
= sqlSecond + dc.ColumnName + " = @"+dc.ColumnName;
InBlock.gif      isFirstKeyColumn 
= false;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
InBlock.gif      sqlSecond 
= sqlSecond + " AND " + dc.ColumnName + " = @"+dc.ColumnName;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
if ( isFirstColumn )
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif      sqlFirst 
= sqlFirst + dc.ColumnName + " = @"+dc.ColumnName;
InBlock.gif      isFirstColumn 
= false;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
InBlock.gif      sqlFirst 
= sqlFirst + "" + dc.ColumnName + " = @"+dc.ColumnName;
InBlock.gif      updateParams.Add(
new sqlParameter("@"+dc.ColumnName,dc.GetColumnSqlDbType()));
InBlock.gif      updateParams[
"@"+dc.ColumnName].SourceColumn = dc.ColumnName;
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
foreach (BaseDataColumn dc in oneTable.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 忽略无用字段
InBlock.gif    if ( dc.IsValidColumn() )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 获取关键字字段
InBlock.gif     if ( dc.IsKeyColumn() ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif     updateParams.Add(
new SqlParameter("@"+dc.ColumnName, dc.GetColumnSqlDbType()));
InBlock.gif     updateParams[
"@"+dc.ColumnName].SourceColumn = dc.ColumnName;
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 设置 Command 类型和完整的 UPDATE 语句 
InBlock.gif   updateCommand.CommandType = CommandType.Text;
InBlock.gif   updateCommand.CommandText 
= sqlFirst + sqlSecond;
InBlock.gif   
return updateCommand;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 检查传入的单个数据表单(DataTable)字段域,自动获得该数据表单的“Insert”命令。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="oneTable">单个数据表单</param>
ExpandedSubBlockEnd.gif  
/// <returns>SqlCommand</returns>

InBlock.gif  private SqlCommand GetInsertCommand(DataTable oneTable)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
string sqlFirst = " INSERT INTO ";
InBlock.gif   
string sqlSecond = " VALUES ( ";
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 拼接表名
InBlock.gif   sqlFirst = sqlFirst + oneTable.TableName ;   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 创建 SqlCommand 对象
InBlock.gif   if ( insertCommand == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    insertCommand 
= new SqlCommand(sqlFirst, dbConnection);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// Insert 语句的参数表集合
InBlock.gif   SqlParameterCollection insertParams = insertCommand.Parameters;
InBlock.gif   insertCommand.Parameters.Clear();
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 遍历 DataTable 字段,生成 SQL INSERT 语句
InBlock.gif   bool isFirstColumn = true;
InBlock.gif   
foreach (BaseDataColumn dc in oneTable.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 忽略无用字段
InBlock.gif    if ( dc.IsValidColumn() && !dc.AutoIncrement)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
if ( isFirstColumn )
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      sqlFirst 
= sqlFirst + " (" + dc.ColumnName;
InBlock.gif      isFirstColumn 
= false;
InBlock.gif      sqlSecond 
= sqlSecond +"@"+dc.ColumnName;
ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      sqlFirst 
= sqlFirst + "" + dc.ColumnName;
InBlock.gif      sqlSecond 
= sqlSecond + "," +"@"+dc.ColumnName;
ExpandedSubBlockEnd.gif     }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 生成 Insert 参数表集合
InBlock.gif     insertParams.Add(new SqlParameter("@"+dc.ColumnName, dc.GetColumnSqlDbType()));
InBlock.gif     insertParams[
"@"+dc.ColumnName].SourceColumn = dc.ColumnName;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }
            
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 设置 Command 类型和完整的 UPDATE 语句 
InBlock.gif   insertCommand.CommandType = CommandType.Text;
InBlock.gif   insertCommand.CommandText 
= sqlFirst + "" + sqlSecond + " )";
InBlock.gif   
return insertCommand;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 检查传入的单个数据表单(DataTable)字段域,自动获得该数据表单的“Delete”命令。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="oneTable">单个数据表单</param>
ExpandedSubBlockEnd.gif  
/// <returns>SqlCommand</returns>

InBlock.gif  private SqlCommand GetDeleteByIdCommand(DataTable oneTable)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
string sqlFirst = " DELETE FROM ";
InBlock.gif   sqlFirst 
= sqlFirst + oneTable.TableName + " WHERE ";   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 创建 SqlCommand 对象
InBlock.gif   if ( deleteCommand == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    deleteCommand 
= new SqlCommand(sqlFirst, dbConnection);
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// Delete 语句的参数表集合
InBlock.gif   SqlParameterCollection deleteParams = deleteCommand.Parameters;
InBlock.gif   deleteCommand.Parameters.Clear();
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 遍历 DataTable 字段,生成 SQL DELETE 语句
InBlock.gif   bool isFirstKeyColumn = true;
InBlock.gif   
foreach (BaseDataColumn dc in oneTable.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 忽略无用字段
InBlock.gif    if ( dc.IsValidColumn() )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 获取关键字字段
InBlock.gif     if ( dc.IsKeyColumn() ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
if ( isFirstKeyColumn )
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       sqlFirst 
= sqlFirst + dc.ColumnName + " = @"+dc.ColumnName;
InBlock.gif       isFirstKeyColumn 
= false;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else
InBlock.gif       sqlFirst 
= sqlFirst + " AND " + dc.ColumnName + " = @"+dc.ColumnName;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// 生成 Delete 参数表集合
InBlock.gif      deleteParams.Add(new SqlParameter("@"+dc.ColumnName, dc.GetColumnSqlDbType()));
InBlock.gif      deleteParams[
"@"+dc.ColumnName].SourceColumn = dc.ColumnName;
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }
   
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 设置 Command 类型和完整的 DELETE 语句 
InBlock.gif   deleteCommand.CommandType = CommandType.Text;
InBlock.gif   deleteCommand.CommandText 
= sqlFirst;
InBlock.gif   
return deleteCommand;
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 根据指定的对象数据集,更新相应的数据库。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="dataSet">对象数据集</param>
ExpandedSubBlockEnd.gif  
/// <returns>返回是否成功的布尔值</returns>

InBlock.gif  public bool UpdateObjectData(BaseDataSet myDataSet)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if ( dsAdapter == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw new System.ObjectDisposedException( GetType().FullName );
ExpandedSubBlockEnd.gif   }

InBlock.gif          
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dbConnection.Open();
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dsTransaction 
= dbConnection.BeginTransaction();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dbConnection.Close();
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif    }
    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 遍历 DataSet 中的每一个表单进行更新处理
InBlock.gif    foreach (DataTable dt in myDataSet.Tables)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dsAdapter.UpdateCommand 
= GetUpdateByIdCommand(dt); 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 设置记录更新的事务句柄 
InBlock.gif    dsAdapter.UpdateCommand.Transaction = dsTransaction;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 根据传入DataSet的表和表中的内容更新相应的数据库 
InBlock.gif    dsAdapter.Update( myDataSet, dt.TableName);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dsTransaction.Commit();
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dsTransaction.Rollback();
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 无论更新是否成功,及时释放数据库连接 
InBlock.gif    dbConnection.Close();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 刷新 DataSet 更新标志
InBlock.gif    myDataSet.AcceptChanges();    
InBlock.gif    
return true;       
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
string a=e.Message;
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif   }
  
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 根据指定的对象数据集,新增插入相应的数据库。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="myDataSet">对象数据集</param>
ExpandedSubBlockEnd.gif  
/// <returns>返回是否成功的布尔值</returns>

InBlock.gif  public bool InsertObjectData(BaseDataSet myDataSet)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if ( dsAdapter == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw new System.ObjectDisposedException( GetType().FullName );
ExpandedSubBlockEnd.gif   }
        
InBlock.gif   
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dbConnection.Open();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 启动事务处理
InBlock.gif    try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dsTransaction 
= dbConnection.BeginTransaction();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dbConnection.Close();
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif    }
    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 遍历 DataSet 中的每一个表单进行更新处理
InBlock.gif    try 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 调用自动产生关键字ID的方法。
InBlock.gif     if ( AutoCreatID(myDataSet,dsTransaction) )
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
foreach (DataTable dt in myDataSet.Tables)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       dsAdapter.InsertCommand 
= GetInsertCommand(dt);   
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 设置记录更新的事务句柄 
InBlock.gif       dsAdapter.InsertCommand.Transaction = dsTransaction;
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 根据传入DataSet的表和表中的内容更新相应的数据库 
InBlock.gif       dsAdapter.Update( myDataSet, dt.TableName);
ExpandedSubBlockEnd.gif      }
     
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// 提交事务
InBlock.gif      dsTransaction.Commit();
ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// 回滚事务
InBlock.gif      dsTransaction.Rollback();
InBlock.gif      
return false;
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 回滚事务
InBlock.gif     string a=e.Message;
InBlock.gif     dsTransaction.Rollback();
InBlock.gif     
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 无论更新是否成功,及时释放数据库连接 
InBlock.gif     dbConnection.Close();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 刷新 DataSet 更新标志
InBlock.gif    myDataSet.AcceptChanges();    
InBlock.gif    
return true;       
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
string a=e.Message;
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 根据指定的对象数据集,删除相应的数据库记录。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="myDataSet">对象数据集</param>
ExpandedSubBlockEnd.gif  
/// <returns>返回是否成功的布尔值</returns>

InBlock.gif  public bool DeleteObjectData(BaseDataSet myDataSet)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if ( dsAdapter == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw new System.ObjectDisposedException( GetType().FullName );
ExpandedSubBlockEnd.gif   }
            
InBlock.gif   
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dbConnection.Open();
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dsTransaction 
= dbConnection.BeginTransaction();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    dbConnection.Close();
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif    }
    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 遍历 DataSet 中的每一个表单进行更新处理
InBlock.gif    try 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
foreach (DataTable dt in myDataSet.Tables)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif     dsAdapter.DeleteCommand 
= GetDeleteByIdCommand(dt);
ExpandedSubBlockStart.gifContractedSubBlock.gif                                         
/**//// 设置记录更新的事务句柄 
InBlock.gif     dsAdapter.DeleteCommand.Transaction = dsTransaction;
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 根据传入DataSet的表和表中的内容更新相应的数据库 
InBlock.gif     dsAdapter.Update( myDataSet, dt.TableName);
ExpandedSubBlockEnd.gif     }

InBlock.gif     dsTransaction.Commit();
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
string a=e.Message;
InBlock.gif     dsTransaction.Rollback();
InBlock.gif     
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 无论更新是否成功,及时释放数据库连接 
InBlock.gif    dbConnection.Close();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 刷新 DataSet 更新标志
InBlock.gif    myDataSet.AcceptChanges();
InBlock.gif    
return true;      
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif   
return false;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }
  
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 根据指定的对象数据集,同时删除、修改、添加相应的数据库记录。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="myDataSet">对象数据集</param>
ExpandedSubBlockEnd.gif  
/// <returns>返回是否成功的布尔值</returns>

InBlock.gif  public bool SynchronizeObjectData(BaseDataSet dataSet)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if ( dsAdapter == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw new System.ObjectDisposedException( GetType().FullName );
ExpandedSubBlockEnd.gif   }
            
InBlock.gif   
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    dbConnection.Open();
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     dsTransaction 
= dbConnection.BeginTransaction();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     dbConnection.Close();
InBlock.gif     
return false;
ExpandedSubBlockEnd.gif    }
    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 遍历 DataSet 中的每一个表单进行更新处理
InBlock.gif    try 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 调用自动产生关键字ID的方法。
InBlock.gif     if ( AutoCreatID(dataSet,dsTransaction) )
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
foreach (DataTable dt in dataSet.Tables)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 删除相应记录。
InBlock.gif       dsAdapter.DeleteCommand = GetDeleteByIdCommand(dt);            
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 设置记录删除的事务句柄 
InBlock.gif       dsAdapter.DeleteCommand.Transaction = dsTransaction;
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 修改相应记录。
InBlock.gif       dsAdapter.UpdateCommand = GetUpdateByIdCommand(dt);            
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 设置记录更新的事务句柄 
InBlock.gif       dsAdapter.UpdateCommand.Transaction = dsTransaction;
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 新增相应记录。
InBlock.gif       dsAdapter.InsertCommand = GetInsertCommand(dt);     
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 设置记录更新的事务句柄 
InBlock.gif       dsAdapter.InsertCommand.Transaction = dsTransaction;
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// 根据传入DataSet的表和表中的内容删除/更新/新增相应的数据库 
InBlock.gif       dsAdapter.Update( dataSet, dt.TableName);
ExpandedSubBlockEnd.gif      }
     
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// 提交事务
InBlock.gif      dsTransaction.Commit();
ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// 回滚事务
InBlock.gif      dsTransaction.Rollback();
InBlock.gif      
return false;
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     dsTransaction.Rollback();
InBlock.gif     
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**//// 无论更新是否成功,及时释放数据库连接 
InBlock.gif     dbConnection.Close();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 刷新 DataSet 更新标志
InBlock.gif    dataSet.AcceptChanges();    
InBlock.gif    
return true;       
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return false;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
InBlock.gif     
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 根据传入关键字值列表,填充相应的对象数据集。
InBlock.gif  
/// 注意:关键字值列表为“@字段名,关键字值”格式的参数表。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="dataSet">用于装配的数据集对象</param>
InBlock.gif  
/// <param name="keyTable">对象的关键字(可能多个)和值参数表</param>
ExpandedSubBlockEnd.gif  
/// <returns>返回是否成功的布尔值</returns>

InBlock.gif  public BaseDataSet FillDataSetByArray(BaseDataSet dataSet, ParmArrayWithOP keyArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{       
InBlock.gif     
if ( dsAdapter == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
throw new System.ObjectDisposedException( GetType().FullName );
ExpandedSubBlockEnd.gif     }
   
InBlock.gif     
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
foreach (DataTable dt in dataSet.Tables)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       dsAdapter.SelectCommand 
= GetSelectBySqlOP(dt,keyArray);
InBlock.gif       
// 设置关键字的值
InBlock.gif
       foreach (SqlParameter pt in dsAdapter.SelectCommand.Parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif        dsAdapter.SelectCommand.Parameters[pt.ParameterName].Value 
= keyArray.GetParmValue(keyArray.IndexOf(pt.ParameterName));
ExpandedSubBlockEnd.gif       }
  
InBlock.gif       dsAdapter.Fill(dataSet,dt.TableName);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// 写入例外处理错误消息。
InBlock.gif      //MessagerBulletin.WriteMsg(e.Message);
InBlock.gif
      return dataSet;
ExpandedSubBlockEnd.gif     }
     
InBlock.gif     
return dataSet;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
///说明:    该方法将取得带有分页显示功能的SELECT语句。目的是一次只返回一页的记录,而不是整个表记录。
InBlock.gif  
///      该方法有两种重载形式,分别为为带条件的查询和不带条件的查询。
InBlock.gif  
///参数: intPageNum:每页的显示的记录数
InBlock.gif  
///   intPageIndex:当前页号
InBlock.gif  
///   strTableName:表名
InBlock.gif  
///   strKey":主键
InBlock.gif  
///   strParam[]:参数数组。此值仅当blnCondition为true时有效。
InBlock.gif  
///   objValues[]:值数组。此值仅当blnCondition为ture时有效。
InBlock.gif  
///   strOrderField:排序的字段名
InBlock.gif  
///   strOrderType:排序类型,是升序还是降序。其值为:ASC-升序,DESC-降序
InBlock.gif  
///返回:select语句,其中输出参数返回取得记录总数的SQL语句,方法本身返回查询SQL语句
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif
InBlock.gif  
public string GetPageSelectByID(int intPageNum,int intPageIndex,string strTableName,string strKey,string[] strParams,object[] objValues,string strOrderField,string strOrderType,out string strSqlCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
string strSql; 
InBlock.gif   
string strTemp = " ";   //存放条件字句
InBlock.gif   
InBlock.gif   
//构造select语句    
InBlock.gif
   strSql = "select Top " + intPageNum.ToString() + " * from " + strTableName + " where " 
InBlock.gif    
+ strKey + " not in (select top " + ((intPageIndex - 1* intPageNum).ToString() + " " + strKey + " from " + strTableName; 
InBlock.gif    
InBlock.gif   
//在生成的select语句上加上where条件子句
InBlock.gif
   if ((strParams.Length == 0| (strParams.Length != objValues.Length))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw (new ParamValueNotMatchException("缺少参数值或参数与值不对应"));
ExpandedSubBlockEnd.gif   }

InBlock.gif   
for (int i=0;i <= strParams.Length - 1;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    strTemp 
=  strTemp + " and " + strParams[i] + "=" + "'" + objValues[i].ToString() + "'";   
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
char[] and = new char[] dot.gif{'a','n','d'};
InBlock.gif   strTemp 
= (strTemp.TrimStart()).TrimStart(and);   //去除头部多余的"and"
InBlock.gif
   strSql = strSql + " where " + strTemp +  " order by " + strOrderField + " " + strOrderType + ")" 
InBlock.gif    
+ " and " + strTemp +  " order by " + strOrderField + " " + strOrderType ;
InBlock.gif   
//得到查询总记录数的语句    
InBlock.gif
   strSqlCount = "select count(*) from " + strTableName + " where " + strTemp;   
InBlock.gif   
return strSql;
ExpandedSubBlockEnd.gif  }
  
InBlock.gif
InBlock.gif

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=526545

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值