一、打开 Microsoft Visual Studio 2010软件
二、新建一个C#空网站
三、选中项目名(jinlanyan),右键----》添加新项(index.aspx)
四、添加App_Code文件夹,右键项目名(jinlanyan)---》添加asp.net文件夹---》App_Code
五、在App_Code中添加三个普通的文件夹,右键App_Code,选择新建文件夹
六、右键BLL,选择添加新项,依次添加三个类(SubjectBLL.cs、SubjectDAL.cs和SubjectModel.cs),其中SqlHelper.cs为数据库访问类,代码后面给出。
附逻辑说明:index页面调用bll,bll再调用dal,再按顺序返回结果。
七、数据表见下图
八、运行截图
九、各页面代码
index.aspx页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
index.aspx.cs页面代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class index : System.Web.UI.Page
{
SubjectBLL sb = new SubjectBLL();
SubjectModel sm = new SubjectModel();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)//查询
{
sm.NewsId1 = Convert.ToInt32(TextBox1.Text); //查询框里输入的Text
DataSet ds = sb.select(sm);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)//插入
{
//s.Id1 = Convert.ToInt32(TextBox5.Text);//Id的值是自增的。
sm.NewsId1 = Convert.ToInt32(TextBox2.Text);
sm.FileTitle1 =TextBox3.Text;
sm.FilePath1 = TextBox4.Text;
if (sb.insert(sm) == 1)
{
Button2.Text = "添加成功!";
}
else
Button2.Text = "添加失败!";
}
protected void Subject_RowDeleting1(object sender, GridViewDeleteEventArgs e)//删除
{
sm.NewsId1 = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[2].Text);
int i = sb.delete(sm);
if (i == 1)
{
Button2.Text = "成功!";
}
}
protected void Subject_RowUpdating(object sender, GridViewUpdateEventArgs e)//更新
{
sm.Id1 = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text);
sm.NewsId1 = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text);
sm.FileTitle1 =((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text;
sm.FilePath1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text;
if (sb.update(sm) == 1)
{
GridView1.EditIndex = -1;
Button1_Click(sender, e);
Button2.Text = "成功!";
}
}
protected void Subject_RowEditing(object sender, GridViewEditEventArgs e)//编辑
{
GridView1.EditIndex = e.NewEditIndex;
Button1_Click(sender, e);//查询
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Button1_Click(sender, e);//查询
}
}
web.config代码
SqlHelper.cs代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;
///
///SqlHelper 的摘要说明
///
public class SqlHelper
{
public SqlHelper()
{
}
public static DataSet ExecuteSelect(string sql)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["sqljw"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
try
{
da.Fill(ds);
return ds;
}
catch (Exception ex)//Exception ex
{
return null;
}
}
///
/// insert delete update
///
///
///
public static int ExecuteNonQuery(string sql)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Sqljw"].ConnectionString);
SqlCommand comm = new SqlCommand(sql, conn);
try
{
conn.Open();
return comm.ExecuteNonQuery();
}
catch (Exception ex)
{
return -1;
}
finally
{
conn.Close();
}
}
///
/// select ,返回第一行,第一列
///
///
///
public static object ExecuteScale(string sql)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Sqljw"].ConnectionString);
SqlCommand comm = new SqlCommand(sql, conn);
try
{
conn.Open();
return comm.ExecuteScalar();
}
catch (Exception ex)
{
return null;
}
finally
{
conn.Close();
}
}
}
SubjectBLL.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
///
///SubjectBLL 的摘要说明
///
public class SubjectBLL
{
public SubjectBLL()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public DataSet select(SubjectModel s)
{
SubjectDAL sd = new SubjectDAL();
DataSet ds = sd.select(s);
if (ds != null)
return ds;
else
return null;
}
public int delete(SubjectModel s)
{
SubjectDAL sd = new SubjectDAL();
int j = sd.delete(s);
if (j == 1)
return 1;
else
return -1;
}
public int insert(SubjectModel s)
{
SubjectDAL sd = new SubjectDAL();
int j = sd.insert(s);
if (j == 1)
return 1;
else
return -1;
}
public int update(SubjectModel s)
{
SubjectDAL sd = new SubjectDAL();
int j = sd.update(s);
if (j == 1)
return 1;
else
return -1;
}
}
SubjectDAL.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
///
///SubjectDAL 的摘要说明
///
public class SubjectDAL
{
public SubjectDAL()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public DataSet select(SubjectModel d)
{
string sql = "select * from NewsUploadFile where NewsId='{0}'";
sql = string.Format(sql, d.NewsId1);
return SqlHelper.ExecuteSelect(sql);
}
public int delete(SubjectModel d)
{
string sql = "delete NewsUploadFile where Id={0} ";
sql = string.Format(sql, d.NewsId1);
return SqlHelper.ExecuteNonQuery(sql);
}
public int insert(SubjectModel d)
{
string sql = "insert into NewsUploadFile (NewsId,FileTitle,FilePath) values ('{0}','{1}','{2}') ";
sql = string.Format(sql,d.NewsId1, d.FileTitle1, d.FilePath1);
return SqlHelper.ExecuteNonQuery(sql);
}
public int update(SubjectModel d)
{
string sql = "update NewsUploadFile set NewsId='{0}',FileTitle='{1}',FilePath='{2}' where Id='{3}'";
sql = string.Format(sql, d.NewsId1, d.FileTitle1, d.FilePath1,d.Id1);
return SqlHelper.ExecuteNonQuery(sql);
}
}
SubjectModel.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
///
///SubjectModel 的摘要说明
///
public class SubjectModel
{
public SubjectModel()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
private int Id;//字段
public int Id1 //属性、方法
{
get { return Id; }
set { Id = value; }
}
private int NewsId;
public int NewsId1
{
get { return NewsId; }
set { NewsId = value; }
}
private string FileTitle;
public string FileTitle1
{
get { return FileTitle; }
set { FileTitle = value; }
}
private string FilePath;
public string FilePath1
{
get { return FilePath; }
set { FilePath = value; }
}
}