目录
演示功能:
点击启动生成页面
点击搜索模糊查询
点击添加跳转新界面
编辑
点击Button添加
步骤:
1、建文件
编辑
2、添加引用关系
3、根据数据库中的列写Models下的XueshengModels类
4、DAL下的DBHelper(对数据库进行操作)
5、DAL数据访问层下的service文件
6、BLL业务逻辑层下调用DAL的文件
7、ui表现层主界面前端部分
8、ui表现层主界面后端部分
9、ui表现层添加界面前端部分
10、ui表现层添加界面后端部分
此处设置文本框多行
下图是三层架构列表,Models里面有模拟数据库中列的类,DAL中有DBHelper和service,BLL中有BllManager文件用于ui界面直接调用
建照片文件夹用于展示图片,数据库存地址
DAL引用Models文件,BLL引用DAL和Models文件,主文件WebApplication1引用Bll和Models
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Models { public class NewsModels { private string newsID; public string NewsID { get { return newsID; } set { newsID = value; } } private string newsTitle; public string NewsTitle { get { return newsTitle; } set { newsTitle = value; } } private string newsContent; public string NewsContent { get { return newsContent; } set { newsContent = value; } } private string type; public string Type { get { return type; } set { type = value; } } private string publisher; public string Publisher { get { return publisher; } set { publisher = value; } } private string pian; public string Pian { get { return pian; } set { pian = value; } } private string pubTime; public string PubTime { get { return pubTime; } set { pubTime = value; } } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace DAL
{
public class DBHelper
{
public static string connstr = "server=.;database=NewsDB;uid=sa;pwd=123123";
public static SqlConnection conn = null;
public static void Conncet() {
if (conn==null)
{
conn=new SqlConnection(connstr);
}
conn.Close();
conn.Open();
}
public static bool NoQuery(string sql) {
Conncet();
SqlCommand cmd = new SqlCommand(sql,conn);
int temp= cmd.ExecuteNonQuery();
conn.Close();
return temp > 0;
}
public static SqlDataReader Reader(string sql) {
Conncet();
SqlCommand cmd = new SqlCommand(sql, conn);
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace DAL
{
public class DalService
{
public static List Zhanshi() {
List list = new List();
string sql = "select * from News";
SqlDataReader read= DBHelper.Reader(sql);
while (read.Read())
{
Models.NewsModels model=new Models.NewsModels();
model.NewsID = read["NewsID"].ToString();
model.NewsTitle = read["NewsTitle"].ToString();
model.Pian = read["Pian"].ToString();
model.Publisher = read["Publisher"].ToString();
model.Type = read["Type"].ToString();
model.PubTime = read["PubTime"].ToString();
model.NewsContent = read["NewsContent"].ToString();
list.Add(model);
}
return list;
}
public static List Cha(string title)
{
List list = new List();
string sql = string.Format("select * from News where NewsTitle like '%{0}%'",title);
SqlDataReader read = DBHelper.Reader(sql);
while (read.Read())
{
Models.NewsModels model = new Models.NewsModels();
model.NewsID = read["NewsID"].ToString();
model.NewsTitle = read["NewsTitle"].ToString();
model.Pian = read["Pian"].ToString();
model.Publisher = read["Publisher"].ToString();
model.Type = read["Type"].ToString();
model.PubTime = read["PubTime"].ToString();
model.NewsContent = read["NewsContent"].ToString();
list.Add(model);
}
return list;
}
public static bool Jia(string title,string content,string type,string publisher){
string sql=string.Format("insert News values('{0}','{1}','{2}','{3}','Jellyfish.jpg',GETDATE())",title,content,type,publisher);
if (DBHelper.NoQuery(sql))
{
return true;
}
else
{
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class BllManager
{
public static List Zhanshi() {
return DAL.DalService.Zhanshi();
}
public static bool Jia(string title, string content, string type, string publisher) {
return DAL.DalService.Jia(title,content,type,publisher);
}
public static List Cha(string title) {
return DAL.DalService.Cha(title);
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Index.Index" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Index
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List list = BLL.BllManager.Zhanshi();
Repeater1.DataSource = list;
Repeater1.DataBind();
}
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
List list = BLL.BllManager.Cha(TextBox1.Text.ToString());
Repeater1.DataSource = list;
Repeater1.DataBind();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TianJia.aspx.cs" Inherits="Index.TianJia" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Index
{
public partial class TianJia : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (BLL.BllManager.Jia(TextBox1.Text.ToString(), TextBox2.Text.ToString(), TextBox3.Text.ToString(), TextBox4.Text.ToString()))
{
ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('成功添加');location.href='Index.aspx'",true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "fail", "alert('失败');", true);
};
}
}
}