using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace dal
{
public class sqlhelper
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr = null;
private SqlDataAdapter sda = null;
public sqlhelper()
{
string connstr = ConfigurationManager.ConnectionStrings
["connstr"].ConnectionString;
conn = new SqlConnection(connstr);
}
private SqlConnection getconn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
/// <summary>
/// 执行不带参数的增删改sql语句或者存储过程
/// </summary>
/// <param name="cmdtext">sql语句或者存储过程</param>
/// <param name="ct">命令类型</param>
/// <returns></returns>
//public int executenonquery(string cmdtext, CommandType ct)
//{
// int res;
// try
// {
// cmd = new SqlCommand(cmdtext, getconn());
// cmd.CommandType = ct;
// res = cmd.ExecuteNonQuery();
// }
// catch (Exception ex)
// {
// throw ex;
// }
// finally
// {
// if (conn.State == ConnectionState.Open)
// {
// conn.Close();
// }
// }
// return res;
//}
/// <summary>
/// 执行不带参数的增删改语句或者存储过程
/// </summary>
/// <param name="cmdtext">sql语句或者存储过程</param>
/// <param name="ct">命令类型</param>
/// <returns></returns>
public int executenonquery(string cmdtext, CommandType ct)
{
int res;
using (cmd = new SqlCommand(cmdtext, getconn()))
{
cmd.CommandType = ct;
res = cmd.ExecuteNonQuery();
}
return res;
}
/// <summary>
/// 执行带参数的增删改语句
/// </summary>
/// <param name="cmdtext">sql语句或者存储过程</param>
/// <param name="paras">参数集合</param>
/// <param name="ct">命令类型</param>
/// <returns></returns>
public int executenonquery(string cmdtext, SqlParameter[] paras,
CommandType ct)
{
int res;
using (cmd = new SqlCommand(cmdtext, getconn()))
{
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
res = cmd.ExecuteNonQuery();
}
return res;
}
/// <summary>
/// 该方法执行传入的查询sql或者存储过程
/// </summary>
/// <param name="cmdtext">sql语句或者存储过程</param>
/// <param name="ct">命令类型</param>
/// <returns></returns>
public DataTable executequery(string cmdtext, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdtext, getconn());
cmd.CommandType = ct;
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
/// <summary>
/// 执行带参数的SQL或者存储过程查询
/// </summary>
/// <param name="cmdtext">sql语句或者存储过程</param>
/// <param name="paras">命令集合</param>
/// <param name="ct">命令类型</param>
/// <returns></returns>
public DataTable executequery(string cmdtext, SqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(cmdtext, getconn());
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
/// <summary>
/// 不带参数sql或者存储过程查询过程
/// </summary>
/// <param name="cmdtext">sql语句或者存储过程名称</param>
/// <param name="ct">命令的类型</param>
/// <returns>返回一个dataset数据库</returns>
public DataSet datasetquery(string cmdtext, CommandType ct)
{
DataSet ds = new DataSet();
cmd = new SqlCommand(cmdtext, getconn());
cmd.CommandType = ct;
using (sda = new SqlDataAdapter(cmd))
{
sda.Fill(ds);
}
return ds;
}
/// <summary>
/// 带参数sql或者存储过程查询过程
/// </summary>
/// <param name="cmdtext">sql语句或者存储过程名称</param>
/// <param name="paras">参数</param>
/// <param name="ct">命令的类型</param>
/// <returns>返回dataset数据库</returns>
public DataSet datasetquerypara(string cmdtext, SqlParameter[] paras, CommandType ct)
{
DataSet ds = new DataSet();
cmd = new SqlCommand(cmdtext, getconn());
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
using (sda = new SqlDataAdapter(cmd))
{
sda.Fill(ds);
}
return ds;
}
/// <summary>
/// 测试程序
/// </summary>
/// <param name="proc_name">存储过程名称</param>
/// <returns></returns>
public DataTable test(string proc_name)
{
DataTable dt = new DataTable();
cmd = new SqlCommand(proc_name, getconn());
cmd.CommandType = CommandType.StoredProcedure;
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
}
}
------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace dal
{
public class newsdao
{
private sqlhelper sqlhelperex;
public newsdao()
{
sqlhelperex = new sqlhelper();
}
/// <summary>
/// 取出所有记录
/// </summary>
/// <returns></returns>
public DataTable selectallnews()
{
return sqlhelperex.executequery("proc_select_allnews", CommandType.StoredProcedure);
}
// /// <summary>
// ///取出最新10条新闻
// /// </summary>
// /// <returns></returns>
// public DataTable selectnewnews()
// {
// return sqlhelperex.executequery("proc_select_newnews",
//CommandType.StoredProcedure);
// }
// /// <summary>
// /// 取出最新10调热点新闻
// /// </summary>
// /// <returns></returns>
// public DataTable selecttennewnews()
// {
// return sqlhelperex.executequery("proc_select_ten_news",
//CommandType.StoredProcedure);
// }
// /// <summary>
// /// 根据新闻类别取出新闻
// /// </summary>
// /// <param name="caid">新闻类别</param>
// /// <returns></returns>
// public DataTable selectbycaid(string caid)
// {
// DataTable dt = new DataTable();
// SqlParameter[] paras = new SqlParameter[] {
// new SqlParameter("@caid",caid)
// };
// dt = sqlhelperex.executequery("proc_select_bycaid", paras,
//CommandType.StoredProcedure);
// return dt;
// }
// /// <summary>
// /// 根据新闻id取出新闻
// /// </summary>
// /// <param name="id">新闻id</param>
// /// <returns></returns>
// public DataTable selectbynewsid(string id)
// {
// DataTable dt = new DataTable();
// SqlParameter[] paras = new SqlParameter[] {
// new SqlParameter("@id",id)
// };
// dt = sqlhelperex.executequery("proc_select_bynewsid", paras,
//CommandType.StoredProcedure);
// return dt;
// }
// /// <summary>
// /// 根据新闻标题搜索新闻
// /// </summary>
// /// <param name="title">新闻标题</param>
// /// <returns></returns>
// public DataTable selectbytitle(string title)
// {
// DataTable dt = new DataTable();
// SqlParameter[] paras = new SqlParameter[] {
// new SqlParameter("@title",title)
// };
// dt = sqlhelperex.executequery("proc_select_bynewstitle", paras,
//CommandType.StoredProcedure);
// return dt;
// }
/// <summary>
/// 根据c1字段内容搜索
/// </summary>
/// <param name="c1">c1字段内容</param>
/// <returns></returns>
public DataTable selectbychar(string c1)
{
DataTable dt = new DataTable();
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter("@c1",c1)
};
dt = sqlhelperex.executequery("proc_select_bynewstitle", paras, CommandType.StoredProcedure);
return dt;
}
// /// <summary>
// /// 根据新闻内容搜索新闻
// /// </summary>
// /// <param name="content">新闻内容</param>
// /// <returns></returns>
// public DataTable selectbycontent(string content)
// {
// DataTable dt = new DataTable();
// SqlParameter[] paras = new SqlParameter[] {
// new SqlParameter("@content",content)
// };
// dt = sqlhelperex.executequery("proc_select_bynewscontent", paras,
//CommandType.StoredProcedure);
// return dt;
// }
// /// <summary>
// /// 增加新闻
// /// </summary>
// /// <param name="title">新闻标题</param>
// /// <param name="content">新闻内容</param>
// /// <param name="caid">新闻类别id</param>
// /// <returns></returns>
// public bool insertnews(string title, string content, string caid)
// {
// bool flag = false;
// SqlParameter[] paras = new SqlParameter[] {
// new SqlParameter("@title",title),
// new SqlParameter("@content",content),
// new SqlParameter("@caid",caid)
// };
// int res = sqlhelperex.executenonquery("proc_insert_news", paras,
//CommandType.StoredProcedure);
// if (res > 0)
// {
// flag = true;
// }
// return flag;
// }
/// <summary>
/// 增加记录
/// </summary>
/// <param name="c1">字段1</param>
/// <param name="c2">字段2</param>
/// <returns>逻辑值</returns>
public bool insertchar(string c1, string c2)
{
bool flag = false;
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter("@c1",c1),
new SqlParameter("@c2",c2)
};
int res = sqlhelperex.executenonquery("proc_insert_news", paras, CommandType.StoredProcedure);
if (res > 0)
{
flag = true;
}
return flag;
}
// /// <summary>
// /// 修改新闻
// /// </summary>
// /// <param name="id">新闻id</param>
// /// <param name="title">新闻标题</param>
// /// <param name="content">新闻内容</param>
// /// <param name="caid">新闻类别id</param>
// /// <returns></returns>
// public bool updatenews(string id, string title, string content,
//string caid)
// {
// bool flag = false;
// SqlParameter[] paras = new SqlParameter[] {
// new SqlParameter("@id",id),
// new SqlParameter("@title",title),
// new SqlParameter("@content",content),
// new SqlParameter("@caid",caid)
// };
// int res = sqlhelperex.executenonquery("porc_update_news", paras,
//CommandType.StoredProcedure);
// if (res > 0)
// {
// flag = true;
// }
// return flag;
// }
/// <summary>
/// 更新记录
/// </summary>
/// <param name="id">id</param>
/// <param name="c1">字段1</param>
/// <param name="c2">字段2</param>
/// <returns></returns>
public bool updatechar(string id, string c1, string c2)
{
bool flag = false;
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter("@id",id),
new SqlParameter("@c1",c1),
new SqlParameter("@c2",c2)
};
int res = sqlhelperex.executenonquery("proc_update_news", paras, CommandType.StoredProcedure);
if (res > 0)
{
flag = true;
}
return flag;
}
// /// <summary>
// /// 删除新闻
// /// </summary>
// /// <param name="id">新闻id</param>
// /// <returns></returns>
// public bool deletenews(string id)
// {
// bool flag = false;
// SqlParameter[] paras = new SqlParameter[] {
// new SqlParameter("@id",id)
// };
// int res = sqlhelperex.executenonquery("proc_delete_news", paras,
//CommandType.StoredProcedure);
// if (res > 0)
// {
// flag = true;
// }
// return flag;
// }
/// <summary>
/// 删除记录
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool deletechar(string id)
{
bool flag = false;
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter("@id",id)
};
int res = sqlhelperex.executenonquery("proc_delete_news", paras, CommandType.StoredProcedure);
if (res > 0)
{
flag = true;
}
return flag;
}
/// <summary>
/// 删除所有数据库中所有数据,请谨慎使用
/// </summary>
/// <returns></returns>
public bool delecharall()
{
bool flag = false;
int res = sqlhelperex.executenonquery("delete donkey", CommandType.Text);
if (res > 0)
{
flag = true;
}
return flag;
}
/// <summary>
///dataset 选择所有数据
/// </summary>
/// <returns>返回dataset数据表</returns>
public DataSet selectallchar()
{
return sqlhelperex.datasetquery("select * from donkey", CommandType.Text);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataSet selectbyid(string id)
{
SqlParameter[] paras = new SqlParameter[] {
new SqlParameter("@id",id)
};
return sqlhelperex.datasetquerypara("select * from donkey where id=@id",paras,CommandType.Text);
}
/// <summary>
/// 按照id选择数据
/// </summary>
/// <param name="id">id</param>
/// <returns>返回一个数据表格</returns>
public DataTable datasetselectbyid(string id)
{
DataTable dt = new DataTable();
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter("@id",id)
};
dt = sqlhelperex.executequery("proc_selectbyid", paras, CommandType.StoredProcedure);
return dt;
}
}
}