温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:博客源代码(课程设计,3层架构)
当前文件:
MVCBlog/App_Code/ArticleOperate.cs,打开代码结构图
MVCBlog/App_Code/ArticleOperate.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Data.SqlClient; 5
using System.Collections.Generic; 6
using System.Data.OleDb; 7
8
public class ArticleOperate 9
{ 10
private SqlConnection con = null; 11
private SqlCommand cmd = null; 12
13
public ArticleOperate() 14
{ 15
this.con = ConDB.getConnection();//在构造函数中连接数据库! 16
} 17
18
public bool insert(Article at)//插入函数! 19
{ 20
21
con.Open(); 22
cmd = new SqlCommand("insert into newstable(title,leibie,content,author) values(@title,@leibie,@content,@author)", con); 23
cmd.Parameters.Add(new SqlParameter("@title", at.title)); 24
cmd.Parameters.Add(new SqlParameter("@content", at.content)); 25
cmd.Parameters.Add(new SqlParameter("@leibie", at.liebie)); 26
cmd.Parameters.Add(new SqlParameter("@author", at.author)); 27
if (cmd.ExecuteNonQuery() > 0) 28
{ 29
con.Close(); 30
return true; 31
} 32
else 33
{ 34
con.Close(); 35
return false; 36
} 37
} 38
39
public Article getByAid(string aid)//获取文章内容 40
{ 41
con.Open(); 42
cmd = new SqlCommand("select * from newstable where id='"+aid+"'", con); 43
SqlDataReader dr = cmd.ExecuteReader(); 44
Article ac = null; 45
while (dr.Read()) 46
{ 47
ac = new Article(dr); 48
} 49
con.Close(); 50
return ac; 51
} 52
53
public bool delete(string aid) 54
{ 55
con.Open(); 56
cmd = new SqlCommand("delete from newstable where id='"+aid+"'", con); 57
if (cmd.ExecuteNonQuery() >= 0) 58
{ 59
con.Close(); 60
return true; 61
} 62
else 63
{ 64
con.Close(); 65
return false; 66
} 67
} 68
69
70
public List<Article> viewAll() 71
{ 72
List<Article> list = new List<Article>(); 73
con.Open(); 74
cmd = new SqlCommand("select * from newstable order by time desc", con); 75
SqlDataReader sdr = cmd.ExecuteReader(); 76
Article ac; 77
Leibie lb = null; 78
LeibieOperater lbo = null; 79
while (sdr.Read()) 80
{ 81
lb = new Leibie(); 82
lbo = new LeibieOperater(); 83
ac = new Article(); 84
ac.id= sdr["id"].ToString(); 85
ac.liebie =Convert.ToInt32(sdr["leibie"]); 86
int i = ac.liebie; 87
lb = lbo.getbyid(i); 88
ac.leibiestr = lb.leibie.ToString(); 89
ac.title = sdr["title"].ToString(); 90
ac.content = sdr["content"].ToString(); 91
if (ac.content.Length > 200) 92
{ 93
ac.content = ac.content.Substring(0, 200); 94
} 95
ac.postdatetime = Convert.ToDateTime(sdr["time"]); 96
ac.gentieCount = Convert.ToInt32(sdr["GentieCount"]); 97
ac.readcount = Convert.ToInt32(sdr["times"]);//读了多少次 98
ac.author = sdr["author"].ToString(); 99
list.Add(ac); 100
} 101
sdr.Close(); 102
con.Close(); 103
return list; 104
} 105
106
public List<Article> viewAllByleibie(int leibie) 107
{ 108
List<Article> list = new List<Article>(); 109
con.Open(); 110
cmd = new SqlCommand("select * from newstable where [leibie]="+leibie+"order by time desc", con); 111
SqlDataReader sdr = cmd.ExecuteReader(); 112
Article ac; 113
Leibie lb = null; 114
LeibieOperater lbo = null; 115
while (sdr.Read()) 116
{ 117
lb = new Leibie(); 118
lbo = new LeibieOperater(); 119
ac = new Article(); 120
ac.id = sdr["id"].ToString(); 121
ac.liebie = Convert.ToInt32(sdr["leibie"]); 122
int i = ac.liebie; 123
lb = lbo.getbyid(i); 124
ac.leibiestr = lb.leibie.ToString(); 125
ac.title = sdr["title"].ToString(); 126
ac.content = sdr["content"].ToString(); 127
if (ac.content.Length > 200) 128
{ 129
ac.content = ac.content.Substring(0, 200); 130
} 131
ac.postdatetime = Convert.ToDateTime(sdr["time"]); 132
ac.gentieCount = Convert.ToInt32(sdr["GentieCount"]); 133
ac.readcount = Convert.ToInt32(sdr["times"]);//读了多少次 134
ac.author = sdr["author"].ToString(); 135
list.Add(ac); 136
} 137
sdr.Close(); 138
con.Close(); 139
return list; 140
} 141
/* 142
public List<Article> viewList() 143
{ 144
List<Article> list = new List<Article>(); 145
con.Open(); 146
cmd = new SqlCommand("select aid,title,posttime,countcomment from bob_article_view order by posttime desc", con); 147
SqlDataReader sdr = cmd.ExecuteReader(); 148
Article ac; 149
while (sdr.Read()) 150
{ 151
ac = new Article(); 152
ac.aid = Convert.ToInt32(sdr["aid"]); 153
ac.title = sdr["title"].ToString(); 154
ac.posttime = Convert.ToDateTime(sdr["posttime"]); 155
ac.countcomment = Convert.ToInt32(sdr["countcomment"]); 156
list.Add(ac); 157
} 158
sdr.Close(); 159
con.Close(); 160
return list; 161
} 162
*/ 163
//查询! 164
public List<Article> search(string s) 165
{ 166
List<Article> list = new List<Article>(); 167
con.Open(); 168
cmd = new SqlCommand("select * from newstable where title like '%"+s+"%' order by time desc", con); 169
SqlDataReader sdr = cmd.ExecuteReader(); 170
Article ac; 171
while (sdr.Read()) 172
{ 173
ac = new Article(); 174
ac.id = sdr["id"].ToString(); 175
ac.liebie =Convert.ToInt32(sdr["leibie"]); 176
ac.title = sdr["title"].ToString(); 177
ac.postdatetime = Convert.ToDateTime(sdr["time"]); 178
ac.gentieCount = Convert.ToInt32(sdr["GentieCount"]); 179
ac.readcount = Convert.ToInt32(sdr["times"]);//读了多少次 180
ac.author = sdr["author"].ToString(); 181
list.Add(ac); 182
} 183
sdr.Close(); 184
con.Close(); 185
return list; 186
} 187
//更新,包括 标题,内容,类别, 188
public bool update(Article ac) 189
{ 190
con.Open(); 191
cmd = new SqlCommand("update newstable set title=@title,content=@content,leibie=@leibie,times=@readcount ,gentiecount=@gentiecount where id=@aid", con); 192
cmd.Parameters.Add(new SqlParameter("@leibie", ac.liebie)); 193
cmd.Parameters.Add(new SqlParameter("@title", ac.title)); 194
cmd.Parameters.Add(new SqlParameter("@content", ac.content)); 195
cmd.Parameters.Add(new SqlParameter("@aid", ac.id)); 196
cmd.Parameters.Add(new SqlParameter("@readcount", ac.readcount)); 197
cmd.Parameters.Add(new SqlParameter("@gentiecount", ac.gentieCount)); 198
if (cmd.ExecuteNonQuery() > 0) 199
{ 200
con.Close(); 201
return true; 202
} 203
else 204
{ 205
con.Close(); 206
return false; 207
} 208
} 209
} 210






}