asp.net 數(shù)據庫連接類代碼(SQL)
更新時間:2010年03月03日 14:06:56 作者:
asp.net數(shù)據庫連接類(SQL) 代碼,需要的朋友可以參考下。
復制代碼 代碼如下:
public class SqlOperation
{
#region 屬性
/// <summary>
/// 保存在Web.config中的連接字符串
/// </summary>
protected static string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["hao"].ConnectionString;
/// <summary>
/// SqlConnection對象
/// </summary>
protected static SqlConnection conn = new SqlConnection();
/// <summary>
/// SqlCommand對象
/// </summary>
protected static SqlCommand comm = new SqlCommand();
#endregion
#region 內部函數(shù)
/// <summary>
/// 打開數(shù)據庫連接
/// </summary>
private static void ConnectionOpen()
{
if (conn.State != ConnectionState.Open)
{
conn.Close();
conn.ConnectionString = connectionstring;
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
/// <summary>
/// 關閉數(shù)據庫連接
/// </summary>
private static void ConnectionClose()
{
conn.Close();
conn.Dispose();
comm.Dispose();
}
#endregion
/// <summary>
/// 執(zhí)行SQL語句
/// </summary>
/// <param name="SqlString">要執(zhí)行的SQL語句</param>
public static void ExecuteSQL(string SqlString)
{
try
{
ConnectionOpen();
comm.CommandType = CommandType.Text;
comm.CommandText = SqlString;
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
}
/// <summary>
/// 執(zhí)行存儲過程
/// </summary>
/// <param name="ProcedureName">存儲過程名稱</param>
/// <param name="coll">存儲過程需要的參數(shù)集合</param>
public static void ExecuteProcedure(string ProcedureName, params SqlParameter[] coll)
{
try
{
ConnectionOpen();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = ProcedureName;
comm.Parameters.Clear();
for (int i = 0; i < coll.Length; i++)
{
comm.Parameters.Add(coll[i]);
}
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
}
/// <summary>
/// 執(zhí)行Sql查詢并返回第一行的第一條記錄,返回object,使用時需要拆箱 -> unbox
/// </summary>
/// <param name="sqlstr">傳入的Sql語句</param>
/// <returns>返回object類型的第一行第一條記錄</returns>
public static object ExecuteScalar(string SqlString)
{
object obj = new object();
try
{
ConnectionOpen();
comm.CommandType = CommandType.Text;
comm.CommandText = SqlString;
obj = comm.ExecuteScalar();
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
return obj;
}
/// <summary>
/// 執(zhí)行SQL語句,同時進行事務處理
/// </summary>
/// <param name="sqlstr">要執(zhí)行的SQL語句</param>
public static void ExecuteTransactionSQL(string SqlString)
{
SqlTransaction trans;
trans = conn.BeginTransaction();
comm.Transaction = trans;
try
{
ConnectionOpen();
comm.CommandType = CommandType.Text;
comm.CommandText = SqlString;
comm.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
}
/// <summary>
/// 執(zhí)行指定SQL查詢,返回DataSet
/// </summary>
/// <param name="sqlstr">要執(zhí)行的SQL語句</param>
/// <returns>DataSet</returns>
public static DataSet GetDataSetBySQL(string SqlString)
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
ConnectionOpen();
comm.CommandType = CommandType.Text;
comm.CommandText = SqlString;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
return ds;
}
/// <summary>
/// 通過存儲過程返回DataSet
/// </summary>
/// <param name="ProcedureName">存儲過程名稱</param>
/// <param name="coll">SqlParameter集合</param>
/// <returns>DataSet</returns>
public static DataSet GetDataSetByProcedure(string ProcedureName, params SqlParameter[] coll)
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
ConnectionOpen();
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Clear();
for (int i = 0; i < coll.Length; i++)
{
comm.Parameters.Add(coll[i]);
}
comm.CommandText = ProcedureName;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
return ds;
}
/// <summary>
/// 通過存儲過程返回DataSet
/// </summary>
/// <param name="ProcedureName">存儲過程名稱</param>
/// <returns>DataSet</returns>
public static DataSet GetDataSetByProcedure(string ProcedureName)
{
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
ConnectionOpen();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = ProcedureName;
comm.Parameters.Clear();
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
return ds;
}
/// <summary>
/// 返回指定sql語句的DataTable
/// </summary>
/// <param name="sqlstr">傳入的Sql語句</param>
/// <returns>DataTable</returns>
public static DataTable GetDataTableBySQL(string SqlString)
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
ConnectionOpen();
comm.CommandType = CommandType.Text;
comm.CommandText = SqlString;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
return dt;
}
/// <summary>
/// 根據存儲過程返回DataTable
/// </summary>
/// <param name="ProcedureName">存儲過程名</param>
/// <param name="coll">SqlParameter集合</param>
/// <returns>DataTable</returns>
public static DataTable GetDataTableByProcedure(string ProcedureName, params SqlParameter[] coll)
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
ConnectionOpen();
comm.Parameters.Clear();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = ProcedureName;
for (int i = 0; i < coll.Length; i++)
{
comm.Parameters.Add(coll[i]);
}
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
return dt;
}
/// <summary>
/// 根據存儲過程返回DataTable
/// </summary>
/// <param name="ProcedureName">存儲過程名稱</param>
/// <returns>DataTable</returns>
public static DataTable GetDataTableByProcedure(string ProcedureName)
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
ConnectionOpen();
comm.Parameters.Clear();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = ProcedureName;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
return dt;
}
}
您可能感興趣的文章:
- 一個ASP.NET的MYSQL的數(shù)據庫操作類自己封裝的
- asp.net Oracle數(shù)據庫訪問操作類
- asp.net下使用DbProviderFactories的數(shù)據庫操作類
- ASP.NET對SQLServer的通用數(shù)據庫訪問類
- asp.net下Oracle,SQL Server,Access萬能數(shù)據庫通用類
- asp.net 數(shù)據庫的連接和datatable類
- ASP.NET封裝的SQL數(shù)據庫訪問類
- ASP.NET web.config中數(shù)據庫連接字符串connectionStrings節(jié)的配置方法
- asp.net連接查詢SQL數(shù)據庫并把結果顯示在網頁上(2種方法)
- ASP.NET 6種常用數(shù)據庫的連接方法
- ASP.NET2.0 SQL Server數(shù)據庫連接詳解
- Asp.net把圖片存入數(shù)據庫和讀取圖片的方法
- ASP.NET數(shù)據庫操作類實例
相關文章
.Net?Core跨平臺應用開發(fā)串口篇HelloArm
這篇文章介紹了.Net?Core跨平臺應用開發(fā)串口篇HelloArm,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-01-01ASP.NET Core 1.0實現(xiàn)郵件發(fā)送功能
這篇文章主要為大家詳細介紹了ASP.NET Core 1.0實現(xiàn)郵件發(fā)送功能的相關資料,需要的朋友可以參考下2016-07-07.NET?Core使用Autofac容器的DI依賴注入,IOC控制反轉及AOP切面編程
本文詳細講解了.NET?Core使用Autofac容器的DI依賴注入,IOC控制反轉及AOP切面編程,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02ASP.NET MVC5使用MiniProfiler監(jiān)控MVC性能
這篇文章主要為大家詳細介紹了ASP.NET MVC5使用MiniProfiler監(jiān)控MVC性能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07