您好,登錄后才能下訂單哦!
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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。