四個(gè)常用的.NET的SQLHELPER方法實(shí)例
本文所述實(shí)例有別于網(wǎng)上常見的由代碼生成器生成的sqlhelper,比如動(dòng)軟、CodeSmith等生成的。其實(shí)代碼生成器生成的sqlhelper很多的方法在實(shí)際開發(fā)中都是用不到的,考慮初學(xué)者如果封裝類的方法太多,會(huì)造成一定的困擾,也會(huì)給他們?cè)黾迂?fù)擔(dān),所以本文列舉出了再實(shí)際運(yùn)用中總結(jié)的四個(gè)比較常用的方法,其實(shí),最常用的應(yīng)該是兩個(gè),就是查和增刪改,其它兩個(gè)也是用的比較少的。
需要說明的是,sqlhelper在winform的開發(fā)中用的比較多,在asp.net和mvc的項(xiàng)目中用的封裝類跟winform有相似,但是也有一定的區(qū)別,因?yàn)榇箜?xiàng)目都是用那種比較好的框架,或者自己公司開發(fā)的框架,其中的封裝類也有所不同,本文總結(jié)的這四個(gè)方法在winform中用比較常用。
主要代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace SQL
{
public static class SqlHelper
{
/// <summary>
/// 創(chuàng)建連接的字符串
/// </summary>
static readonly string connStr=ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
#region 1.0 執(zhí)行查詢語句,返回一個(gè)表 + static DataTable ExcuteTable(string sql, params SqlParameter[] ps)
/// <summary>
/// 1.0 執(zhí)行查詢語句,返回一個(gè)表
/// </summary>
/// <param name="sql">sql語句</param>
/// <param name="ps">參數(shù)數(shù)組</param>
/// <returns>返回一張表</returns>
public static DataTable ExcuteTable(string sql, params SqlParameter[] ps)
{
SqlDataAdapter da = new SqlDataAdapter(sql, connStr);
da.SelectCommand.Parameters.AddRange(ps);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
#endregion
#region 2.0 執(zhí)行增刪改的方法 + static int ExcuteNoQuery(string sql, params SqlParameter[] ps)
/// <summary>
/// 2.0 執(zhí)行增刪改的方法
/// </summary>
/// <param name="sql">sql語句</param>
/// <param name="ps">參數(shù)數(shù)組</param>
/// <returns>返回一條記錄</returns>
public static int ExcuteNoQuery(string sql, params SqlParameter[] ps)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.AddRange(ps);
return command.ExecuteNonQuery();
}
}
#endregion
#region 3.0 執(zhí)行存儲(chǔ)過程的方法 + static int ExcuteProc(string procName, params SqlParameter[] ps)
/// <summary>
/// 3.0 執(zhí)行存儲(chǔ)過程的方法
/// </summary>
/// <param name="procName">存儲(chǔ)過程名</param>
/// <param name="ps">參數(shù)數(shù)組</param>
/// <returns></returns>
public static int ExcuteProc(string procName, params SqlParameter[] ps)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand command = new SqlCommand(procName, conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(ps);
return command.ExecuteNonQuery();
}
}
#endregion
#region 4.0 查詢結(jié)果集,返回的是首行首列 + static int ExecScalar(string sql, params SqlParameter[] ps)
/// <summary>
/// 4.0 查詢結(jié)果集,返回的是首行首列
/// </summary>
/// <param name="sql">sql語句</param>
/// <param name="ps">參數(shù)數(shù)組</param>
/// <returns></returns>
public static object ExecScalar(string sql, params SqlParameter[] ps) //調(diào)用的時(shí)候才判斷是什么類型
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.AddRange(ps);
return command.ExecuteScalar();
}
}
#endregion
}
}
相信本文所述對(duì)大家的.net程序設(shè)計(jì)有一定的借鑒價(jià)值。
- php中分頁(yè)及SqlHelper類用法實(shí)例
- C#實(shí)現(xiàn)較為實(shí)用的SQLhelper
- 微軟官方SqlHelper類 數(shù)據(jù)庫(kù)輔助操作類
- C#基于SQLiteHelper類似SqlHelper類實(shí)現(xiàn)存取Sqlite數(shù)據(jù)庫(kù)的方法
- c#中SqlHelper封裝SqlDataReader的方法
- C#實(shí)現(xiàn)操作MySql數(shù)據(jù)層類MysqlHelper實(shí)例
- 自己編寫sqlhelper類示例分享
- c# SQLHelper(for winForm)實(shí)現(xiàn)代碼
- asp.net SqlHelper數(shù)據(jù)訪問層的使用
- C# SqlHelper應(yīng)用開發(fā)學(xué)習(xí)
相關(guān)文章
利用Aspose.Cells實(shí)現(xiàn)萬能導(dǎo)出功能
這篇文章主要為大家詳細(xì)介紹了利用Aspose.Cells實(shí)現(xiàn)萬能導(dǎo)出功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
c#設(shè)置xml內(nèi)容不換行及屬性xsi:nil=true的空節(jié)點(diǎn)添加
c#設(shè)置xml內(nèi)容不換行:添加屬性為xsi:nil=true的空節(jié)點(diǎn)便可實(shí)現(xiàn),感興趣的你可以參考下本文,或許有意想不到的收獲2013-03-03
ASP.NET?Core?MVC中使用Tag?Helper組件
這篇文章介紹了ASP.NET?Core?MVC中使用Tag?Helper組件的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
集合類Array List HashTable實(shí)例操作練習(xí)
集合常用操作添加、遍歷、移除;本文將詳細(xì)介紹下ArrayList對(duì)值類型的操作/ArrayList對(duì)引用類型的操作及HashTable的使用,感興趣的你可不要錯(cuò)過了哈2013-02-02
asp.net System.Guid ToString五種格式
這篇文章主要介紹了asp.net System.Guid ToString五種格式,需要的朋友可以參考下2017-02-02
ASP.NET Web頁(yè)生命周期和執(zhí)行的方法介紹
這是前幾天去一家公司時(shí),其中當(dāng)時(shí)的一個(gè)筆試題! 我去,看了當(dāng)時(shí)我就暈菜了,所以,就記錄下來,以備以后查看,方便需要的朋友2012-10-10
asp.net SqlDataReader綁定Repeater
asp.net SqlDataReader綁定Repeater2009-04-04

