一个写的非常漂亮的数据库操作类
2007-05-09 14:54
429 查看
这段代码包含了除对视图的操作外的所有数据库操作,写的很漂亮,写在这里可以不断的欣赏 。
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 DB
{
/// <summary>
/// ADO.NET数据库操作基础类。
/// </summary>
public abstract class DbManagerSQL
{
//数据库连接字符串
protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
public DbManagerSQL()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// 执行SQL语句,返回影响的记录数
public static int ExecuteSql(string SQLString)
{
SqlConnection connection = new SqlConnection(connectionString);
{
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);
}
}
}
}
/// 执行两条SQL语句,实现数据库事务。
public static void ExecuteSqlTran(string SQLString1, string SQLString2)
{
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();
}
}
}
// 执行多条SQL语句,实现数据库事务,每条语句以“;”分割。
public static void ExecuteSqlTran(string SQLStringList)
{
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);
}
}
}
// 执行带一个存储过程参数的的SQL语句。
public static int ExecuteSql(string SQLString, string content)
{
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();
}
}
}
// 向数据库里插入图像格式的字段
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();
}
}
}
// 执行一条计算查询结果语句,返回查询结果(整数)。
public static int GetCount(string strSQL)
{
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();
}
}
}
/// 执行一条计算查询结果语句,返回查询结果(object)
public static object GetSingle(string SQLString)
{
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();
}
}
/// 执行查询语句,返回SqlDataReader
public static SqlDataReader ExecuteReader(string strSQL)
{
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();
}
}
// 执行查询语句,返回DataSet
public static DataSet Query(string SQLString)
{
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;
}
}
}
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 DB
{
/// <summary>
/// ADO.NET数据库操作基础类。
/// </summary>
public abstract class DbManagerSQL
{
//数据库连接字符串
protected static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
public DbManagerSQL()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// 执行SQL语句,返回影响的记录数
public static int ExecuteSql(string SQLString)
{
SqlConnection connection = new SqlConnection(connectionString);
{
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);
}
}
}
}
/// 执行两条SQL语句,实现数据库事务。
public static void ExecuteSqlTran(string SQLString1, string SQLString2)
{
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();
}
}
}
// 执行多条SQL语句,实现数据库事务,每条语句以“;”分割。
public static void ExecuteSqlTran(string SQLStringList)
{
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);
}
}
}
// 执行带一个存储过程参数的的SQL语句。
public static int ExecuteSql(string SQLString, string content)
{
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();
}
}
}
// 向数据库里插入图像格式的字段
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();
}
}
}
// 执行一条计算查询结果语句,返回查询结果(整数)。
public static int GetCount(string strSQL)
{
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();
}
}
}
/// 执行一条计算查询结果语句,返回查询结果(object)
public static object GetSingle(string SQLString)
{
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();
}
}
/// 执行查询语句,返回SqlDataReader
public static SqlDataReader ExecuteReader(string strSQL)
{
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();
}
}
// 执行查询语句,返回DataSet
public static DataSet Query(string SQLString)
{
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;
}
}
}
相关文章推荐
- android一个很好的数据库基类,利用此类非常方便构建数据库操作类
- 一个非常有用的delphi数据库操作类
- 一个很好的数据库基类,利用此类非常方便构建数据库操作类
- 一个效果非常不错的JAVA数据库连接池
- 一个数据库操作类
- 一个简单的数据库操作类
- 【推荐】一个非常漂亮的列表框
- 一个数据库操作类实例
- 【php】使用mysqli完成的一个数据库操作类
- asp.net 自己封装数据库操作一个类中一个自定义方法Execute(),非常实用,省去了麻烦的中间过程,动态参数
- 一个基于PDO的数据库操作类
- 一个效果非常不错的JAVA数据库连接池
- 一个方便有用的Delphi数据库操作类(转)
- 应有那个数据库操作类的一个简单实例
- Funny Linq Part2: 建一个 Linq 数据库通用的操作类 (下)
- 一个基于PDO的数据库操作类(新) 一个PDO事务实例
- 一个非常方便好用的ADO数据库连接字符串生成工具
- PHP实现的一个简单的数据库操作类(修改版)
- 【分享】分享一个非常漂亮且好用的绘图控件QCustomPlot - qwt的完美替代品
- 封装一个通用的数据库操作类