自编自用DataProxy

using System;

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

namespace DataProxy
{
 /// <summary>
 /// 说明:  本类主要实现对数据库的操作(查询|SP)
 /// 建立者:  黄宗银
 /// 建立时间: 2005-07-28
 /// </summary>
 public class Sql
 {

  #region 执行存储过程
  /// <summary>
  /// 执行某个存储过程通过参数返回值
  /// </summary>
  /// <param name="P">存储过程名</param>
  /// <param name="xsd">数据集对象</param>
  /// <param name="Connection">一个打开的数据库连接</param>
  /// <param name="Ex">异常信息</param>
  /// <returns>受影响的行数</returns>
  public static int ExecProc( string P, DataSet xsd, SqlConnection Connection, ref string Ex )
  {
   int nReturn = -1;
   try
   {
    SqlCommand sqlCommand = new SqlCommand( P, Connection );
    sqlCommand.CommandType = CommandType.StoredProcedure;
    AddParam( sqlCommand, xsd );

    nReturn = sqlCommand.ExecuteNonQuery();

    if( xsd.Tables["OUT"] != null )
    {
     DataRow dr = xsd.Tables["OUT"].NewRow();
     xsd.Tables["OUT"].Rows.InsertAt( dr, 0 );
     for( int i = 0; i < xsd.Tables["OUT"].Columns.Count; i++ )
     {
      string strColumnName = xsd.Tables["OUT"].Columns[i].ColumnName;
      xsd.Tables["OUT"].Rows[0][i] = sqlCommand.Parameters["@" + strColumnName].Value;
     }
    }

    return nReturn;
   }
   catch( SqlException ex )
   {
    Ex = ex.Message;
    return nReturn;
   }
  }
  #endregion

  #region  参数处理

  #region AddParam[Type]

  /// <summary>
  /// 添加参数
  /// </summary>
  public static void AddParam( SqlCommand sc, string ParamName, Type type )
  {
   sc.Parameters.Add( ParamName, type );
  }

  /// <summary>
  /// 添加参数
  /// </summary>
  public static void AddParam( SqlCommand sc, string ParamName, Type type, object ParamValue )
  {
   AddParam( sc, ParamName, type );
   sc.Parameters[ParamName].Value = ParamValue;
  }

  /// <summary>
  /// 添加参数
  /// </summary>
  public static void AddParam( SqlCommand sc, string ParamName, Type type, object ParamValue, ParameterDirection pd )
  {
   AddParam( sc, ParamName, type, ParamValue );
   sc.Parameters[ParamName].Direction = pd;
  }

  #endregion

  #region AddParam[SqlDbType]

  /// <summary>
  /// 添加参数
  /// </summary>
  public static void AddParam( SqlCommand sc, string ParamName, SqlDbType SDT )
  {
   sc.Parameters.Add( ParamName, SDT );
  }

  /// <summary>
  /// 添加参数
  /// </summary>
  public static void AddParam( SqlCommand sc, string ParamName, SqlDbType SDT, object ParamValue )
  {
   AddParam( sc, ParamName, SDT );
   sc.Parameters[ParamName].Value = ParamValue;
  }

  /// <summary>
  /// 添加参数
  /// </summary>
  public static void AddParam( SqlCommand sc, string ParamName, SqlDbType SDT, object ParamValue, ParameterDirection pd )
  {
   AddParam( sc, ParamName, SDT, ParamValue );
   sc.Parameters[ParamName].Direction = pd;
  }

  #endregion

  #region AddParam[DataSet]

  /// <summary>
  /// 从数据集取出参数及其值
  /// </summary>
  public static void AddParam( SqlCommand sc, DataSet xsd )
  {
   if( xsd.Tables["IN"] != null )
   {
    foreach( DataColumn dc in xsd.Tables["IN"].Columns )
    {
     AddParam( sc, "@" + dc.ColumnName, dc.DataType, dc.Table.Rows[0][dc.ColumnName] );
    }
   }

   if( xsd.Tables["OUT"] != null )
   {
    foreach( DataColumn dc in xsd.Tables["OUT"].Columns )
    {
     object Value = xsd.Tables["OUT"].Rows.Count > 0 ? dc.Table.Rows[0][dc.ColumnName] : System.DBNull.Value;
     AddParam( sc, "@" + dc.ColumnName, dc.DataType, Value, ParameterDirection.InputOutput );
    }
   }
  }

  #endregion

  #endregion

 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值