asp.net底层公共方法
DBHeper:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace NewsDAL
{
public static class DBHeper
{
private static SqlConnection connection;
/// <summary>
/// 连接数据库
/// </summary><returns>返回 SqlConnection 对象</returns>
public static SqlConnection Connection
{
get {
string connectionstring=ConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
if(connection==null)
{
connection = new SqlConnection(connectionstring);
connection.Open();
}
else if (connection.State== System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if(connection.State==System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return DBHeper.connection;
}
}
//关闭数据库连接的方法
public static void CloesConnection()
{
try {
if(connection.State!=ConnectionState.Closed)
{
connection.Close();
}
}
catch(Exception e){
}
}
/// <summary>
/// 根据 SQL语句 查询所影响的行数
/// </summary>
/// <param name="sql"></param>
/// <returns>返回 int 类型</returns>
public static int ExecutCommand(string sql) {
try
{
SqlCommand cmd = new SqlCommand(sql,Connection);
int result = cmd.ExecuteNonQuery();
return result;
}catch(Exception e){
return 0;
}
}
/// <summary>
/// 根据 SQL语句、预编译数组 查询所影响的行数
/// </summary>
/// <param name="sql">参数 SQL 语句</param>
/// <param name="values">参数 预编译数组</param>
/// <returns>返回 int 类型</returns>
public static int ExecutCommand(string sql,params SqlParameter[] values) {
try
{
SqlCommand cmd = new SqlCommand(sql,Connection);
cmd.Parameters.Add(values);
int result = cmd.ExecuteNonQuery();
return result;
}
catch (Exception e)
{
return 0;
}
}
/// <summary>
/// 根据 SQL 语句查询得到的条数,执行查询,返回第一行第一列的值
/// </summary>
/// <param name="sql">参数 SQL 语句</param>
/// <returns>返回 int 类型</returns>
///
public static int GetScalar(string sql)
{
SqlCommand cmd = new SqlCommand(sql,Connection);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
/// <summary>
/// 根据 SQL语句、预编译数组 查询得到的条数,执行查询,返回第一行第一列的值
/// </summary>
/// <param name="sql">参数 SQL 语句</param>
/// <param name="values">参数 预编译数组</param>
/// <returns>返回 int 类型</returns>
public static int GetScalar(string sql,params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql,Connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());//cmd.ExecuteScalar()返回的是一个Ojbect类型的
return result;
}
/// <summary>
/// 根据 SQL语句 查询数据
/// </summary>
/// <param name="sql">参数 接受一个 SQL语句</param>
/// <returns>返回 DataTable 类型</returns>
///
public static DataTable GetDataSet(string sql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql,Connection);
SqlDataAdapter sda =new SqlDataAdapter(cmd);//sqlDataAdapter用于填充DataSet
sda.Fill(ds);//向DataTable中添加数据
return ds.Tables[0];//获得表的集合
}
/// <summary>
/// 根据 SQL语句、预编译数组 查询数据
/// </summary>
/// <param name="sql">参数 接受一个 SQL语句</param>
/// <param name="values">参数 接受一个 预编译数组</param>
/// <returns>返回 DataTable 类型</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 sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
return ds.Tables[0];
}
}
}
Service:
using System;
using System.Collections.Generic;
using System.Text;
using Newsentity;
using System.Data.SqlClient;
using System.Data;
using NewsDAL;
namespace NewsDAL
{
public class newsService
{
#region
/// <summary>
/// 得到所有信息
/// </summary>
/// <returns></returns>
///
#endregion
public static List<news> GetNews()
{
string sql = "SELECT * FROM NEWS";
DataTable tables = DBHeper.GetDataSet(sql);
List<news > list = new List<news>();
if (tables.Rows.Count > 0)
{
for (int i = 0; i < tables.Rows.Count; i++)
{
news n = new news();
n.Nid = Convert.ToInt32(tables.Rows[i]["nid"]);
n.Sname = Convert.ToString(tables.Rows[i]["sname"]);
n.Spass =Convert.ToString(tables.Rows[i]["spass"]);
n.Stype=Convert.ToString(tables.Rows[i]["stype"]);
n.Ncontent = Convert.ToString(tables.Rows[i]["ncontent"]);
list.Add(n);
}
return list;
}
else
{
return null;
}
}
//根据类型查询新闻
public static List<news> GetByNews(string type)
{
string sql=string.Format("SELECT * FROM NEWS WHERE STYPE=‘{0}‘",type);
DataTable tables = DBHeper.GetDataSet(sql);
List<news> list = new List<news>();
if (tables.Rows.Count > 0)
{
for (int i = 0; i < tables.Rows.Count; i++)
{
news n = new news();
n.Sname = Convert.ToString(tables.Rows[i]["sname"]);
n.Spass = Convert.ToString(tables.Rows[i]["spass"]);
n.Stype = Convert.ToString(tables.Rows[i]["stype"]);
n.Ncontent = Convert.ToString(tables.Rows[i]["ncontent"]);
list.Add(n);
}
return list;
}
else
{
return null;
}
}
//验证用户登陆
public static news GetNewstLogin(string name, string pass)
{
string sql = string.Format("SELECT * FROM NEWS WHERE SNAME=‘{0}‘ and SPASS=‘{1}‘", name, pass);
DataTable tables = DBHeper.GetDataSet(sql);
if (tables.Rows.Count != 0)
{
news n = new news();
n.Sname = Convert.ToString(tables.Rows[0]["sname"]);
n.Spass = Convert.ToString(tables.Rows[0]["spass"]);
n.Stype = Convert.ToString(tables.Rows[0]["stype"]);
n.Ncontent = Convert.ToString(tables.Rows[0]["ncontent"]);
return n;
}
else
{
return null;
}
}
//删除记录
public static int DelectNews(int id)
{
string sql = string.Format("DELETE FROM NEWS WHERE NID in (‘{0}‘)", id);
int result = DBHeper.ExecutCommand(sql);
return result;
}
//删除记录
public static int DelectNews(string id)
{
string sql = string.Format("DELETE FROM NEWS WHERE NID in ({0})", id);
int result = DBHeper.ExecutCommand(sql);
return result;
}
//根据id查询详细信息
public static List<news> GetNewsById(int id)
{
string sql = string.Format("SELECT * FROM NEWS WHERE NID={0}", id);
DataTable tables = DBHeper.GetDataSet(sql);
List<news > list = new List<news>();
if (tables.Rows.Count != 0)
{
news n = new news();
n.Sname = Convert.ToString(tables.Rows[0]["sname"]);
n.Spass = Convert.ToString(tables.Rows[0]["spass"]);
n.Stype = Convert.ToString(tables.Rows[0]["stype"]);
n.Ncontent = Convert.ToString(tables.Rows[0]["ncontent"]);
list.Add(n);
return list;
}
else
{
return null;
}
}
//添加信息
public static int AddNews(news n)
{
string sql = string.Format("insert into news(ncontent,spass,sname,stype) values(‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘)",n.Ncontent,n.Spass,n.Sname,n.Stype);
int result = DBHeper.ExecutCommand(sql);
if (result > 0)
{
return result;
}
else
{
return 0;
}
}
//修改信息
public static int UpdateNews(news n)
{
string sql = string.Format("UPDATE NEWS SET NCONTENT=‘{0}‘,SNAME=‘{1}‘,SPASS=‘{2}‘,STYPE=‘{3}‘ WHERE NID=‘{4}‘",n.Ncontent,n.Sname,n.Spass,n.Stype,n.Nid);
int result = DBHeper.ExecutCommand(sql);
if (result > 0)
{
return result;
}
else
{
return 0;
}
}
}
}
配置文件
<?xml version="1.0"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<appSettings/>
<connectionStrings>
<add name="conn" connectionString="uid=sa;pwd=123456;server=.\sqlexpress;database=NEWS"/>
</connectionStrings>
<system.web>
<httpModules>
<add type="Discuz.Forum.HttpModule, Discuz.Forum" name="HttpModule" />
</httpModules>
<!--
设置 compilation debug="true" 可将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="true" targetFramework="4.0">
</compilation>
<!--
通过 <authentication> 节可以配置 ASP.NET 用来
识别进入用户的
安全身份验证模式。
-->
<authentication mode="Windows"/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
</configuration>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。