最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net 數(shù)據(jù)庫連接類代碼(SQL)

 更新時間:2010年03月03日 14:06:56   作者:  
asp.net數(shù)據(jù)庫連接類(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 內(nèi)部函數(shù)
/// <summary>
/// 打開數(shù)據(jù)庫連接
/// </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ù)據(jù)庫連接
/// </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>
/// 根據(jù)存儲過程返回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>
/// 根據(jù)存儲過程返回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;
}
}

相關文章

  • 解析WPF綁定層次結構數(shù)據(jù)的應用詳解

    解析WPF綁定層次結構數(shù)據(jù)的應用詳解

    本文講述WPF中單層次數(shù)據(jù)和多層次數(shù)據(jù)的綁定方法,主要闡述數(shù)據(jù)綁定的顯示層面,其中涉及了ListBox和Treeview控件。并說明它們之間的差異
    2013-05-05
  • .NET 6開發(fā)之實現(xiàn)緩存過程詳解

    .NET 6開發(fā)之實現(xiàn)緩存過程詳解

    有的時候為了減少客戶端請求相同資源的邏輯重復執(zhí)行,我們會考慮使用一些緩存的方式。這篇文章主要就介紹了在.NET 6開發(fā)中如何實現(xiàn)緩存,感興趣的可以學習一下
    2022-01-01
  • 實例說明asp.net中的簡單角色權限控制

    實例說明asp.net中的簡單角色權限控制

    權限控制在信息管理中屬于基本功能,權限控制中其中以Window權限為模型的角色用戶(也稱用戶組用戶)模型使用較多。本文以網(wǎng)站管理后臺權限控制為例簡要說明。
    2009-10-10
  • asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)

    asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)

    上傳功能,是大家經(jīng)常用到了,可能每一個項目都可以會用到。網(wǎng)上到處都有上傳功能的代碼。比我寫的好的有很多。我這里也僅是分享我的代碼。
    2011-09-09
  • ADO.NET實用經(jīng)驗匯總

    ADO.NET實用經(jīng)驗匯總

    這篇文章主要介紹了ADO.NET實用經(jīng)驗匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • asp.net 生成靜態(tài)頁時的進度條顯示

    asp.net 生成靜態(tài)頁時的進度條顯示

    本文側重點在講解生成靜態(tài)頁的“進度條”,所以將采用模擬的方法。生成靜態(tài)時需要生成的文章必須非常多,否則進度條可能一閃而過,看不到效果。
    2009-05-05
  • ASP.NET連接數(shù)據(jù)庫并獲取數(shù)據(jù)方法總結

    ASP.NET連接數(shù)據(jù)庫并獲取數(shù)據(jù)方法總結

    這篇文章主要介紹了ASP.NET連接數(shù)據(jù)庫并獲取數(shù)據(jù)方法,結合實例分析總結了ASP.NET連接數(shù)據(jù)庫及獲取數(shù)據(jù)的相關實現(xiàn)技巧,并附帶了web.config配置文件的使用方法與相關注意事項,需要的朋友可以參考下
    2015-11-11
  • asp.net Repeater 數(shù)據(jù)綁定代碼

    asp.net Repeater 數(shù)據(jù)綁定代碼

    asp.net Repeater 數(shù)據(jù)綁定代碼
    2010-03-03
  • ASP.NET?Core中MVC模式實現(xiàn)路由二

    ASP.NET?Core中MVC模式實現(xiàn)路由二

    這篇文章介紹了ASP.NET?Core中MVC模式實現(xiàn)路由的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • .net自帶的庫生成zip文件的方法

    .net自帶的庫生成zip文件的方法

    平時我們創(chuàng)建Zip文件的時候,要么用現(xiàn)成的軟件,要么用第三方的開源庫。其實用.net自帶的類操作起來也非常方便
    2012-08-08

最新評論

筠连县| 贵定县| 永康市| 崇阳县| 黄山市| 仁化县| 潼关县| 大洼县| 临海市| 万载县| 怀集县| 丁青县| 辽宁省| 黔西县| 南投市| 陕西省| 陵川县| 通化县| 读书| 秦安县| 亚东县| 山阴县| 镇坪县| 永修县| 苏尼特左旗| 济源市| 营口市| 铁岭市| 兰州市| 那坡县| 繁昌县| 内丘县| 石棉县| 阳泉市| 洛隆县| 大洼县| 镇平县| 稷山县| 岳池县| 新营市| 壶关县|