资源简介
用委托写的sqlHelper,代码简洁了,而且更加的灵活
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlHelperLianxi
{
class SqlHelper
{
public delegate object SqlDeal(SqlCommand cmd);
public static SqlDeal mySqlDeal = null;
public static readonly string connstr =
ConfigurationManager.ConnectionStrings[“connstr“].ConnectionString;
public static int ExecuteNonQuery(string cmdText params SqlParameter[] parameters)
{
return (int)Connectionhelp(ExecuteNonQueryHelp cmdText parameters);
}
public static object ExecuteScalar(string cmdTextparams SqlParameter[] parameters)
{
return Connectionhelp(ExecuteScalarHelp cmdText parameters);
}
public static DataTable ExecuteDataTable(string cmdText params SqlParameter[] parameters)
{
return (DataTable)Connectionhelp(ExecuteDataTableHelp cmdText parameters);
}
public static SqlDataReader ExecuteDataReader(string cmdText params SqlParameter[] parameters)
{
return (SqlDataReader)Connectionhelp(ExecuteDataTableHelp cmdText parameters);
}
public static object Connectionhelp(SqlDeal mySqlDeal string cmdText params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters);
return mySqlDeal(cmd);
}
}
}
public static object ExecuteNonQueryHelp(SqlCommand cmd)
{
return cmd.ExecuteNonQuery();
}
public static object ExecuteScalarHelp(SqlCommand cmd)
{
return cmd.ExecuteScalar();
}
public static DataTable ExecuteDataTableHelp(SqlCommand cmd)
{
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
}
public static SqlDataReader ExecuteDataReader(SqlCommand cmd)
{
return cmd.ExecuteReader
(CommandBehavior.CloseConnection);
}
//public static int ExecuteNonQuery(string cmdText
// params SqlParameter[] parameters)
//{
// using (SqlConnection conn = new SqlConnection(connstr))
// {
// conn.Open();
// using (SqlCommand cmd = conn.CreateCommand())
// {
// cmd.CommandText = cmdText;
//
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5025 2015-01-22 08:56 SqlHelper.cs
----------- --------- ---------- ----- ----
5025 1
评论
共有 条评论