using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SystemDAL
{
#region 数据库层操作类
public class SqlHelper
{
///
/// 添加 修改 删除所用的方法
///
///
///
///
/// rowscount
public static int ExecuteNonQuery(string sql, CommandType commandtype, params SqlParameter[] parameters)
{
SqlCommand cmd = GetCommand(sql, commandtype, parameters);
int rowscount = cmd.ExecuteNonQuery(); //返回受影响的行数
cmd.Parameters.Clear(); //移除对象
cmd.Connection.Close(); //关闭数据库连接
return rowscount;
}
///
/// 一个被重用的得到Command命令对象的方法
///
///
///
///
/// cmd
private static SqlCommand GetCommand(string sql, CommandType commandtype, params SqlParameter[] parameters)
{
GetConnectionString Opencon = new GetConnectionString();
Opencon.OpenDataBase();
SqlConnection conn = Opencon.m_objConnection;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = commandtype;
cmd.CommandText = sql;
if (parameters != null)
{
foreach (SqlParameter p in parameters)
{
cmd.Parameters.Add(p);
}
}
//cmd.Connection.Open();
return cmd;
}
///
/// 查询返回一行一列的值
///
///
///
///
/// result
public static object ExecuteScalar(string sql, CommandType commandtype, params SqlParameter[] parameters)
{
SqlCommand cmd = GetCommand(sql, commandtype, parameters);
object result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
cmd.Connection.Close();
return result;
}
///
/// 查询得到Datareader
///
///
///
///
/// dr
public static SqlDataReader ExecuteReader(string sql, CommandType commandtype, params SqlParameter[] parameters)
{
SqlCommand cmd = GetCommand(sql, commandtype, parameters);
//SqlDataReader dr = cmd.ExecuteReader();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
}
#endregion
}