求SQLHelper类中文版(注释中文的)

 我来答
百度网友94ee01a86
推荐于2016-11-07 · TA获得超过237个赞
知道答主
回答量:297
采纳率:0%
帮助的人:147万
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using BookShop.Models;

namespace BookShop.DAL
{
public class DBHelp
{
public static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["mybookshop"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}

#region 增,删,改ExecuteNonQuery
/// <summary>
/// 单个数据增,删,改
/// </summary>
/// <param name="safeSql"></param>
/// <returns></returns>
public static int ExecuteCommand(string safeSql)
{
try
{
using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
{
int result = cmd.ExecuteNonQuery();
return result;
}

}
catch (SqlException ex)
{

throw ex;
}
}
#endregion

#region 带参数的增,删,改ExecuteNonQuery
/// <summary>
/// 带多个参数的增,删,改
/// </summary>
/// <param name="safeSql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static int ExecuteCommand(string safeSql, params SqlParameter[] values)
{
try
{
using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
{
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
throw ex;
}
}
#endregion

#region 带参数的增,删,改ExecuteNonQuery
/// <summary>
/// 带多个参数的增,删,改
/// </summary>
/// <param name="safeSql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static int ExecuteCommand(string safeSql, CommandType type, params SqlParameter[] values)
{
try
{
using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
{
cmd.CommandType = type;
cmd.Parameters.AddRange(values);
cmd.ExecuteNonQuery();
return cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
throw ex;
}
}
#endregion

#region 带参数的增,删,改ExecuteNonQuery (Special)
/// <summary>
/// 带多个参数的增,删,改
/// </summary>
/// <param name="safeSql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static int ExecuteCommand(string safeSql,CommandType type, int index)
{
try
{
using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
{
cmd.CommandType = type;
SqlParameter paramOne = new SqlParameter("@rid", SqlDbType.Int);
paramOne.Value = index;
cmd.Parameters.Add(paramOne);
return cmd.ExecuteNonQuery();
}
}
catch (SqlException ex)
{
throw ex;
}
}
#endregion

#region 查询语句ExecuteScalar
/// <summary>
/// 查单个值
/// </summary>
/// <param name="safeSql"></param>
/// <returns></returns>
public static int GetScalar(string safeSql)
{
try
{
using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
{
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}

}
catch (SqlException ex)
{

throw ex;
}

}
#endregion

#region 带参数的查询语句ExecuteScalar
/// <summary>
/// 带参数的查询语句
/// </summary>
/// <param name="sql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static int GetScalar(string sql, params SqlParameter[] values)
{
try
{
using (SqlCommand cmd = new SqlCommand(sql, Connection))
{
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
catch (SqlException ex)
{

throw ex;
}
}
#endregion

#region 带执行类型的ExecuteScalar
/// <summary>
/// 带执行类型的ExecuteScalar
/// </summary>
/// <param name="sql"></param>
/// <param name="type"></param>
/// <param name="values"></param>
/// <returns></returns>
public static int GetScalar(string sql, CommandType type, params SqlParameter[] values)
{
try
{
using (SqlCommand cmd = new SqlCommand(sql, Connection))
{
cmd.CommandType = type;
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
}
catch (SqlException ex)
{
throw ex;
}
}
#endregion

#region 返回DataReader
/// <summary>
/// 查询表,获取多个记录
/// </summary>
/// <param name="safeSql"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string safeSql)
{
try
{
using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
{
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}

}
catch (SqlException ex)
{

throw ex;
}
}
#endregion

#region 带参数DataReader
/// <summary>
/// 带参数的-查询表,获取多个记录
/// </summary>
/// <param name="sql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
try
{
using (SqlCommand cmd = new SqlCommand(sql, Connection))
{
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
}
catch (SqlException)
{

throw;
}

}
#endregion

#region 返回DataReader ,语句,类型,参数
/// <summary>
/// 查询表,获取多个记录---语句,类型,参数
/// </summary>
/// <param name="safeSql"></param>
/// <param name="cmdType"></param>
/// <param name="values"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string safeSql, CommandType cmdType, params SqlParameter[] values)
{
try
{
using (SqlCommand cmd = new SqlCommand(safeSql, Connection))
{
cmd.CommandType = cmdType;
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}

}
catch (SqlException ex)
{

throw ex;
}

}
#endregion

#region 返回dataTable
/// <summary>
/// 返回datatable
/// </summary>
/// <param name="safeSql"></param>
/// <returns></returns>
public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
#endregion

#region 返回dataTable ,带参数
/// <summary>
/// 返回dataTable ,带参数使用
/// </summary>
/// <param name="sql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
#endregion

}

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
希奕际
2024-04-10 广告
剑桥文理与剑桥艺术学校中国 基于剑桥文理学校优质的海外校本部资源和课程方向,现开设四类课程:艺术与设计课程、音乐与戏剧课程、英国A-Level课程、波士顿文理中学美高与AP课程。荣获2021胡润百学中国国际学校艺术类领军学校奖,2022胡润... 点击进入详情页
本回答由希奕际提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式