国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET 數據訪問類

2024-07-10 12:40:52
字體:
來源:轉載
供稿:網友
using System;
using System.Data;
using System.Data.SqlClient; 

namespace SysClassLibrary
{
/// <summary>
/// DataAccess 的摘要說明。
/// <description>數據處理基類,調用方式:DataAccess.DataSet((string)sqlstr);或者DataAccess.DataSet((string)sqlstr,ref DataSet ds); </description>
/// </summary>
public class DataAccess
{
#region 屬性
protected static SqlConnection conn=new SqlConnection();
protected static SqlCommand comm=new SqlCommand();
#endregion
public DataAccess()
{
//init();
}
#region 內部函數 靜態方法中不會執行DataAccess()構造函數

/// <summary>
/// 打開數據庫連接
/// </summary>
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
//SysConfig.ConnectionString 為系統配置類中連接字符串,如:"server=localhost;database=databasename;uid=sa;pwd=;"

conn.ConnectionString = SysConfig.ConnectionString ;
comm.Connection =conn;
try
{
conn.Open();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
}
}
/// <summary>
/// 關閉當前數據庫連接
/// </summary>
private static void closeConnection()
{
if(conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
comm.Dispose();
}
#endregion
/// <summary>
/// 執行Sql查詢語句
/// </summary>
/// <param name="sqlstr">傳入的Sql語句</param>
public static void ExecuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType =CommandType.Text ;
comm.CommandText =sqlstr;
comm.ExecuteNonQuery(); 
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}

/// <summary>
/// 執行存儲過程
/// </summary>
/// <param name="procName">存儲過程名</param>
/// <param name="coll">SqlParameters 集合</param>
public static void ExecutePorcedure(string procName,SqlParameter[] coll)
{
try
{
openConnection();
for(int i=0;i<coll.Length;i++)
{
comm.Parameters .Add(coll);
}
comm.CommandType=CommandType.StoredProcedure ;
comm.CommandText =procName;
comm.ExecuteNonQuery();
}
catch(Exception e)
{
throw new Exception(e.Message);
}
finally
{
comm.Parameters.Clear();
closeConnection();
}
}

/// <summary>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛山市| 芦山县| 博兴县| 揭东县| 江津市| 远安县| 江源县| 佛学| 赣州市| 临西县| 林甸县| 广安市| 惠来县| 松原市| 万安县| 延边| 昌江| 聊城市| 肇庆市| 保山市| 伊宁县| 大丰市| 临漳县| 长海县| 乌海市| 仙游县| 徐水县| 绥德县| 紫金县| 常德市| 来凤县| 普兰店市| 民乐县| 酒泉市| 高要市| 犍为县| 犍为县| 南昌县| 东乡| 固原市| 墨玉县|