分享
 
 
 

自编自用DataProxy

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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;

if( arrColumns[0].Trim() == "*" )

{

strQuery += " *,";

nStart = 1;

}

for( int i = nStart; i < arrColumns.Length; i++ )

{

string[] arrColumn = arrColumns[i].Trim().Split( ' ' );

strQuery += " [" + arrColumn[0].Trim() + "]";

if( arrColumn.Length > 1 )

{

strQuery += " [" + arrColumn[1].Trim() + "]";

}

strQuery += ",";

}

strQuery = strQuery.Substring( 0, strQuery.Length - 1 ) + " 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="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>查询结果</returns>

public static DataSet GetDBData( string Columns, string Target, string Condition, string Connect, ref string Ex )

{

Ex = string.Empty;

DataSet ds = GetDBData( Columns, Target, Condition, null, Connect, ref Ex );

if( Ex != string.Empty )

{

return null;

}

return ds;

}

/// <summary>

/// 从数据库查询数据

/// </summary>

/// <param name="Columns">查询列名</param>

/// <param name="Target">查询目标</param>

/// <param name="PKColumn">主键列名</param>

/// <param name="PKValue">主键值</param>

/// <param name="Q">是否加引号</param>

/// <param name="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>返回查询结果</returns>

public static DataTable GetDBData( string Columns, string Target, string PKColumn, string PKValue, bool Q, string Connect, ref string Ex )

{

Ex = string.Empty;

string strCondition = null;

if( PKColumn != null )

{

strCondition = "[" + PKColumn + "] = ";

if( Q )

{

strCondition += "'" + PKValue + "'";

}

else

{

strCondition += PKValue;

}

}

DataSet ds = GetDBData( Columns, Target, strCondition, Connect, ref Ex );

if( Ex != string.Empty )

{

return null;

}

return ds.Tables[0];

}

#endregion

#region 执行存储过程

/// <summary>

/// 执行某个存储过程通过参数返回值

/// </summary>

/// <param name="P">存储过程名</param>

/// <param name="xsd">数据集对象</param>

/// <param name="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>执行结果</returns>

public static void ExecParam( string P, DataSet xsd, string Connect, ref string Ex )

{

SqlConnection sqlConnection = new SqlConnection( Connect );

try

{

SqlCommand sqlCommand = new SqlCommand( P, sqlConnection );

sqlCommand.CommandType = CommandType.StoredProcedure;

if( xsd.Tables["IN"] != null )

{

foreach( DataColumn dc in xsd.Tables["IN"].Columns )

{

sqlCommand.Parameters.Add( "@" + dc.ColumnName, dc.DataType );

sqlCommand.Parameters["@" + dc.ColumnName].Value = dc.Table.Rows[0][dc.ColumnName];

}

}

if( xsd.Tables["OUT"] != null )

{

foreach( DataColumn dc in xsd.Tables["OUT"].Columns )

{

SqlParameter sqlParameter = new SqlParameter( "@" + dc.ColumnName, dc.DataType );

sqlParameter.Direction = ParameterDirection.InputOutput;

sqlParameter.Value = System.DBNull.Value;

if( xsd.Tables["OUT"].Rows.Count > 0 )

{

sqlParameter.Value = dc.Table.Rows[0][dc.ColumnName];

}

sqlCommand.Parameters.Add( sqlParameter );

}

}

sqlConnection.Open();

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;

}

}

}

catch( SqlException ex )

{

Ex = ex.Message;

}

finally

{

sqlConnection.Close();

}

}

#endregion

#region 四种基本语句

/// <summary>

/// 执行SELECT

/// </summary>

/// <param name="Text">SELECT后的文本</param>

/// <param name="ds">输出查询结果</param>

/// <param name="srcTable">用于表映射的源表的名称</param>

/// <param name="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>返回行数</returns>

public static int Select( string Text, ref DataSet ds, string srcTable, string Connect, ref string Ex )

{

return Fill( "SELECT " + Text, ref ds, srcTable, Connect, ref Ex );

}

public static int Select( string Text, ref DataSet ds, string Connect, ref string Ex )

{

return Fill( "SELECT " + Text, ref ds, Connect, ref Ex );

}

/// <summary>

/// 执行一串SQL语句

/// </summary>

/// <param name="Text">SQL文本</param>

/// <param name="ds">输出查询结果</param>

/// <param name="srcTable">用于表映射的源表的名称</param>

/// <param name="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>返回行数</returns>

public static int Fill( string Text, ref DataSet ds, string srcTable, string Connect, ref string Ex )

{

Ex = string.Empty;

int nFill = 0;

try

{

SqlDataAdapter sqlDataAdapter = new SqlDataAdapter( Text, Connect );

nFill = sqlDataAdapter.Fill( ds, srcTable );

return nFill;

}

catch( SqlException ex )

{

Ex = ex.Message;

return nFill;

}

catch( Exception ex )

{

throw ex;

}

}

public static int Fill( string Text, ref DataSet ds, string Connect, ref string Ex )

{

return Fill( Text, ref ds, "Table", Connect, ref Ex );

}

/// <summary>

/// 执行INSERT

/// </summary>

/// <param name="Text">INSERT后的文本</param>

/// <param name="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>返回行数</returns>

public static int Insert( string Text, string Connect, ref string Ex )

{

return ExecuteNonQuery( "INSERT " + Text, Connect, ref Ex );

}

/// <summary>

/// 执行UPDATE

/// </summary>

/// <param name="Text">UPDATE后的文本</param>

/// <param name="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>返回行数</returns>

public static int Update( string Text, string Connect, ref string Ex )

{

return ExecuteNonQuery( "UPDATE " + Text, Connect, ref Ex );

}

/// <summary>

/// 执行DELETE

/// </summary>

/// <param name="Text">DELETE后的文本</param>

/// <param name="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>返回行数</returns>

public static int Delete( string Text, string Connect, ref string Ex )

{

return ExecuteNonQuery( "DELETE " + Text, Connect, ref Ex );

}

/// <summary>

/// 执行某SQL语句(非SELECT子句)

/// </summary>

/// <param name="Text">SQL文本</param>

/// <param name="Connect">连接字符串</param>

/// <param name="Ex">异常信息</param>

/// <returns>返回行数</returns>

public static int ExecuteNonQuery( string Text, string Connect, ref string Ex )

{

int nFill = 0;

Ex = string.Empty;

SqlConnection sqlConnection = new SqlConnection( Connect );

try

{

SqlCommand sqlCommand = new SqlCommand( Text, sqlConnection );

sqlConnection.Open();

nFill = sqlCommand.ExecuteNonQuery();

return nFill;

}

catch( SqlException ex )

{

sqlConnection.Close();

Ex = ex.Message;

return nFill;

}

catch( Exception ex )

{

sqlConnection.Close();

throw ex;

}

finally

{

sqlConnection.Close();

}

}

#endregion

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有