分享
 
 
 

封装的ADO.NET对数据库操作经典类

王朝c#·作者佚名  2006-05-06
窄屏简体版  字體: |||超大  

using System;

using System.Collections;

using System.Collections.Specialized;

using System.Runtime.Remoting.Messaging;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

namespace LTP.SQLServerDAL

{

/// <summary>

/// ADO.NET数据库操作基础类。

/// </summary>

public abstract class DbManagerSQL

{

//数据库连接字符串

protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

public DbManagerSQL()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

/// <summary>

/// 执行SQL语句,返回影响的记录数

/// </summary>

/// <param name="SQLString"></param>

/// <returns></returns>

public static int ExecuteSql(string SQLString)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

using (SqlCommand cmd = new SqlCommand(SQLString,connection))

{

try

{

connection.Open();

int rows=cmd.ExecuteNonQuery();

return rows;

}

catch(System.Data.SqlClient.SqlException E)

{

throw new Exception(E.Message);

}

}

}

}

/// <summary>

/// 执行两条SQL语句,实现数据库事务。

/// </summary>

/// <param name="SQLString1"></param>

/// <param name="SQLString2"></param>

public static void ExecuteSqlTran(string SQLString1,string SQLString2)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

connection.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection=connection;

SqlTransaction tx=connection.BeginTransaction();

cmd.Transaction=tx;

try

{

cmd.CommandText=SQLString1;

cmd.ExecuteNonQuery();

cmd.CommandText=SQLString2;

cmd.ExecuteNonQuery();

tx.Commit();

}

catch(System.Data.SqlClient.SqlException E)

{

tx.Rollback();

throw new Exception(E.Message);

}

finally

{

cmd.Dispose();

connection.Close();

}

}

}

/// <summary>

/// 执行多条SQL语句,实现数据库事务,每条语句以“;”分割。

/// </summary>

/// <param name="SQLStringList"></param>

public static void ExecuteSqlTran(string SQLStringList)

{

using (OdbcConnection conn = new OdbcConnection(connectionString))

{

conn.Open();

OdbcCommand cmd = new OdbcCommand();

cmd.Connection=conn;

OdbcTransaction tx=conn.BeginTransaction();

cmd.Transaction=tx;

try

{

string [] split= SQLStringList.Split(new Char [] { ';'});

foreach (string strsql in split)

{

if (strsql.Trim()!="")

{

cmd.CommandText=strsql;

cmd.ExecuteNonQuery();

}

}

tx.Commit();

}

catch(System.Data.Odbc.OdbcException E)

{

tx.Rollback();

throw new Exception(E.Message);

}

}

}

/// <summary>

/// 执行带一个存储过程参数的的SQL语句。

/// </summary>

/// <param name="SQLString"></param>

/// <param name="content"></param>

/// <returns></returns>

public static int ExecuteSql(string SQLString,string content)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand cmd = new SqlCommand(SQLString,connection);

System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter ( "@content", SqlDbType.NText);

myParameter.Value = content ;

cmd.Parameters.Add(myParameter);

try

{

connection.Open();

int rows=cmd.ExecuteNonQuery();

return rows;

}

catch(System.Data.SqlClient.SqlException E)

{

throw new Exception(E.Message);

}

finally

{

cmd.Dispose();

connection.Close();

}

}

}

/// <summary>

/// 向数据库里插入图像格式的字段

/// </summary>

/// <param name="strSQL"></param>

/// <param name="fs"></param>

/// <returns></returns>

public static int ExecuteSqlInsertImg(string strSQL,byte[] fs)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand cmd = new SqlCommand(strSQL,connection);

System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter ( "@fs", SqlDbType.Image);

myParameter.Value = fs ;

cmd.Parameters.Add(myParameter);

try

{

connection.Open();

int rows=cmd.ExecuteNonQuery();

return rows;

}

catch(System.Data.SqlClient.SqlException E)

{

throw new Exception(E.Message);

}

finally

{

cmd.Dispose();

connection.Close();

}

}

}

/// <summary>

/// 执行一条计算查询结果语句,返回查询结果(整数)。

/// </summary>

/// <param name="strSQL"></param>

/// <returns></returns>

public static int GetCount(string strSQL)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand cmd = new SqlCommand(strSQL,connection);

try

{

connection.Open();

SqlDataReader result = cmd.ExecuteReader();

int i=0;

while(result.Read())

{

i=result.GetInt32(0);

}

result.Close();

return i;

}

catch(System.Data.SqlClient.SqlException e)

{

throw new Exception(e.Message);

}

finally

{

cmd.Dispose();

connection.Close();

}

}

}

/// <summary>

/// 执行一条计算查询结果语句,返回查询结果(object)。

/// </summary>

/// <param name="SQLString"></param>

/// <returns></returns>

public static object GetSingle(string SQLString)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand cmd = new SqlCommand(SQLString,connection);

try

{

connection.Open();

object obj = cmd.ExecuteScalar();

if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))

{

return null;

}

else

{

return obj;

}

}

catch(System.Data.SqlClient.SqlException e)

{

throw new Exception(e.Message);

}

finally

{

cmd.Dispose();

connection.Close();

}

}

}

/// <summary>

/// 执行查询语句,返回SqlDataReader

/// </summary>

/// <param name="strSQL"></param>

/// <returns></returns>

public static SqlDataReader ExecuteReader(string strSQL)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand cmd = new SqlCommand(strSQL,connection);

SqlDataReader myReader;

try

{

connection.Open();

myReader = cmd.ExecuteReader();

return myReader;

}

catch(System.Data.SqlClient.SqlException e)

{

throw new Exception(e.Message);

}

finally

{

cmd.Dispose();

connection.Close();

}

}

}

/// <summary>

/// 执行查询语句,返回DataSet

/// </summary>

/// <param name="SQLString"></param>

/// <returns></returns>

public static DataSet Query(string SQLString)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

DataSet ds = new DataSet();

try

{

connection.Open();

SqlDataAdapter command = new SqlDataAdapter(SQLString,connection);

command.Fill(ds,"ds");

}

catch(System.Data.SqlClient.SqlException ex)

{

throw new Exception(ex.Message);

}

return ds;

}

}

#region 存储过程操作

/// <summary>

/// 运行存储过程

/// </summary>

/// <param name="storedProcName"></param>

/// <param name="parameters"></param>

/// <returns></returns>

public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlDataReader returnReader;

connection.Open();

SqlCommand command = BuildQueryCommand( connection,storedProcName, parameters );

command.CommandType = CommandType.StoredProcedure;

returnReader = command.ExecuteReader();

//Connection.Close();

return returnReader;

}

}

private static SqlCommand BuildQueryCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters)

{

SqlCommand command = new SqlCommand( storedProcName, connection );

command.CommandType = CommandType.StoredProcedure;

foreach (SqlParameter parameter in parameters)

{

command.Parameters.Add( parameter );

}

return command;

}

public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

DataSet dataSet = new DataSet();

connection.Open();

SqlDataAdapter sqlDA = new SqlDataAdapter();

sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters );

sqlDA.Fill( dataSet, tableName );

connection.Close();

return dataSet;

}

}

#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- 王朝網路 版權所有