根据资料改写的一个数据库组件

//数据库组件
//Powered by Sai,大雄
//QQ:2231068
//E-mail:[email protected]

using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace DataBase.SqlServer
{

public class SqlBase
{
//静态变量,保存数据库连接字符串,字符串从web.config配置文件中读取
protected static string m_strConn = ConfigurationSettings.AppSettings["strConn"];

//构造函数
public SqlBase()
{}

//静态方法,设置数据库连接字符串
public static void SetConnectionString(string strConn)
{
m_strConn = strConn;
}

//静态方法,取得数据库连接字符串
public static string GetConnectionString()
{
return m_strConn;
}

//静态方法,执行不返回数据集的Sql语句
public static int ExecuteSql(string strSql)
{
SqlConnection dbConn = new SqlConnection(m_strConn);
// myConnection.Open();
//SqlCommand myCommand = myConnection.CreateCommand();
SqlCommand dbCmd = new SqlCommand(strSql);
try
{
dbConn.Open();
dbCmd.ExecuteNonQuery();
return 0;//返回0表示查询成功
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
finally
{
dbCmd.Dispose();
dbConn.Close();
}
}

//执行SQL语句,判断是否返回数据
public static int ExecuteSqlEx(string strSql)
{
SqlConnection dbConn = new SqlConnection(m_strConn);
SqlCommand dbCmd = new SqlCommand(strSql);
try
{
dbConn.Open();
SqlDataReader myReader = dbCmd.ExecuteReader();
if(myReader.Read())
return 0;
else
throw new Exception("数据库里没有要查询的数据");
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
finally
{
dbCmd.Dispose();
dbConn.Close();
}

}

//执行SQL语句并返回数据集
public static DataSet ExecuteSqlDs(string strSql)
{
SqlConnection dbConn = new SqlConnection(m_strConn);
DataSet ds = new DataSet("ds");
try
{
dbConn.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(strSql,dbConn);
adapter.Fill(ds);
adapter.Dispose();
return ds;
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
finally
{

dbConn.Close();
}
}

//执行仅返回一个数字值结果的SQL语句
public static int ExecuteSqlValue(string strSql)
{
SqlConnection dbConn = new SqlConnection(m_strConn);
SqlCommand dbCmd = new SqlCommand(strSql);
try
{
dbConn.Open();
object r = dbCmd.ExecuteScalar();
if(object.Equals(r,null))
throw new Exception("没有数值");
else
return (int)r;
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
finally
{
dbCmd.Dispose();
dbConn.Close();
}
}

//执行仅返回一个数据的SQL语句,返回数据可以为任何类型
public static object ExecuteSqlValueEx(string strSql)
{
SqlConnection dbConn = new SqlConnection(m_strConn);
SqlCommand dbCmd = new SqlCommand(strSql);
try
{
dbConn.Open();
object r = dbCmd.ExecuteScalar();
if(object.Equals(r,null))
throw new Exception("没有对象");
else
return r;
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
finally
{
dbCmd.Dispose();
dbConn.Close();
}
}

//执行一组SQL语句,不返回数据集
public static int ExecuteSqls(string[] strSqls)
{
SqlConnection dbConn = new SqlConnection(m_strConn);
SqlCommand dbCmd = new SqlCommand();
//SqlTransaction类,表示要在 SQL Server 数据库中处理的 Transact-SQL 事务。不能继承此类。
//对 SqlTransaction 对象执行与该事务关联的所有后面的操作(例如提交或中止该事务)。
//应用程序通过在 SqlConnection 对象上调用 BeginTransaction 来创建 SqlTransaction 对象。
SqlTransaction myTrans = dbConn.BeginTransaction();
int j = strSqls.Length;//数组长度

try
{
dbConn.Open();
}
catch(SqlException e)
{
throw new Exception(e.Message);
}
dbCmd.Connection = dbConn;
dbCmd.Transaction = myTrans;
try
{
foreach(string str in strSqls)
{
dbCmd.CommandText = str;
dbCmd.ExecuteNonQuery();
}
//Commit 方法等效于 Transact-Sql COMMIT TRANSACTION 语句。
//提交事务
myTrans.Commit();
return 0;
}
catch(SqlException e)
{
//Rollback()方法:从挂起状态回滚事务
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
//SqlTransaction.Dispose();
dbCmd.Dispose();
dbConn.Close();
}
}
}
}

namespace DataBase.OleDB
{
public class OleBase
{
protected static string m_strConn = ConfigurationSettings.AppSettings["strConn"];

//静态方法,设置数据库连接字符串
public static void SetConnectionString(string strConn)
{
m_strConn = strConn;
}

//静态方法,取得数据库连接字符串
public static string GetConnectionString()
{
return m_strConn;
}

//静态方法,执行不返回数据集的Sql语句
public static int ExecuteSql(string strSql)
{
OleDbConnection dbConn = new OleDbConnection(m_strConn);
OleDbCommand dbCmd = new OleDbCommand(strSql);
try
{
dbConn.Open();
dbCmd.ExecuteNonQuery();
return 0;//返回0表示查询成功
}
catch(OleDbException e)
{
throw new Exception(e.Message);
}
finally
{
dbCmd.Dispose();
dbConn.Close();
}
}

//执行SQL语句,判断是否返回数据
public static int ExecuteSqlEx(string strSql)
{
OleDbConnection dbConn = new OleDbConnection(m_strConn);
OleDbCommand dbCmd = new OleDbCommand(strSql);
try
{
dbConn.Open();
OleDbDataReader myReader = dbCmd.ExecuteReader();
if(myReader.Read())
return 0;
else
throw new Exception("数据库里没有要查询的数据");
}
catch(OleDbException e)
{
throw new Exception(e.Message);
}
finally
{
dbCmd.Dispose();
dbConn.Close();
}

}

//执行SQL语句并返回数据集
public static DataSet ExecuteSqlDs(string strSql)
{
OleDbConnection dbConn = new OleDbConnection(m_strConn);
DataSet ds = new DataSet("ds");
try
{
dbConn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(strSql,dbConn);
adapter.Fill(ds);
adapter.Dispose();
return ds;
}
catch(OleDbException e)
{
throw new Exception(e.Message);
}
finally
{

dbConn.Close();
}
}

//执行仅返回一个数字值结果的SQL语句
public static int ExecuteSqlValue(string strSql)
{
OleDbConnection dbConn = new OleDbConnection(m_strConn);
OleDbCommand dbCmd = new OleDbCommand(strSql);
try
{
dbConn.Open();
object r = dbCmd.ExecuteScalar();
if(object.Equals(r,null))
throw new Exception("没有数值");
else
return (int)r;
}
catch(OleDbException e)
{
throw new Exception(e.Message);
}
finally
{
dbCmd.Dispose();
dbConn.Close();
}
}

//执行仅返回一个数据的SQL语句,返回数据可以为任何类型
public static object ExecuteSqlValueEx(string strSql)
{
OleDbConnection dbConn = new OleDbConnection(m_strConn);
OleDbCommand dbCmd = new OleDbCommand(strSql);
try
{
dbConn.Open();
object r = dbCmd.ExecuteScalar();
if(object.Equals(r,null))
throw new Exception("没有对象");
else
return r;
}
catch(OleDbException e)
{
throw new Exception(e.Message);
}
finally
{
dbCmd.Dispose();
dbConn.Close();
}
}

//执行一组SQL语句,不返回数据集
public static int ExecuteSqls(string[] strSqls)
{
OleDbConnection dbConn = new OleDbConnection(m_strConn);
OleDbCommand dbCmd = new OleDbCommand();
//SqlTransaction类,表示要在 SQL Server 数据库中处理的 Transact-SQL 事务。不能继承此类。
//对 SqlTransaction 对象执行与该事务关联的所有后面的操作(例如提交或中止该事务)。
//应用程序通过在 SqlConnection 对象上调用 BeginTransaction 来创建 SqlTransaction 对象。
OleDbTransaction myTrans = dbConn.BeginTransaction();
int j = strSqls.Length;//数组长度

try
{
dbConn.Open();
}
catch(OleDbException e)
{
throw new Exception(e.Message);
}
dbCmd.Connection = dbConn;
dbCmd.Transaction = myTrans;
try
{
foreach(string str in strSqls)
{
dbCmd.CommandText = str;
dbCmd.ExecuteNonQuery();
}
//Commit 方法等效于 Transact-Sql COMMIT TRANSACTION 语句。
//提交事务
myTrans.Commit();
return 0;
}
catch(OleDbException e)
{
//Rollback()方法:从挂起状态回滚事务
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
//SqlTransaction.Dispose();
dbCmd.Dispose();
dbConn.Close();
}
}
}
}

你可能感兴趣的:(sql,qq,SQL Server,J#)