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
}
}