- 浏览: 37184 次
- 性别:
- 来自: 苏州
文章分类
最新评论
-
shoushounihao:
弹出增加用户,保存时如何让弹出框架消失,数据也提交对应acti ...
封装JQuery Dialog实现Dialog、Tip、alert和confirm -
爪哇岛岛主:
楼主继续学习啊
Android开发,用ExpandableListView+Dialog 仿Spinner -
sexy22:
感谢分享。
Android开发,用ExpandableListView+Dialog 仿Spinner -
贫僧不吃肉:
楼主辛苦了
Android开发,用ExpandableListView+Dialog 仿Spinner
C# 利用MS的 EntLib的Database类编写的DbHelper,可以自动匹配支持Oracle、SQL Server、MySql
继续回馈ITEYE,绝对原创,好不好各位用了就知道
C# 利用MS的 EntLib的Database类编写的DbHelper,由于MS的EntLib对Oracle、SQL Server和MySql已经封装,所以可以该DbHelper可以适用这三种数据库,根据Web.config中connectionString节点配置数据库连接,会自动根据providerName属性来选择是使用了Oracle数据库还是Sql Server。
需要在Web.config的配置数据库:
<connectionStrings>
<!-- 数据库连接字符串 -->
<add name="ConnectionStringWSC" connectionString="Data Source=(local);Initial Catalog=F.WSC;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
各位使用时需要引用微软的EntLib:
using Microsoft.Practices;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
1. 实现基本的数据操作: 查询、删除、新增
2. 支持事务
3. 支持存储过程使用参数
不废话,看代码:
#region Last Revised: 2009-02-02
/****************************************************************************************************
* Enterprise Library
* Author: Lin Hai
* Class description: Represent the database operation methods
* Creation date: 2008-07-08
====================================================================================================
* Update history:
* 2008-07-18: Update the Constructor function
* 2009-02-02: Add transaction function
*
****************************************************************************************************/
#endregion
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Web;
using Microsoft.Practices;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Text;
namespace WSC.Common
{
/// <summary>
/// 数据库服务基类,支持SQL Server,Oracle和MySql
/// </summary>
public class DbHelper : IDisposable
{
private Database db;
private DbConnection conn;
/// <summary>
///
/// </summary>
public string LastError = "";
#region Constructor and Destructor
/// <summary>
/// 数据库服务基类,支持SQL Server,Oracle和MySql
/// </summary>
public DbHelper()
{
this.LastError = string.Empty;
try
{
db = getDb("");
conn = db.CreateConnection();
conn.Open();
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("DbHelper", "Constructor", ex.StackTrace);
throw new Exception("数据库初始化失败!", ex);
}
}
/// <summary>
/// 数据库服务基类,支持SQL Server,Oracle和MySql。Represent the system database operation class for local caller.
/// <param name="connectionString">Database connection string</param>
/// </summary>
public DbHelper(string connectionString)
{
this.LastError = string.Empty;
try
{
db = getDb(connectionString);
conn = db.CreateConnection();
conn.Open();
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("DbHelper", "Constructor", ex.StackTrace);
throw new Exception("数据库初始化失败!", ex);
}
}
private Database getDb(string connectionStringKey)
{
if (String.IsNullOrWhiteSpace(connectionStringKey))
connectionStringKey = Global.ConnectionStringKey;
//return DatabaseFactory.CreateDatabase(connectionString);
return EnterpriseLibraryContainer.Current.GetInstance<Database>(connectionStringKey);
}
/// <summary>
///
/// </summary>
~DbHelper()
{
this.Dispose();
}
#region IDisposable Members
private bool disposed = false;
/// <summary>
/// GC
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this.disposed && disposing)
{
try
{
if ((conn != null) && (conn.State != ConnectionState.Closed))
{
conn.Close();
conn.Dispose();
}
if (_transaction != null) _transaction.Dispose();
_transaction = null;
conn = null;
}
catch { }
}
disposed = true;
}
#endregion
#endregion
/// <summary>
/// Current database
/// </summary>
public Database GetDatabase
{
get { return db; }
}
#region Execute SQL
/// <summary>
/// 执行StoreProcedure或Select、Delete、Update等
/// </summary>
/// <param name="SQL">要执行的SQL语句</param>
/// <returns>SUCCESS|Error message</returns>
public string Execute(string SQL)
{
try
{
if (String.IsNullOrWhiteSpace(SQL))
return "没有可执行的SQL语句";
this.LastError = string.Empty;
using (DbCommand cmd = db.GetSqlStringCommand(SQL))
{
cmd.Connection = conn;
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
int ret = cmd.ExecuteNonQuery();
WriteLog("Execute", SQL, "成功, 影响记录数:" + ret.ToString() + "条");
}
return Global.SUCCESS;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
return "执行失败, -2! " + ex.Message;
}
}
/// <summary>
/// 执行多SQL语句,StoreProcedure或Select、Delete、Update等
/// </summary>
/// <param name="SQL">要执行的SQL语句集合</param>
/// <returns>true|false</returns>
public bool Execute(List<string> SQL)
{
if (SQL == null || SQL.Count == 0)
return false;
StringBuilder sb = new StringBuilder();
SQL.ForEach(x => { sb.AppendLine(x); });
try
{
this.LastError = string.Empty;
using (DbCommand cmd = conn.CreateCommand())
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
foreach (string sql in SQL)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
WriteLog("Execute(List[SQL])", sb.ToString(), Global.SUCCESS);
return true;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", sb.ToString(), ex.Message + "\\r\\n" + ex.StackTrace);
return false;
}
}
/// <summary>
/// 执行多SQL语句,StoreProcedure或Select、Delete、Update等,有事务支持。
/// </summary>
/// <param name="SQL">要执行的SQL语句集合</param>
/// <returns>true|false</returns>
public bool ExecuteWithTransaction(List<string> SQL)
{
if (SQL == null || SQL.Count == 0)
return false;
StringBuilder sb = new StringBuilder();
SQL.ForEach(x => { sb.AppendLine(x); });
DbTransaction transaction = conn.BeginTransaction();
try
{
this.LastError = string.Empty;
using (DbCommand cmd = conn.CreateCommand())
{
cmd.Connection = conn;
if (transaction != null && transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = transaction;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
foreach (string sql in SQL)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
transaction.Commit();
WriteLog("ExecuteWithTransaction(List[SQL])", sb.ToString(), Global.SUCCESS);
return true;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", sb.ToString(), ex.Message + "\\r\\n" + ex.StackTrace);
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
WriteErrLog("ExecuteRollback", string.Format("Rollback Exception Type: {0}", ex2.GetType()), string.Format("Message: {0}", ex2.Message));
this.LastError += ex2.Message;
}
return false;
}
}
#endregion
#region Execute StoreProcedure
/// <summary>
/// 执行StoreProcedure
/// </summary>
/// <param name="spName">要执行的SQL语句</param>
/// <param name="outParams">返回值</param>
/// <returns>SUCCESS|Error message</returns>
public string ExecuteStoreProcedure(string spName, out List<NameValuePair> outParams)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(spName))
{
outParams = null;
this.LastError = "没有可执行的存储过程";
return this.LastError;
}
using (DbCommand cmd = db.GetStoredProcCommand(spName))
{
cmd.Connection = conn;
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
int ret = cmd.ExecuteNonQuery();
outParams = new List<NameValuePair>();
foreach (DbParameter param in cmd.Parameters)
{
if (param.Direction == ParameterDirection.Output)
{
outParams.Add(new NameValuePair(param.ParameterName, param.Value.ToString()));
}
}
WriteLog("Execute", spName, "成功, 影响记录数:" + ret.ToString() + "条, 返回参数: " + UtilityHelper.ConvertListToString(outParams));
}
return Global.SUCCESS;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", spName, ex.Message + "\\r\\n" + ex.StackTrace);
outParams = null;
return "执行失败, -2! " + "\r\n" + ex.Message;
}
}
/// <summary>
/// 执行StoreProcedure
/// </summary>
/// <param name="spName">要执行的SQL语句</param>
/// <param name="ds">执行StoreProcedure获取的数据集</param>
/// <param name="outParams">返回值</param>
/// <returns>SUCCESS|Error message</returns>
public string ExecuteStoreProcedure(string spName, out DataSet ds, out List<NameValuePair> outParams)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(spName))
{
outParams = null;
ds = null;
this.LastError = "没有可执行的存储过程";
return this.LastError;
}
using (DbCommand cmd = db.GetStoredProcCommand(spName))
{
cmd.Connection = conn;
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
ds = new DataSet();
db.LoadDataSet(cmd, ds, "OutParams");
outParams = new List<NameValuePair>();
foreach (DbParameter param in cmd.Parameters)
{
if (param.Direction == ParameterDirection.Output)
{
outParams.Add(new NameValuePair(param.ParameterName, param.Value.ToString()));
}
}
WriteLog("Execute", spName, "成功, 返回参数:" + UtilityHelper.ConvertListToString(outParams));
return Global.SUCCESS;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", spName, ex.Message + "\\r\\n" + ex.StackTrace);
outParams = null;
ds = null;
return ex.Message;
}
}
#endregion
#region Query
/// <summary>
/// 执行StoreProcedure或Select,返回查询结果
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <returns>返回一查询结果DataReader</returns>
public DbDataReader Reader(string SQL)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return null;
}
using (DbCommand cmd = db.GetSqlStringCommand(SQL))
{
cmd.Connection = conn;
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
this.InjectParameters(cmd);
DbDataReader dr = cmd.ExecuteReader();
//WriteLog("Query", SQL, GlobalDefinition.SUCCESS);
return dr;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Reader", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
//return null;
throw new Exception("SQL.Reader: " + ex.Message);
}
}
/// <summary>
/// 执行StoreProcedure或Select,返回查询结果
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <returns>返回一查询结果DataSet</returns>
public DataSet Query(string SQL)
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return null;
}
try
{
using (DbCommand cmd = db.GetSqlStringCommand(SQL))
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
this.InjectParameters(cmd);
DataSet ds = new DataSet();
db.LoadDataSet(cmd, ds, "DefaultTableName");
//WriteLog("Query", SQL, GlobalDefinition.SUCCESS);
return ds;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Query", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.Query: " + ex.Message, ex);
}
}
/// <summary>
/// 执行StoreProcedure或Select,返回查询结果
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <returns>返回一查询结果DataSet</returns>
public DataTable QueryTable(string SQL)
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return null;
}
try
{
using (DbCommand cmd = db.GetSqlStringCommand(SQL))
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
this.InjectParameters(cmd);
DataSet ds = new DataSet();
db.LoadDataSet(cmd, ds, "DefaultTableName");
//WriteLog("Query", SQL, GlobalDefinition.SUCCESS);
if (ds != null && ds.Tables.Count > 0)
return ds.Tables[0];
return null;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Query", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.QueryTable: " + ex.Message, ex);
}
}
#endregion
#region Get value
/// <summary>
/// 根据列序号返回SQL语句第一行,第n列,zero based.
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <param name="ColumnIndex">列序号</param>
/// <returns>字符串: 第一行,第n列</returns>
public string GetValue(string SQL, int ColumnIndex)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return String.Empty;
}
using (DbDataReader dr = this.Reader(SQL))
{
if (dr.Read())
{
return dr[ColumnIndex].ToString().Trim();
}
return string.Empty;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("GetValue-ColIndex", SQL + "\r\nColIndex=" + ColumnIndex.ToString(), ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.GetValue: " + ex.Message, ex);
}
}
/// <summary>
/// 根据列名返回SQL语句第一行,第n列
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <param name="ColumnName">列名</param>
/// <returns>字符串: 第一行,第n列</returns>
public string GetValue(string SQL, string ColumnName)
{
try
{
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return String.Empty;
}
this.LastError = string.Empty;
using (DbDataReader dr = this.Reader(SQL))
{
if (dr.Read())
{
return dr[ColumnName].ToString().Trim();
}
return string.Empty;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("GetValue-ColName", SQL + "\r\nColName=" + ColumnName, ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.GetValue: " + ex.Message, ex);
}
}
/// <summary>
/// 是否有记录
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <returns></returns>
public bool HasRows(string SQL)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return false;
}
using (DbDataReader dr = this.Reader(SQL))
{
if (dr.Read())
{
return true;
}
return false;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("HasRows", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.HasRows: " + ex.Message, ex);
}
}
#endregion
#region Transaction
private DbTransaction _transaction;
/// <summary>
/// 开始事务
/// </summary>
public bool BeginTransaction()
{
if (db != null && conn != null && conn.State == ConnectionState.Open)
{
try
{
_transaction = conn.BeginTransaction();
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Transaction", "BeginTransaction", ex.Message + "\\r\\n" + ex.StackTrace);
return false;
}
}
else
{
this.LastError = "You must have a valid connection object before calling BeginTransaction.";
WriteErrLog("Transaction", "BeginTransaction", this.LastError);
return false;
}
return true;
}
/// <summary>
/// 提交事务
/// </summary>
public bool CommitTransaction()
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
{
try
{
_transaction.Commit();
_transaction = null;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Transaction", "CommitTransaction", ex.Message + "\\r\\n" + ex.StackTrace);
return false;
}
}
else
{
this.LastError = "You must call BeginTransaction before calling CommitTransaction.";
WriteErrLog("Transaction", "CommitTransaction", this.LastError);
return false;
}
return true;
}
/// <summary>
/// 回滚事务
/// </summary>
public bool RollbackTransaction()
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
{
try
{
_transaction.Rollback();
_transaction = null;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Transaction", "RollbackTransaction", ex.Message + "\\r\\n" + ex.StackTrace);
return false;
}
}
else
{
this.LastError = "You must call BeginTransaction before calling RollbackTransaction.";
WriteErrLog("Transaction", "RollbackTransaction", this.LastError);
return false;
}
return true;
}
#endregion
#region Parameters
private List<DbParameter> _parameters = new List<DbParameter>();
/// <summary>
/// 参数集
/// </summary>
public List<DbParameter> Parameters
{
get
{
return _parameters;
}
}
#region Add Parameter
/// <summary>
/// 新增参数
/// </summary>
/// <param name="parameter">SQL参数</param>
protected void AddParameter(DbParameter parameter)
{
_parameters.Add(parameter);
}
/// <summary>
/// 新增参数
/// </summary>
/// <param name="name">参数名</param>
/// <param name="type">数据类型</param>
/// <param name="value">值</param>
/// <returns></returns>
public DbParameter AddInParameter(string name, DbType type, object value)
{
return AddInParameter(name, type, value, 500);
}
/// <summary>
/// 新增参数
/// </summary>
/// <param name="name">参数名</param>
/// <param name="type">数据类型</param>
/// <param name="value">值</param>
/// <param name="size">长度</param>
/// <returns></returns>
public DbParameter AddInParameter(string name, DbType type, object value, int size)
{
if (value == null || String.IsNullOrWhiteSpace(name))
throw new Exception("The parameter name or value is empty.");
DbParameter prm = conn.CreateCommand().CreateParameter();
prm.Direction = ParameterDirection.Input;
prm.ParameterName = name;
prm.DbType = type;
prm.Size = size;
prm.Value = value;
_parameters.Add(prm);
return prm;
}
/// <summary>
/// 新增参数
/// </summary>
/// <param name="name">参数名</param>
/// <param name="type">数据类型</param>
/// <returns></returns>
public DbParameter AddOutParameter(string name, DbType type)
{
return AddOutParameter(name, type, 500);
}
/// <summary>
/// 新增参数
/// </summary>
/// <param name="name">参数名</param>
/// <param name="type">数据类型</param>
/// <param name="size">长度</param>
/// <returns></returns>
public DbParameter AddOutParameter(string name, DbType type, int size)
{
if (String.IsNullOrWhiteSpace(name))
throw new Exception("The parameter name is empty.");
DbParameter prm = conn.CreateCommand().CreateParameter();
prm.Direction = ParameterDirection.Output;
prm.ParameterName = name;
prm.DbType = type;
prm.Size = size;
_parameters.Add(prm);
return prm;
}
#endregion
/// <summary>
/// 清空参数
/// </summary>
public void ClearParameters()
{
_parameters.Clear();
}
/// <summary>
/// 为DbCommand对象填充参数
/// </summary>
/// <param name="command">DbCommand对象</param>
private void InjectParameters(DbCommand command)
{
command.Parameters.Clear();
if (_parameters == null) return;
foreach (DbParameter para in _parameters)
{
if (para != null)
command.Parameters.Add(para);
}
this.ClearParameters(); //注入后清空参数
}
#endregion
#region Write log
/// <summary>
///
/// </summary>
/// <param name="Function"></param>
/// <param name="SQL"></param>
/// <param name="Message"></param>
private void WriteErrLog(string Function, string SQL, string Message)
{
}
/// <summary>
///
/// </summary>
/// <param name="Function"></param>
/// <param name="SQL"></param>
/// <param name="Message"></param>
private void WriteLog(string Function, string SQL, string Message)
{
}
#endregion
}
}
C# 利用MS的 EntLib的Database类编写的DbHelper,由于MS的EntLib对Oracle、SQL Server和MySql已经封装,所以可以该DbHelper可以适用这三种数据库,根据Web.config中connectionString节点配置数据库连接,会自动根据providerName属性来选择是使用了Oracle数据库还是Sql Server。
需要在Web.config的配置数据库:
<connectionStrings>
<!-- 数据库连接字符串 -->
<add name="ConnectionStringWSC" connectionString="Data Source=(local);Initial Catalog=F.WSC;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
</connectionStrings>
各位使用时需要引用微软的EntLib:
using Microsoft.Practices;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
1. 实现基本的数据操作: 查询、删除、新增
2. 支持事务
3. 支持存储过程使用参数
不废话,看代码:
#region Last Revised: 2009-02-02
/****************************************************************************************************
* Enterprise Library
* Author: Lin Hai
* Class description: Represent the database operation methods
* Creation date: 2008-07-08
====================================================================================================
* Update history:
* 2008-07-18: Update the Constructor function
* 2009-02-02: Add transaction function
*
****************************************************************************************************/
#endregion
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Web;
using Microsoft.Practices;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Text;
namespace WSC.Common
{
/// <summary>
/// 数据库服务基类,支持SQL Server,Oracle和MySql
/// </summary>
public class DbHelper : IDisposable
{
private Database db;
private DbConnection conn;
/// <summary>
///
/// </summary>
public string LastError = "";
#region Constructor and Destructor
/// <summary>
/// 数据库服务基类,支持SQL Server,Oracle和MySql
/// </summary>
public DbHelper()
{
this.LastError = string.Empty;
try
{
db = getDb("");
conn = db.CreateConnection();
conn.Open();
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("DbHelper", "Constructor", ex.StackTrace);
throw new Exception("数据库初始化失败!", ex);
}
}
/// <summary>
/// 数据库服务基类,支持SQL Server,Oracle和MySql。Represent the system database operation class for local caller.
/// <param name="connectionString">Database connection string</param>
/// </summary>
public DbHelper(string connectionString)
{
this.LastError = string.Empty;
try
{
db = getDb(connectionString);
conn = db.CreateConnection();
conn.Open();
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("DbHelper", "Constructor", ex.StackTrace);
throw new Exception("数据库初始化失败!", ex);
}
}
private Database getDb(string connectionStringKey)
{
if (String.IsNullOrWhiteSpace(connectionStringKey))
connectionStringKey = Global.ConnectionStringKey;
//return DatabaseFactory.CreateDatabase(connectionString);
return EnterpriseLibraryContainer.Current.GetInstance<Database>(connectionStringKey);
}
/// <summary>
///
/// </summary>
~DbHelper()
{
this.Dispose();
}
#region IDisposable Members
private bool disposed = false;
/// <summary>
/// GC
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this.disposed && disposing)
{
try
{
if ((conn != null) && (conn.State != ConnectionState.Closed))
{
conn.Close();
conn.Dispose();
}
if (_transaction != null) _transaction.Dispose();
_transaction = null;
conn = null;
}
catch { }
}
disposed = true;
}
#endregion
#endregion
/// <summary>
/// Current database
/// </summary>
public Database GetDatabase
{
get { return db; }
}
#region Execute SQL
/// <summary>
/// 执行StoreProcedure或Select、Delete、Update等
/// </summary>
/// <param name="SQL">要执行的SQL语句</param>
/// <returns>SUCCESS|Error message</returns>
public string Execute(string SQL)
{
try
{
if (String.IsNullOrWhiteSpace(SQL))
return "没有可执行的SQL语句";
this.LastError = string.Empty;
using (DbCommand cmd = db.GetSqlStringCommand(SQL))
{
cmd.Connection = conn;
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
int ret = cmd.ExecuteNonQuery();
WriteLog("Execute", SQL, "成功, 影响记录数:" + ret.ToString() + "条");
}
return Global.SUCCESS;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
return "执行失败, -2! " + ex.Message;
}
}
/// <summary>
/// 执行多SQL语句,StoreProcedure或Select、Delete、Update等
/// </summary>
/// <param name="SQL">要执行的SQL语句集合</param>
/// <returns>true|false</returns>
public bool Execute(List<string> SQL)
{
if (SQL == null || SQL.Count == 0)
return false;
StringBuilder sb = new StringBuilder();
SQL.ForEach(x => { sb.AppendLine(x); });
try
{
this.LastError = string.Empty;
using (DbCommand cmd = conn.CreateCommand())
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
foreach (string sql in SQL)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
}
WriteLog("Execute(List[SQL])", sb.ToString(), Global.SUCCESS);
return true;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", sb.ToString(), ex.Message + "\\r\\n" + ex.StackTrace);
return false;
}
}
/// <summary>
/// 执行多SQL语句,StoreProcedure或Select、Delete、Update等,有事务支持。
/// </summary>
/// <param name="SQL">要执行的SQL语句集合</param>
/// <returns>true|false</returns>
public bool ExecuteWithTransaction(List<string> SQL)
{
if (SQL == null || SQL.Count == 0)
return false;
StringBuilder sb = new StringBuilder();
SQL.ForEach(x => { sb.AppendLine(x); });
DbTransaction transaction = conn.BeginTransaction();
try
{
this.LastError = string.Empty;
using (DbCommand cmd = conn.CreateCommand())
{
cmd.Connection = conn;
if (transaction != null && transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = transaction;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
foreach (string sql in SQL)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
transaction.Commit();
WriteLog("ExecuteWithTransaction(List[SQL])", sb.ToString(), Global.SUCCESS);
return true;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", sb.ToString(), ex.Message + "\\r\\n" + ex.StackTrace);
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
WriteErrLog("ExecuteRollback", string.Format("Rollback Exception Type: {0}", ex2.GetType()), string.Format("Message: {0}", ex2.Message));
this.LastError += ex2.Message;
}
return false;
}
}
#endregion
#region Execute StoreProcedure
/// <summary>
/// 执行StoreProcedure
/// </summary>
/// <param name="spName">要执行的SQL语句</param>
/// <param name="outParams">返回值</param>
/// <returns>SUCCESS|Error message</returns>
public string ExecuteStoreProcedure(string spName, out List<NameValuePair> outParams)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(spName))
{
outParams = null;
this.LastError = "没有可执行的存储过程";
return this.LastError;
}
using (DbCommand cmd = db.GetStoredProcCommand(spName))
{
cmd.Connection = conn;
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
int ret = cmd.ExecuteNonQuery();
outParams = new List<NameValuePair>();
foreach (DbParameter param in cmd.Parameters)
{
if (param.Direction == ParameterDirection.Output)
{
outParams.Add(new NameValuePair(param.ParameterName, param.Value.ToString()));
}
}
WriteLog("Execute", spName, "成功, 影响记录数:" + ret.ToString() + "条, 返回参数: " + UtilityHelper.ConvertListToString(outParams));
}
return Global.SUCCESS;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", spName, ex.Message + "\\r\\n" + ex.StackTrace);
outParams = null;
return "执行失败, -2! " + "\r\n" + ex.Message;
}
}
/// <summary>
/// 执行StoreProcedure
/// </summary>
/// <param name="spName">要执行的SQL语句</param>
/// <param name="ds">执行StoreProcedure获取的数据集</param>
/// <param name="outParams">返回值</param>
/// <returns>SUCCESS|Error message</returns>
public string ExecuteStoreProcedure(string spName, out DataSet ds, out List<NameValuePair> outParams)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(spName))
{
outParams = null;
ds = null;
this.LastError = "没有可执行的存储过程";
return this.LastError;
}
using (DbCommand cmd = db.GetStoredProcCommand(spName))
{
cmd.Connection = conn;
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600; //10 minutes
this.InjectParameters(cmd);
ds = new DataSet();
db.LoadDataSet(cmd, ds, "OutParams");
outParams = new List<NameValuePair>();
foreach (DbParameter param in cmd.Parameters)
{
if (param.Direction == ParameterDirection.Output)
{
outParams.Add(new NameValuePair(param.ParameterName, param.Value.ToString()));
}
}
WriteLog("Execute", spName, "成功, 返回参数:" + UtilityHelper.ConvertListToString(outParams));
return Global.SUCCESS;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Execute", spName, ex.Message + "\\r\\n" + ex.StackTrace);
outParams = null;
ds = null;
return ex.Message;
}
}
#endregion
#region Query
/// <summary>
/// 执行StoreProcedure或Select,返回查询结果
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <returns>返回一查询结果DataReader</returns>
public DbDataReader Reader(string SQL)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return null;
}
using (DbCommand cmd = db.GetSqlStringCommand(SQL))
{
cmd.Connection = conn;
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
this.InjectParameters(cmd);
DbDataReader dr = cmd.ExecuteReader();
//WriteLog("Query", SQL, GlobalDefinition.SUCCESS);
return dr;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Reader", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
//return null;
throw new Exception("SQL.Reader: " + ex.Message);
}
}
/// <summary>
/// 执行StoreProcedure或Select,返回查询结果
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <returns>返回一查询结果DataSet</returns>
public DataSet Query(string SQL)
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return null;
}
try
{
using (DbCommand cmd = db.GetSqlStringCommand(SQL))
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
this.InjectParameters(cmd);
DataSet ds = new DataSet();
db.LoadDataSet(cmd, ds, "DefaultTableName");
//WriteLog("Query", SQL, GlobalDefinition.SUCCESS);
return ds;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Query", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.Query: " + ex.Message, ex);
}
}
/// <summary>
/// 执行StoreProcedure或Select,返回查询结果
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <returns>返回一查询结果DataSet</returns>
public DataTable QueryTable(string SQL)
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return null;
}
try
{
using (DbCommand cmd = db.GetSqlStringCommand(SQL))
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
cmd.Transaction = _transaction;
this.InjectParameters(cmd);
DataSet ds = new DataSet();
db.LoadDataSet(cmd, ds, "DefaultTableName");
//WriteLog("Query", SQL, GlobalDefinition.SUCCESS);
if (ds != null && ds.Tables.Count > 0)
return ds.Tables[0];
return null;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Query", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.QueryTable: " + ex.Message, ex);
}
}
#endregion
#region Get value
/// <summary>
/// 根据列序号返回SQL语句第一行,第n列,zero based.
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <param name="ColumnIndex">列序号</param>
/// <returns>字符串: 第一行,第n列</returns>
public string GetValue(string SQL, int ColumnIndex)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return String.Empty;
}
using (DbDataReader dr = this.Reader(SQL))
{
if (dr.Read())
{
return dr[ColumnIndex].ToString().Trim();
}
return string.Empty;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("GetValue-ColIndex", SQL + "\r\nColIndex=" + ColumnIndex.ToString(), ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.GetValue: " + ex.Message, ex);
}
}
/// <summary>
/// 根据列名返回SQL语句第一行,第n列
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <param name="ColumnName">列名</param>
/// <returns>字符串: 第一行,第n列</returns>
public string GetValue(string SQL, string ColumnName)
{
try
{
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return String.Empty;
}
this.LastError = string.Empty;
using (DbDataReader dr = this.Reader(SQL))
{
if (dr.Read())
{
return dr[ColumnName].ToString().Trim();
}
return string.Empty;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("GetValue-ColName", SQL + "\r\nColName=" + ColumnName, ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.GetValue: " + ex.Message, ex);
}
}
/// <summary>
/// 是否有记录
/// </summary>
/// <param name="SQL">查询的SQL语句</param>
/// <returns></returns>
public bool HasRows(string SQL)
{
try
{
this.LastError = string.Empty;
if (String.IsNullOrWhiteSpace(SQL))
{
this.LastError = "没有可执行的SQL语句";
return false;
}
using (DbDataReader dr = this.Reader(SQL))
{
if (dr.Read())
{
return true;
}
return false;
}
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("HasRows", SQL, ex.Message + "\\r\\n" + ex.StackTrace);
throw new Exception("SQL.HasRows: " + ex.Message, ex);
}
}
#endregion
#region Transaction
private DbTransaction _transaction;
/// <summary>
/// 开始事务
/// </summary>
public bool BeginTransaction()
{
if (db != null && conn != null && conn.State == ConnectionState.Open)
{
try
{
_transaction = conn.BeginTransaction();
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Transaction", "BeginTransaction", ex.Message + "\\r\\n" + ex.StackTrace);
return false;
}
}
else
{
this.LastError = "You must have a valid connection object before calling BeginTransaction.";
WriteErrLog("Transaction", "BeginTransaction", this.LastError);
return false;
}
return true;
}
/// <summary>
/// 提交事务
/// </summary>
public bool CommitTransaction()
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
{
try
{
_transaction.Commit();
_transaction = null;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Transaction", "CommitTransaction", ex.Message + "\\r\\n" + ex.StackTrace);
return false;
}
}
else
{
this.LastError = "You must call BeginTransaction before calling CommitTransaction.";
WriteErrLog("Transaction", "CommitTransaction", this.LastError);
return false;
}
return true;
}
/// <summary>
/// 回滚事务
/// </summary>
public bool RollbackTransaction()
{
if (_transaction != null && _transaction.Connection.State == ConnectionState.Open)
{
try
{
_transaction.Rollback();
_transaction = null;
}
catch (Exception ex)
{
this.LastError = ex.Message;
WriteErrLog("Transaction", "RollbackTransaction", ex.Message + "\\r\\n" + ex.StackTrace);
return false;
}
}
else
{
this.LastError = "You must call BeginTransaction before calling RollbackTransaction.";
WriteErrLog("Transaction", "RollbackTransaction", this.LastError);
return false;
}
return true;
}
#endregion
#region Parameters
private List<DbParameter> _parameters = new List<DbParameter>();
/// <summary>
/// 参数集
/// </summary>
public List<DbParameter> Parameters
{
get
{
return _parameters;
}
}
#region Add Parameter
/// <summary>
/// 新增参数
/// </summary>
/// <param name="parameter">SQL参数</param>
protected void AddParameter(DbParameter parameter)
{
_parameters.Add(parameter);
}
/// <summary>
/// 新增参数
/// </summary>
/// <param name="name">参数名</param>
/// <param name="type">数据类型</param>
/// <param name="value">值</param>
/// <returns></returns>
public DbParameter AddInParameter(string name, DbType type, object value)
{
return AddInParameter(name, type, value, 500);
}
/// <summary>
/// 新增参数
/// </summary>
/// <param name="name">参数名</param>
/// <param name="type">数据类型</param>
/// <param name="value">值</param>
/// <param name="size">长度</param>
/// <returns></returns>
public DbParameter AddInParameter(string name, DbType type, object value, int size)
{
if (value == null || String.IsNullOrWhiteSpace(name))
throw new Exception("The parameter name or value is empty.");
DbParameter prm = conn.CreateCommand().CreateParameter();
prm.Direction = ParameterDirection.Input;
prm.ParameterName = name;
prm.DbType = type;
prm.Size = size;
prm.Value = value;
_parameters.Add(prm);
return prm;
}
/// <summary>
/// 新增参数
/// </summary>
/// <param name="name">参数名</param>
/// <param name="type">数据类型</param>
/// <returns></returns>
public DbParameter AddOutParameter(string name, DbType type)
{
return AddOutParameter(name, type, 500);
}
/// <summary>
/// 新增参数
/// </summary>
/// <param name="name">参数名</param>
/// <param name="type">数据类型</param>
/// <param name="size">长度</param>
/// <returns></returns>
public DbParameter AddOutParameter(string name, DbType type, int size)
{
if (String.IsNullOrWhiteSpace(name))
throw new Exception("The parameter name is empty.");
DbParameter prm = conn.CreateCommand().CreateParameter();
prm.Direction = ParameterDirection.Output;
prm.ParameterName = name;
prm.DbType = type;
prm.Size = size;
_parameters.Add(prm);
return prm;
}
#endregion
/// <summary>
/// 清空参数
/// </summary>
public void ClearParameters()
{
_parameters.Clear();
}
/// <summary>
/// 为DbCommand对象填充参数
/// </summary>
/// <param name="command">DbCommand对象</param>
private void InjectParameters(DbCommand command)
{
command.Parameters.Clear();
if (_parameters == null) return;
foreach (DbParameter para in _parameters)
{
if (para != null)
command.Parameters.Add(para);
}
this.ClearParameters(); //注入后清空参数
}
#endregion
#region Write log
/// <summary>
///
/// </summary>
/// <param name="Function"></param>
/// <param name="SQL"></param>
/// <param name="Message"></param>
private void WriteErrLog(string Function, string SQL, string Message)
{
}
/// <summary>
///
/// </summary>
/// <param name="Function"></param>
/// <param name="SQL"></param>
/// <param name="Message"></param>
private void WriteLog(string Function, string SQL, string Message)
{
}
#endregion
}
}
相关推荐
综合以上信息,我们可以得出这个压缩包包含的可能是一个使用ASP.NET 3.5和SQL Server 2005技术构建的论坛系统,该系统利用了Enterprise Library 3.5来提高开发效率和系统的可维护性。开发者已经对其进行过实验,证明...
下面是一份关于 SQL Server Service Broker 基础知识介绍的PPT 文档,可以下载。 演讲者:EntLib.com MSN & Email:entlib@hotmail.com 如有任何技术问题或者培训需求,欢迎和我们联系或者留言。
Enterprise Library(EntLib)是微软开发的一套面向.NET Framework的应用程序开发框架,旨在提供一套可重用的、灵活的和可扩展的库,...通过学习,开发者可以更有效地利用EntLib来提高.NET应用程序的质量和可维护性。
【描述】:“数据库在文件里”暗示我们关注的是Oracle数据库的本地安装或文件系统中的数据文件,而“他跟sqlserver还是不怎么一样”则强调Oracle数据库与SQL Server在数据访问层的实现上存在差异。这可能涉及SQL语法...
【Entlib电子商务网站sql脚本】是用于构建和初始化一个基于Entlib框架的电子商务网站数据库的SQL脚本。这个脚本通常包含了创建表、视图、存储过程、触发器等数据库对象的语句,用于搭建支持电子商务功能的数据库结构...
在微软企业库5.0的Data Access模块中,主要的功能是提供了一个抽象层,封装了对数据库的访问,使得开发者无需直接编写SQL语句,而是通过配置文件就能切换不同的数据库系统,如MS SQL Server、SQLite等。这极大地提高...
通过实践,你可以更深入地理解如何利用Validation模块来提升应用程序的质量和安全性。 总之,服务器端验证是确保数据完整性和安全性的重要环节,微软企业库5.0的Validation模块提供了强大且灵活的工具来实现这一...
Service Broker 是 Microsoft SQL Server 2005/2008/2012 中的新技术,它可帮助数据库开发人员生成安全、可靠且可伸缩的应用程序。本文档由 EntLib.com team 编写。
Database 访问类库,用于访问SQLServer oracle 等
"EntLib.com论坛"是一个基于C# .NET技术构建的开源论坛系统,它提供了一个用于社区交流、讨论和分享IT知识的平台。这个系统可能是由EntLib[1].com开发的,版本为3.5,可能包含了一系列的功能改进和优化。源码的开放...
这两者的结合使得开发者可以利用.NET框架的强大工具和库,同时享受到SQL Server的高级数据库功能。 论坛系统的关键特性可能包括: 1. 用户注册与登录:用户可以创建账户,安全地保存个人信息,并通过电子邮件验证...
基于Subtext v2.0 开源ASP.NET博客系统的中文汉化及定制化开发,EntLib.com 开源博客小组根据用户的反馈意见,不断改进系统。2008年12月19日正式发布 EntLib.com Blog 开源博客平台 v3.0,已经在...
... EntLib.com 电子商务系统运行环境: (1)操作系统支持 Windows XP,Windows Vista,Windows 7,Windows Server 2003,Windows ...(3)数据库采用微软SQL Server 2005 / 2008 或SQL Server Express 免费版本。
EntLib.com Blog 可能使用了ADO.NET或Entity Framework进行数据库操作,提供了对多种数据库(如SQL Server、MySQL等)的支持。 3. **内容管理系统(CMS)**:作为一个博客平台,EntLib.com Blog 必须有一个强大的...
EntLib.Validation模块内置了一系列丰富的验证器,大致可以分为以下几类: 1. **可作为特性的验证器**:这类验证器既可以通过特性应用到代码中,也可以通过配置文件进行定义。主要包括: - `...
1、SQL助手:管理数据库表,查看结构、生成常用SQL语句和存储过程、导出表结构文档。新特性:支持SQL自动完成 2、C#代码生成器:系统自带...支持sqlserver/access /oracle/mysql 官方网页:http://dbsharp.qingweb.com
DBRulesAuthorizationProviderNode可能涉及到配置UI的节点类,DbRulesManager负责管理授权规则,DBRulesAuthorizationConfigurationDesignManager是设计时的支持类,而DBRulesAuthorizationProvider是实际的提供程序...
这个论坛可能是讨论、问答和技术支持的平台,用户可以在其中交流关于EntLib v3.5的使用经验、问题和解决方案。 Enterprise Library 3.5 是该框架的一个较早版本,它包含了一系列可重用的、配置驱动的库,用于实现如...
基于.NET FRAMEWORK 2.0 / C# / SQL Server 平台。 <br>本次发布更新的内容: 1.修改Theme设置,是UI更加符合国人的审美眼光。 2.修改ActiveUser的统计运算方法。 3.POINTS 汉化为发帖积分。 4.删除...
SQL Server提供了稳定可靠的数据库服务,并支持高级特性如事务处理和复杂查询。 - **IIS (Internet Information Services)**:作为Web服务器,负责处理HTTP请求并返回响应。 #### SQL、索引、数据库设计与死锁处理 ...