温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:博客源代码(课程设计,3层架构)
当前文件:
MVCBlog/App_Code/LiuyanOperation.cs,打开代码结构图
MVCBlog/App_Code/LiuyanOperation.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Data.SqlClient; 5
using System.Collections.Generic; 6
/// <summary> 7
/// LiuyanOperation 的摘要说明 8
/// </summary> 9
public class LiuyanOperation 10
{ 11
private SqlConnection con = null; 12
private SqlCommand cmd = null; 13
14
public LiuyanOperation() 15
{ 16
// 17
// TODO: 在此处添加构造函数逻辑 18
// 19
this.con = ConDB.getConnection();//在构造函数中连接数据库! 20
} 21
public bool insert_ly(liuyan at)//插入留言函数! 22
{ 23
24
con.Open(); 25
cmd = new SqlCommand("insert into liuyantable(CNAME,LIUYAN_CONTENT) values(@author,@content)", con); 26
cmd.Parameters.Add(new SqlParameter("@content", at.content)); 27
cmd.Parameters.Add(new SqlParameter("@author", at.author)); 28
if (cmd.ExecuteNonQuery() > 0) 29
{ 30
con.Close(); 31
return true; 32
} 33
else 34
{ 35
con.Close(); 36
return false; 37
} 38
} 39
public bool update(liuyan ac) 40
{ 41
con.Open(); 42
cmd = new SqlCommand("update liuyantable set liuyan_huifu=@huifu where id=@aid", con); 43
cmd.Parameters.Add(new SqlParameter("@aid", ac.id)); 44
cmd.Parameters.Add(new SqlParameter("@huifu", ac.huifu)); 45
// cmd.Parameters.Add(new SqlParameter("@huifu_time", ac.huifutime)); 46
if (cmd.ExecuteNonQuery() > 0) 47
{ 48
con.Close(); 49
return true; 50
} 51
else 52
{ 53
con.Close(); 54
return false; 55
} 56
} 57
public List<liuyan> viewAll() 58
{ 59
List<liuyan> list = new List<liuyan>(); 60
con.Open(); 61
cmd = new SqlCommand("select * from liuyantable order by liuyan_time desc", con); 62
SqlDataReader sdr = cmd.ExecuteReader(); 63
liuyan ac; 64
while (sdr.Read()) 65
{ 66
ac = new liuyan(); 67
ac.id = sdr["ID"].ToString(); 68
69
ac.author = sdr["CNAME"].ToString(); 70
71
ac.content = sdr["liuyan_content"].ToString(); 72
73
ac.huifu = sdr["LIUYAN_HUIFU"].ToString(); 74
75
ac.huifutime = Convert.ToDateTime(sdr["LIUYAN_HUIFU_TIME"]); 76
ac.time = Convert.ToDateTime(sdr["LIUYAN_TIME"]); 77
78
list.Add(ac); 79
} 80
sdr.Close(); 81
con.Close(); 82
return list; 83
} 84
public liuyan getByAid(string aid)//获取文章内容 85
{ 86
con.Open(); 87
cmd = new SqlCommand("select * from liuyantable where id='" + aid + "'", con); 88
SqlDataReader dr = cmd.ExecuteReader(); 89
liuyan ac = null; 90
while (dr.Read()) 91
{ 92
ac = new liuyan(dr); 93
} 94
con.Close(); 95
return ac; 96
} 97
public bool delete(string aid) 98
{ 99
con.Open(); 100
cmd = new SqlCommand("delete from liuyantable where id='" + aid + "'", con); 101
if (cmd.ExecuteNonQuery() > 0) 102
{ 103
con.Close(); 104
return true; 105
} 106
else 107
{ 108
con.Close(); 109
return false; 110
} 111
112
} 113
114
} 115








