日期:2008-11-15  浏览次数:20472 次

 

using System;
using System.Data;
using System.Data.SqlClient;

namespace DataProxy
{
 /// <summary>
 /// 说明:  本类主要实现对数据库的操作(查询|SP)
 /// 建立者:  黄宗银
 /// 建立时间: 2004-12-4
 /// </summary>
 public class DataProxy
 {
  #region 读取数据
  /// <summary>
  /// 从数据库查询数据
  /// </summary>
  /// <param name="RowsCount">返回最 Top 的记录数</param>
  /// <param name="Columns">查询列名</param>
  /// <param name="Target">查询目标</param>
  /// <param name="Condition">查询条件</param>
  /// <param name="OrderBy">排序</param>
  /// <param name="Connect">连接字符串</param>
  /// <param name="Ex">异常信息</param>
  /// <returns>查询结果</returns>
  public static DataSet GetDBData( int RowsCount, string Columns, string Target, string Condition, string OrderBy, string Connect, ref string Ex )
  {
   Ex = string.Empty;
   SqlConnection sqlConnection = new SqlConnection( Connect );
   try
   {
    string[] arrColumns = Columns.Split( ',' );
    string strQuery = "SELECT Top " + RowsCount + " " + ((arrColumns[0].Trim() == "*") ? " *" : " [" + arrColumns[0].Trim() + "]");
    for( int i = 1; i < arrColumns.Length; i++ )
    {
     strQuery += ", [" + arrColumns[i].Trim() + "]";
    }
    strQuery += " FROM [" + Target + "]";
    
    if( Condition != null && Condition.Trim() != string.Empty )
    {
     strQuery += " WHERE " + Condition;
    }

    if( OrderBy != null && OrderBy.Trim() != string.Empty )
    {
     strQuery += " ORDER BY " + OrderBy;
    }

    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter( strQuery, sqlConnection );

    DataSet ds = new DataSet();
    sqlDataAdapter.Fill( ds );

    sqlConnection.Close();
    return ds;
   }
   catch( SqlException ex )
   {
    sqlConnection.Close();
    Ex = ex.Message;
    return null;
   }
  }

  /// <summary>
  /// 从数据库查询数据
  /// </summary>
  /// <param name="Columns">查询列名</param>
  /// <param name="Target">查询目标</param>
  /// <param name="Condition">查询条件</param>
  /// <param name="OrderBy">排序</param>
  /// <param name="Connect">连接字符串</param>
  /// <param name="Ex">异常信息</param>
  /// <returns>查询结果</returns>
  public static DataSet GetDBData( string Columns, string Target, string Condition, string OrderBy, string Connect, ref string Ex )
  {
   Ex = string.Empty;
   SqlConnection sqlConnection = new SqlConnection( Connect );
   try
   {
    string[] arrColumns = Columns.Split( ',' );
    string strQuery = "SELECT";
    int nStart = 0;
&n