此文章出处来源于网络,本人稍作修改

 

首先在新建项目里面建立Linq To Sql 类dbml,将数据库连接到VS上面

然后将要使用的表,直接拖到dbml类上面,保存

这样会生成LinqToSql所需的实体类

新建数据表:

客户端留言显示效果:

后台管理留言效果:

前台添加

前台页面:

 1    <div>
 2 
 3         姓名
 4 
 5         <asp:TextBox ID="tb_UserName" runat="server"></asp:TextBox><br />
 6 
 7         <br />
 8 
 9         留言
10 
11         <asp:TextBox ID="tb_Message" runat="server" Height="100px" TextMode="MultiLine" Width="300px"></asp:TextBox><br />
12 
13         <br />
14 
15         <asp:Button ID="btn_SendMessage" runat="server" Text="发表留言" OnClick="btn_SendMessage_Click" /><br />
16 
17         <br />
18 
19         <asp:Repeater ID="rpt_Message" runat="server">
20 
21         <ItemTemplate>
22 
23         <table width="600px" style="border:solid 1px #666666; font-size:10pt; background-color:#f0f0f0">
24 
25             <tr>
26 
27             <td align="left" width="400px">
28 
29             <%# Eval("Message")%>
30 
31             </td>
32 
33             <td align="right" width="200px">
34 
35             <%# Eval("PostTime")%> - <%# Eval("UserName")%>
36 
37             </td>
38 
39             </tr>
40 
41             <tr>
42 
43             <td colspan="2" align="right">
44 
45             <hr width="300px" />
46 
47             管理员回复:<%# Eval("IsReplied").ToString() == "False" ? "暂无" : Eval("Reply")%>
48 
49             </td>
50 
51             </tr>           
52 
53         </table>
54 
55         <br/>
56 
57         </ItemTemplate>
58 
59         </asp:Repeater>
60 
61     </div>

后台代码:

 1    public partial class SendMessage : System.Web.UI.Page
 2     {
 3         GuestBookDataContext ctx = new GuestBookDataContext("server=HAPPY-PC;database=XinShiDai4.13;uid=sa;pwd=happy");
 4         protected void Page_Load(object sender, EventArgs e)
 5         {
 6             if (!Page.IsPostBack)
 7             {
 8                 SetBind();
 9             }
10         }
11         /// <summary>
12         /// 添加
13         /// </summary>
14         /// <param name="sender"></param>
15         /// <param name="e"></param>
16         protected void btn_SendMessage_Click(object sender, EventArgs e)
17         {
18             tbGuestBook gb = new tbGuestBook();
19 
20             gb.ID = Guid.NewGuid().ToString();
21 
22             gb.UserName = tb_UserName.Text;
23 
24             gb.Message = tb_Message.Text;
25 
26             gb.IsReplied = false;
27 
28             gb.PostTime = DateTime.Now;
29 
30             //调用插入方法,写入数据库
31             ctx.tbGuestBook.InsertOnSubmit(gb);
32 
33             ctx.SubmitChanges();
34 
35 
36             SetBind();
37 
38 
39         }
40         private void SetBind()
41         {
42 
43             rpt_Message.DataSource = from gb in ctx.tbGuestBook orderby gb.PostTime descending select gb;
44 
45             rpt_Message.DataBind();
46 
47 
48         }
49 
50     }

=================================================================================================

后台删除和修改:

注:有可能会有更新冲突,解决方法,将出主键之外的字段明上面添加 UpdateCheck = UpdateCheck.Never

前台:

 1     <div>
 2 
 3         <asp:Repeater ID="rpt_Message" runat="server" OnItemCommand="rpt_Message_ItemCommand">
 4 
 5         <ItemTemplate>
 6 
 7         <table width="600px" style="border:solid 1px #666666; font-size:10pt; background-color:#f0f0f0">
 8 
 9             <tr>
10 
11             <td align="left" width="400px">
12 
13             <%# Eval("Message")%>
14 
15             </td>
16 
17             <td align="right" width="200px">
18 
19             <%# Eval("PostTime")%> - <%# Eval("UserName")%>
20 
21             </td>
22 
23             </tr>
24 
25             <tr>
26 
27             <td colspan="2" align="right">
28 
29             <hr width="300px" />
30 
31             <asp:Button ID="btn_DeleteMessage" runat="server" Text="删除留言" CommandName="DeleteMessage" CommandArgument='<%# Eval("ID")%>'/>
32 
33             管理员回复:<asp:TextBox runat="server" ID="tb_Reply" TextMode="MultiLine" Width="300px" Text='<%# Eval("Reply")%>'/>
34 
35             <asp:Button ID="btn_SendReply" runat="server" Text="发表回复" CommandName="SendReply" CommandArgument='<%# Eval("ID")%>'/>
36 
37             </td>
38 
39             </tr>           
40 
41         </table>
42 
43         <br/>
44 
45         </ItemTemplate>
46 
47         </asp:Repeater>
48 
49     </div>

后台:

 1  public partial class Admin : System.Web.UI.Page
 2     {
 3         GuestBookDataContext ctx = new GuestBookDataContext("server=HAPPY-PC;database=XinShiDai4.13;uid=sa;pwd=happy");
 4         protected void Page_Load(object sender, EventArgs e)
 5         {
 6             if (!Page.IsPostBack)
 7             {
 8                 SetBind();
 9             }
10         }
11         /// <summary>
12         /// 绑定
13         /// </summary>
14         private void SetBind()
15         {
16 
17             rpt_Message.DataSource = from gb in ctx.tbGuestBook orderby gb.PostTime descending select gb;
18 
19             rpt_Message.DataBind();
20 
21         }
22         /// <summary>
23         /// 删除
24         /// </summary>
25         /// <param name="source"></param>
26         /// <param name="e"></param>
27         protected void rpt_Message_ItemCommand(object source, RepeaterCommandEventArgs e)
28         {
29 
30             if (e.CommandName == "DeleteMessage")
31             {
32 
33                 StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);
34 
35                 ctx.Log = sw;
36                 //查询当前Id的表对象
37                 tbGuestBook gb = ctx.tbGuestBook.Single(b => b.ID == new Guid(e.CommandArgument.ToString()).ToString());
38                 //调用删除对象
39                 ctx.tbGuestBook.DeleteOnSubmit(gb);
40 
41                 //提交更改(必须调用此方法)
42                 ctx.SubmitChanges();
43 
44 
45                 SetBind();
46 
47                 sw.Close();
48 
49             }
50             //更新
51             if (e.CommandName == "SendReply")
52             {
53 
54                 StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);
55 
56                 ctx.Log = sw;
57                 //查询要更新的对象
58                 tbGuestBook gb = ctx.tbGuestBook.Single(b => b.ID == new Guid(e.CommandArgument.ToString()).ToString());
59                 //赋值
60                 gb.Reply = ((TextBox)e.Item.FindControl("tb_Reply")).Text;
61 
62                 gb.IsReplied = true;
63                 //提交更新
64                 ctx.SubmitChanges();
65 
66                 SetBind();
67 
68                 sw.Close();
69 
70             }
71 
72         }
73     }

 

posted on 2012-04-18 18:50  高兴happy  阅读(542)  评论(0编辑  收藏  举报