.net C# Sql数据库SQLHelper类
2018-06-23 23:55:04来源:未知 阅读 ()
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public class SQLHelper
{
//取得数据库连接web.config 中配置
public static readonly string ConnectString = ConfigurationManager.ConnectionStrings["DBString"].ConnectionString;
/// <summary>
/// 无事务,数据查询
/// </summary>
/// <param name="cmdType">存储过程或Sql语句</param>
/// <param name="cmdText">存储过程名或Sql语句内容</param>
/// <param name="CommandParams">参数列表</param>
/// <returns></returns>
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(ConnectString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
catch
{
throw;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 有事务,数据操作类
/// </summary>
/// <param name="trans">事务</param>
/// <param name="cmdType">操作类别 (stored procedure,sql)</param>
/// <param name="cmdText">存储过程名或Sql语句</param>
/// <param name="CommandParams">参数</param>
/// <returns>返回影响的数据行数</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
/*if (cmdType == CommandType.StoredProcedure)
{
cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
val = (int)cmd.Parameters["@RETURN_VALUE"].Value;
}
else
if (cmdType==CommandType.Text)
{
val = cmd.ExecuteNonQuery();
}*/
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 返回数据集 DataReader
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="CommandParams"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(ConnectString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception ex)
{
conn.Close();
// throw new Exception("操作失败!");
throw new Exception(ex.Message);
}
}
/// <summary>
/// 有事务的取数据
/// </summary>
/// <param name="trans"></param>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="CommandParams"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(ConnectString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
catch
{
throw;
}
finally
{
conn.Close();
}
}
public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 根据Sql语句取得表
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="CommandParams"></param>
/// <returns></returns>
public static DataTable ExecuteTable(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
DataTable temptable = new DataTable();
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(ConnectString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
da.Fill(temptable);
}
finally
{
conn.Close();
}
return temptable;
}
public static DataTable ExecuteTable(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
DataTable temptable = new DataTable();
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
da.Fill(temptable);
cmd.Parameters.Clear();
return temptable;
}
/// <summary>
/// 根据Sql语句或存储过程取得数据
/// </summary>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="CommandParams"></param>
/// <returns></returns>
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlConnection conn = new SqlConnection(ConnectString);
SqlCommand cmd = new SqlCommand();
DataSet TempDataSet = new DataSet();
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, CommandParams);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(TempDataSet);
cmd.Parameters.Clear();
return TempDataSet;
}
finally
{
conn.Close();
}
}
public static DataSet ExecuteDataSet(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] CommandParams)
{
SqlCommand cmd = new SqlCommand();
DataSet TempDataSet = new DataSet();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, CommandParams);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(TempDataSet);
cmd.Parameters.Clear();
return TempDataSet;
}
/// <summary>
/// 生成Sql语句或准备
/// </summary>
/// <param name="cmd"></param>
/// <param name="conn"></param>
/// <param name="trans"></param>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="cmdParms"></param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
public static object ToDBValue(object value)
{
return value == null ? DBNull.Value : value;
}
public static object FromDBValue(object dbValue)
{
return dbValue == DBNull.Value ? null : dbValue;
}
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Cookie SameSite属性介绍及其在ASP.NET项目中的应用 2020-03-28
- jsp+servlet+mysql简单实现用户登陆注册 2019-08-26
- Asp.Net 将HTML中通过dom-to-image.js标签div内的内容转化为 2019-03-10
- ASP.NET MVC4分页Site.CSS 2018-12-20
- MySql+Socket 完成数据库的增查Demo 2018-09-05
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash