public static class SqlHelper
{
public static object FromDbValue(object value)
{
if (value == DBNull.Value)
{
return null;
}
else
{
return value;
} } public static object ToDbValue(object value)
{
if (value == null)
{
return DBNull.Value;
}
else
{
return value;
}
} private static string connStr = ConfigurationManager.ConnectionStrings["myconnstring"].ConnectionString; public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{ cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
} public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();
}
}
} public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
DataSet dataset = new DataSet();
SqlDataAdapter apdater = new SqlDataAdapter(cmd);
apdater.Fill(dataset);
return dataset.Tables[];
}
}
}
public static DataSet ExecuteDataSet(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
DataSet dataset = new DataSet();
SqlDataAdapter apdater = new SqlDataAdapter(cmd);
apdater.Fill(dataset);
return dataset;
}
}
}
}
2016.1.29
1.增加存储过程
2.修改垃圾回收机制
public class SQL_Helper
{
private static readonly string connectionString =
ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters) {
using (SqlConnection connection = new SqlConnection(connectionString)) {
using( SqlCommand command = new SqlCommand( cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
DataSet dataset = new DataSet();
SqlDataAdapter apdater = new SqlDataAdapter(command);
apdater.Fill(dataset);
return dataset.Tables[];
}
}
}
public static DataSet ExecuteDataSet(string cmdText,params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString)) {
using (SqlCommand command = new SqlCommand(cmdText, connection)) {
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
DataSet dataset = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(command);
sda.Fill(dataset);
return dataset;
}
}
}
public static SqlDataReader ExecuteReader(string cmdText, params SqlParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
}
}
public static SqlDataReader ExecuteReaderSp(string cmdText, params SqlParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
command.CommandType = CommandType.StoredProcedure;
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
}
}
}
2016.1.30
1.修改存储过程,返回 object dataset dataread datatable
public class SqlHelper
{ #region comm sql
public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
DataSet dataset = new DataSet();
SqlDataAdapter apdater = new SqlDataAdapter(command);
apdater.Fill(dataset);
return dataset.Tables[];
}
}
}
public static DataSet ExecuteDataSet(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
DataSet dataset = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(command);
sda.Fill(dataset);
return dataset;
}
}
}
public static SqlDataReader ExecuteReader(string cmdText, params SqlParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteReader();
}
}
}
#endregion #region storedProcedure
public static int ExecuteNonQuerySp(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
command.CommandType = CommandType.StoredProcedure;
return command.ExecuteNonQuery();
}
}
}
public static object ExecuteScalarSp(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
command.CommandType = CommandType.StoredProcedure;
return command.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTableSp(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
command.CommandType = CommandType.StoredProcedure;
DataSet dataset = new DataSet();
SqlDataAdapter apdater = new SqlDataAdapter(command);
apdater.Fill(dataset);
return dataset.Tables[];
}
}
}
public static SqlDataReader ExecuteReaderSp(string cmdText, params SqlParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
command.CommandType = CommandType.StoredProcedure;
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
}
}
public static DataSet ExecuteDataSetSp(string cmdText, params SqlParameter[] parameters) {
using (SqlConnection conn = new SqlConnection(connectionString)) {
using (SqlCommand comm = new SqlCommand(cmdText, conn)) {
if (parameters != null) comm.Parameters.AddRange(parameters);
if (conn.State == ConnectionState.Closed) conn.Open();
comm.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(comm);
sda.Fill(ds);
return ds;
}
}
} #endregion
}
2016.4.19
1.增加事务管理
public class MSSQLHelper
{ private static string connectionString = ConfigurationManager.ConnectionStrings["myconnstring"].ConnectionString;
#region comm sql
public static int ExecuteNonQuery(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
DataSet dataset = new DataSet();
SqlDataAdapter apdater = new SqlDataAdapter(command);
apdater.Fill(dataset);
return dataset.Tables[];
}
}
}
public static DataSet ExecuteDataSet(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
DataSet dataset = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(command);
sda.Fill(dataset);
return dataset;
}
}
}
public static SqlDataReader ExecuteReader(string cmdText, params SqlParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteReader();
}
}
}
#endregion #region storedProcedure
public static int ExecuteNonQuerySp(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
command.CommandType = CommandType.StoredProcedure;
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open(); return command.ExecuteNonQuery();
}
}
}
public static object ExecuteScalarSp(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
command.CommandType = CommandType.StoredProcedure;
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTableSp(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
command.CommandType = CommandType.StoredProcedure;
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
DataSet dataset = new DataSet();
SqlDataAdapter apdater = new SqlDataAdapter(command);
apdater.Fill(dataset);
return dataset.Tables[];
}
}
}
public static SqlDataReader ExecuteReaderSp(string cmdText, params SqlParameter[] parameters)
{
SqlConnection connection = new SqlConnection(connectionString);
{
using (SqlCommand command = new SqlCommand(cmdText, connection))
{
command.CommandType = CommandType.StoredProcedure;
if (parameters != null) command.Parameters.AddRange(parameters);
if (connection.State == ConnectionState.Closed) connection.Open();
return command.ExecuteReader(CommandBehavior.CloseConnection);
}
}
}
public static DataSet ExecuteDataSetSp(string cmdText, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand(cmdText, conn))
{
if (parameters != null) comm.Parameters.AddRange(parameters);
if (conn.State == ConnectionState.Closed) conn.Open();
comm.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(comm);
sda.Fill(ds);
return ds;
}
}
} #endregion #region transaction
public static bool RunSqlsTran(string[] strSql)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
int len = strSql.Length;
if (conn.State == ConnectionState.Closed) conn.Open();
SqlTransaction myTrans = conn.BeginTransaction();
try
{
comm.Connection = conn;
comm.Transaction = myTrans;
foreach (var s in strSql)
{
comm.CommandText = s;
comm.ExecuteNonQuery();
}
myTrans.Commit();
return true;
}
catch (System.Data.SqlClient.SqlException e)
{
myTrans.Rollback();
return false;
}
}
}
}
public static bool RunSqlsTran(string[] strSql , SqlParameter[] parameter)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
int len = strSql.Length;
if (conn.State == ConnectionState.Closed) conn.Open();
SqlTransaction myTrans = conn.BeginTransaction();
try
{
int strLen = strSql.Length;
comm.Connection = conn;
comm.Transaction = myTrans;
for(int i = ;i < strLen; i++)
{
comm.CommandText = strSql[i];
comm.Parameters.Add(parameter[i]);
comm.ExecuteNonQuery();
} myTrans.Commit();
return true;
}
catch (System.Data.SqlClient.SqlException e)
{
myTrans.Rollback();
return false;
}
}
}
}
#endregion
}
20160621 看到一个好的代码,重写ado.net的集中方式,编码普通和存储过程的重复代码
public abstract class SQLHelper
{
public static readonly string txtConnecttionString = ConfigurationManager.ConnectionStrings["SqlConnectStringOne"].ConnectionString; public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParas)
{ SqlCommand cmd = new SqlCommand();
using (SqlConnection con = new SqlConnection(txtConnecttionString))
{ PrepareCommand(cmd, con, null, cmdType, cmdText, cmdParas);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val; } }
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParas)
{ SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
try
{ PrepareCommand(cmd, con, null, cmdType, cmdText, cmdParas);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr; }
catch (SqlException ex)
{ con.Close();
throw new Exception(ex.Message, ex); }
}
public static object ExecuteSclare(string connectionString, CommandType cmdType, string cmdText, SqlParameter[] cmdParas)
{ SqlCommand cmd = new SqlCommand();
using (SqlConnection con = new SqlConnection(connectionString))
{ PrepareCommand(cmd, con, null, cmdType, cmdText, cmdParas); object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val; } }
public static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParas)
{ if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText; if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType; //当定义cmdParas.Length< 0的时候,在调用该方法时,如果参数为空的话就会报错,错误为“调用的对象可能为空”,所以使用cmdParas!=null if (cmdParas != null)
{ foreach (SqlParameter para in cmdParas)
{ cmd.Parameters.Add(para); } } }
}
}
SqlHelper.cs的更多相关文章
-
微软版的SqlHelper.cs类
一,微软SQLHelper.cs类 中文版: using System; using System.Data; using System.Xml; using System.Data.SqlClien ...
-
微软SQLHelper.cs类 中文版
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...
-
微软SQLHelper.cs类
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
-
C#、WinForm、ASP.NET - SQLHelper.cs
SQLHelper.cs using System; using System.Data; using System.Configuration; using System.Data.SqlClien ...
-
处女篇:自用C#后端SqlHelper.cs类
自用SqlHelper.cs类,此类来自软谋教育徐老师课程SqlHelper.cs! using System; using System.Collections; using System.Coll ...
-
C#版SQLHelper.cs类
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
-
SQLHelper.cs的经典代码-存储过程
using System; using System.Collections.Generic; using System.Text; using System.Collections; using S ...
-
SQL Server数据库连接类SQLHelper.cs
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...
-
微软C#版SQLHelper.cs类
转载自:http://blog.csdn.net/fengqingtao2008/article/details/17399247 using System; using System.Data; u ...
随机推荐
-
Java基础Map接口+Collections
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
-
C# MD5加密
public static string Encrypt(string txt) { System.Security.Cryptography.MD5CryptoServiceProvider md5 ...
-
---Arch Linux 之AUR
只需下载压缩包,解压,进入文件夹,里面好像也只有一个PKBUILD文件,makepkg -s (自动下载程序然后编译打包), 然后pacman -U xxxx.pkg.xz 就好了
-
redis 详解
什么是redis? redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) Reids的特点 Redis本质上是一个Key-Value类型的内存数据库, ...
-
【extjs】 extjs5 Ext.grid.Panel 搜索示例
先看效果图: 页面js: <script type="text/javascript"> /** * 日志类型 store * */ var logTypeStore ...
-
C++构造函数的自动调用(调用一个父类的构造函数,有显性调用最好,否则就默认调用无参数的构造函数)——哲学思想:不调用怎么初始化父类的成员数据和VMT?
我总是记不住构造函数的特点,关键还是没有领会那个哲学思想:父类的构造函数一方面要初始化它自己的成员数据,另一方面也要建立它自己的VMT呀!心里默念一百遍:一定调用父类构造函数,一定调用父类构造函数,一 ...
-
Connect To Ubuntu 16.04 / 17.10 / 18.04 Desktop Via Remote Desktop Connection (RDP) With Xrdp
[1] https://websiteforstudents.com/connect-to-ubuntu-16-04-17-10-18-04-desktop-via-remote-desktop-co ...
-
CountDownLatch/CyclicBarrie用法记录
在jdk1.5中,java提供了很多工具类帮助我们进行并发编程,其中就有CountDownLatch和CyclicBarrie 1.CountDownLatch的用法 CountDownLatch 位 ...
-
Spring Boot 构建电商基础秒杀项目 (六) 用户登陆
SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...
-
Windows系统下oracle数据库每天定时备份
第一步:建立备份脚本oraclebackup.bat 首先建立一个备份bat文件,在D盘下新建备份目录oraclebackup,将oracle安装目录下的EXP.EXE复制到此目录下,再新建一个文本文 ...