分享
 
 
 

为ASP.NET封装的SQL数据库访问类

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

using System;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Collections;

namespace MyCorporation.DepartMent.DataBase

{

/// <summary>

/// 通用数据库类

/// </summary>

public class DataBase

{

private string ConnStr = null;

public DataBase()

{

ConnStr = ConfigurationSettings.AppSettings["ConnStr"];

}

public DataBase(string Str)

{

try

{

this.ConnStr = Str;

}

catch(Exception ex)

{

throw ex;

}

}

/// <summary>

/// 返回connection对象

/// </summary>

/// <returns></returns>

public SqlConnection ReturnConn()

{

SqlConnection Conn = new SqlConnection(ConnStr);

Conn.Open();

return Conn;

}

public void Dispose(SqlConnection Conn)

{

if(Conn!=null)

{

Conn.Close();

Conn.Dispose();

}

GC.Collect();

}

/// <summary>

/// 运行SQL语句

/// </summary>

/// <param name="SQL"></param>

public void RunProc(string SQL)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlCommand Cmd ;

Cmd = CreateCmd(SQL, Conn);

try

{

Cmd.ExecuteNonQuery();

}

catch

{

throw new Exception(SQL);

}

Dispose(Conn);

return;

}

/// <summary>

/// 运行SQL语句返回DataReader

/// </summary>

/// <param name="SQL"></param>

/// <returns>SqlDataReader对象.</returns>

public SqlDataReader RunProcGetReader(string SQL)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlCommand Cmd ;

Cmd = CreateCmd(SQL, Conn);

SqlDataReader Dr;

try

{

Dr = Cmd.ExecuteReader(CommandBehavior.Default);

}

catch

{

throw new Exception(SQL);

}

//Dispose(Conn);

return Dr;

}

/// <summary>

/// 生成Command对象

/// </summary>

/// <param name="SQL"></param>

/// <param name="Conn"></param>

/// <returns></returns>

public SqlCommand CreateCmd(string SQL, SqlConnection Conn)

{

SqlCommand Cmd ;

Cmd = new SqlCommand(SQL, Conn);

return Cmd;

}

/// <summary>

/// 生成Command对象

/// </summary>

/// <param name="SQL"></param>

/// <returns></returns>

public SqlCommand CreateCmd(string SQL)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlCommand Cmd ;

Cmd = new SqlCommand(SQL, Conn);

return Cmd;

}

/// <summary>

/// 返回adapter对象

/// </summary>

/// <param name="SQL"></param>

/// <param name="Conn"></param>

/// <returns></returns>

public SqlDataAdapter CreateDa(string SQL)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlDataAdapter Da;

Da = new SqlDataAdapter(SQL, Conn);

return Da;

}

/// <summary>

/// 运行SQL语句,返回DataSet对象

/// </summary>

/// <param name="procName">SQL语句</param>

/// <param name="prams">DataSet对象</param>

public DataSet RunProc(string SQL ,DataSet Ds)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlDataAdapter Da;

//Da = CreateDa(SQL, Conn);

Da = new SqlDataAdapter(SQL,Conn);

try

{

Da.Fill(Ds);

}

catch(Exception Err)

{

throw Err;

}

Dispose(Conn);

return Ds;

}

/// <summary>

/// 运行SQL语句,返回DataSet对象

/// </summary>

/// <param name="procName">SQL语句</param>

/// <param name="prams">DataSet对象</param>

/// <param name="dataReader">表名</param>

public DataSet RunProc(string SQL ,DataSet Ds,string tablename)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlDataAdapter Da;

Da = CreateDa(SQL);

try

{

Da.Fill(Ds,tablename);

}

catch(Exception Ex)

{

throw Ex;

}

Dispose(Conn);

return Ds;

}

/// <summary>

/// 运行SQL语句,返回DataSet对象

/// </summary>

/// <param name="procName">SQL语句</param>

/// <param name="prams">DataSet对象</param>

/// <param name="dataReader">表名</param>

public DataSet RunProc(string SQL , DataSet Ds ,int StartIndex ,int PageSize, string tablename )

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlDataAdapter Da ;

Da = CreateDa(SQL);

try

{

Da.Fill(Ds, StartIndex, PageSize, tablename);

}

catch(Exception Ex)

{

throw Ex;

}

Dispose(Conn);

return Ds;

}

/// <summary>

/// 检验是否存在数据

/// </summary>

/// <returns></returns>

public bool ExistDate(string SQL)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlDataReader Dr ;

Dr = CreateCmd(SQL,Conn).ExecuteReader();

if (Dr.Read())

{

Dispose(Conn);

return true;

}

else

{

Dispose(Conn);

return false;

}

}

/// <summary>

/// 返回SQL语句执行结果的第一行第一列

/// </summary>

/// <returns>字符串</returns>

public string ReturnValue(string SQL)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

string result;

SqlDataReader Dr ;

try

{

Dr = CreateCmd(SQL,Conn).ExecuteReader();

if (Dr.Read())

{

result = Dr[0].ToString();

Dr.Close();

}

else

{

result = "";

Dr.Close();

}

}

catch

{

throw new Exception(SQL);

}

Dispose(Conn);

return result;

}

/// <summary>

/// 返回SQL语句第一列,第ColumnI列,

/// </summary>

/// <returns>字符串</returns>

public string ReturnValue(string SQL, int ColumnI)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

string result;

SqlDataReader Dr ;

try

{

Dr = CreateCmd(SQL,Conn).ExecuteReader();

}

catch

{

throw new Exception(SQL);

}

if (Dr.Read())

{

result = Dr[ColumnI].ToString();

}

else

{

result = "";

}

Dr.Close();

Dispose(Conn);

return result;

}

/// <summary>

/// 生成一个存储过程使用的sqlcommand.

/// </summary>

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

/// <param name="prams">存储过程入参数组.</param>

/// <returns>sqlcommand对象.</returns>

public SqlCommand CreateCmd(string procName, SqlParameter[] prams)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlCommand Cmd = new SqlCommand(procName, Conn);

Cmd.CommandType = CommandType.StoredProcedure;

if (prams != null)

{

foreach (SqlParameter parameter in prams)

{

if(parameter != null)

{

Cmd.Parameters.Add(parameter);

}

}

}

return Cmd;

}

/// <summary>

/// 为存储过程生成一个SqlCommand对象

/// </summary>

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

/// <param name="prams">存储过程参数</param>

/// <returns>SqlCommand对象</returns>

private SqlCommand CreateCmd(string procName, SqlParameter[] prams,SqlDataReader Dr)

{

SqlConnection Conn;

Conn = new SqlConnection(ConnStr);

Conn.Open();

SqlCommand Cmd = new SqlCommand(procName, Conn);

Cmd.CommandType = CommandType.StoredProcedure;

if (prams != null)

{

foreach (SqlParameter parameter in prams)

Cmd.Parameters.Add(parameter);

}

Cmd.Parameters.Add(

new SqlParameter("ReturnValue", SqlDbType.Int, 4,

ParameterDirection.ReturnValue, false, 0, 0,

string.Empty, DataRowVersion.Default, null));

return Cmd;

}

/// <summary>

/// 运行存储过程,返回.

/// </summary>

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

/// <param name="prams">存储过程参数</param>

/// <param name="dataReader">SqlDataReader对象</param>

public void RunProc(string procName, SqlParameter[] prams, SqlDataReader Dr)

{

SqlCommand Cmd = CreateCmd(procName, prams, Dr);

Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

return;

}

/// <summary>

/// 运行存储过程,返回.

/// </summary>

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

/// <param name="prams">存储过程参数</param>

public string RunProc(string procName, SqlParameter[] prams)

{

SqlDataReader Dr;

SqlCommand Cmd = CreateCmd(procName, prams);

Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

if(Dr.Read())

{

return Dr.GetValue(0).ToString();

}

else

{

return "";

}

}

/// <summary>

/// 运行存储过程,返回dataset.

/// </summary>

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

/// <param name="prams">存储过程入参数组.</param>

/// <returns>dataset对象.</returns>

public DataSet RunProc(string procName,SqlParameter[] prams,DataSet Ds)

{

SqlCommand Cmd = CreateCmd(procName,prams);

SqlDataAdapter Da = new SqlDataAdapter(Cmd);

try

{

Da.Fill(Ds);

}

catch(Exception Ex)

{

throw Ex;

}

return Ds;

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有