ado.net实现一个通知公告功能

一、建立相应的数据库,此处就不多说了,相信大家都非常了解

二、开始建立连接,建立连接首先需要有一个连接字符串,用来存储连接相关的信息,比如你连的是哪里的数据库呀,什么数据库呀,数据库名字,以及数据库用户名,密码之类的呀等等!这个链接字符串呢,一般都是直接放在配置文件(Web.config)中的,如下

然后就需要在程序中读取那个链接字符串并创建链接了:

三、链接建立后就是打开那个链接,然后执行数据库的一些操作,然后在关闭链接,具体业务操作也不同,这里把实现一个通知公告功能的数据库操作代码粘贴一下,仅供参考:

 1 /*
 2  * 创建人:
 3  * 创建时间:2015-07-01
 4  * 说明:数据库的助手类
 5  * */
 6 using System;
 7 using System.Collections.Generic;
 8 using System.Linq;
 9 using System.Text;
10 using System.Data;
11 using System.Data.SqlClient;
12 using System.Configuration;
13 
14 namespace DAL
15 {  
16 
17     public class SQLHelp
18     {
19         private SqlConnection conn = null;
20         private SqlCommand cmd = null;
21         private SqlDataReader sdr = null;
22         public SQLHelp()
23         {
24             string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
25             conn = new SqlConnection(connStr);
26 
27         }
28         //SqlConnection conn = new SqlConnection(connStr);
29 
30         /// <summary>
31         /// 该方法实现对数据库的增删改。
32         /// </summary>
33         /// <param name="sql">对数据库的增删改sql语句</param>
34         /// <returns></returns>
35         public int ExecuteNonQuery(string sql)
36         {
37             conn.Open();           
38             cmd = new SqlCommand(sql,conn);
39             int res = cmd.ExecuteNonQuery();
40             conn.Close();
41             return res;            
42         }
43         /// <summary>
44         /// 执行查询的方法
45         /// </summary>
46         /// <param name="sql">执行查询的sql语句</param>
47         /// <returns></returns>
48         public  DataTable ExecuteQuery(string sql)
49         {
50             DataTable dt = new DataTable();        
51             conn.Open();
52             cmd = new SqlCommand(sql, conn);
53             sdr = cmd.ExecuteReader();
54             dt.Load(sdr);
55             //sdr.Close;         
56             conn.Close();
57             return dt;
58         }
59     }
60     
61 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Data;
 6 using System.Data.SqlClient;
 7 namespace DAL
 8 {
 9     /// <summary>
10     /// 公告管理数据库操作类
11     /// </summary>
12     public class NoticeDAO
13     {
14       /// <summary>
15       /// 根据公告id删除公告信息
16       /// </summary>
17       /// <param name="NoticeId">公告id</param>
18         public static void deleteNotice(string NoticeId)
19         {
20             SQLHelp SQLHelp = new SQLHelp();
21             string sql = "delete from T_notice where NoticeId='" + NoticeId + "'";
22             int i = SQLHelp.ExecuteNonQuery(sql);
23             //return i;
24         }
25         /// <summary>
26         /// 查询所有公告内容
27         /// </summary>
28         /// <returns>返回一个datatable类型的值</returns>
29         public static DataTable selectAll()
30         {
31             SQLHelp SQLHelp = new SQLHelp();
32             string sql = "select * from T_notice";
33             DataTable dt = SQLHelp.ExecuteQuery(sql);
34             return dt;
35         }
36         /// <summary>
37         /// 更新公告内容
38         /// </summary>
39         /// <param name="title">公告标题</param>
40         /// <param name="content">公告内容</param>
41         /// <param name="time">发布时间,默认为当前时间</param>
42         /// <param name="keyword">公告id</param>
43         public static void UpdateNotice(string title, string content, string time,string keyword)
44         {
45             string retitle = title.Replace("'", "''");//为了避免用户输入单引号的时候报错
46             title = retitle;
47             string recontent = content.Replace("'", "''");
48             content = recontent;
49             SQLHelp SQLHelp = new SQLHelp();
50             string sql = "update T_notice set NoticeTitle='"
51                 + title + "',NoticeContent='"
52                 + content + "',NoticeTime='"
53                 + time + "' where NoticeId='"
54                 + keyword + "'";
55             int i = SQLHelp.ExecuteNonQuery(sql);
56 
57         }
58 
59         /// <summary>
60         /// 插入公告内容
61         /// </summary>
62         /// <param name="title">公告标题</param>
63         /// <param name="content">公告内容</param>
64         /// <returns></returns> 
65         public static int InsertNotice(string title, string content)
66         {
67             string retitle=title.Replace("'", "''");//为了避免用户输入单引号的时候报错
68             title = retitle;
69             string recontent = content.Replace("'", "''");
70             content = recontent;
71             string sqlstr = @"insert into T_notice(NoticeTitle,NoticeContent,UserId)values('"+title+"','"+content+"','1')";         
72             SQLHelp SQLHelp = new SQLHelp();
73             int i = SQLHelp.ExecuteNonQuery(sqlstr);
74             return i;
75         }
76     }
77 }

四、页面的话,使用的是一个gridview来实现的,这里说一下从后台绑定数据:

五、分页功能的话本文暂时不做分享,谢谢

posted on 2015-07-27 16:13  喜狗  阅读(658)  评论(0编辑  收藏  举报