Linq to SQL 简单的增删改操作。
新建数据库表tbGuestBook.结构如下:
新建web项目,完成相应的dbml文件。留言页面布局如下
<body>
<form id="form1" runat="server">
<asp:TextBox ID="tb_UserName" runat="server"></asp:TextBox>
姓名<br />
<asp:TextBox ID="tb_Message" runat="server" Rows="6" TextMode="MultiLine"></asp:TextBox>
留言<br />
<asp:Button ID="btn_SendMessage" runat="server" Text="发表留言"
onclick="btn_SendMessage_Click" />
<div>
<asp:Repeater ID="rpt_Message" runat="server"
onitemcommand="rpt_Message_ItemCommand">
<ItemTemplate>
<table width="600px" style="border: solid 1px #666666; font-size: 10pt; background-color: #f0f0f0">
<tr>
<td align="left" width="400px">
<%# Eval("Message")%>
</td>
<td align="right" width="200px">
<%# Eval("PostTime")%>
-
<%# Eval("UserName")%>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<hr width="300px" />
管理员回复:<%# Eval("IsReplied").ToString() == "False" ? "暂无" : Eval("Reply")%>
</td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
后台代码实现:
DataGuestBookDataContext content = new DataGuestBookDataContext("Data Source=10.1.0.1;Initial Catalog=app;Persist Security Info=True;User ID=sa;Password=XXX");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindRepter();
}
}
private void bindRepter()
{
Table<tbGuestBook> tb = content.GetTable<tbGuestBook>();
this.rpt_Message.DataSource = from s in tb
orderby s.PostTime descending
select s;
this.rpt_Message.DataBind();
}
protected void rpt_Message_ItemCommand(object source, RepeaterCommandEventArgs e)
{}
protected void btn_SendMessage_Click(object sender, EventArgs e)
{
tbGuestBook book = new tbGuestBook();
book.ID = Guid.NewGuid().ToString();
book.UserName = this.tb_UserName.Text;
book.PostTime = DateTime.Now;
book.Message = this.tb_Message.Text;
book.IsReplied = false;
//以下:插入数据到库表!
content.tbGuestBook.InsertOnSubmit(book);
content.SubmitChanges();
bindRepter();
}
新建管理员删除留言和回复留言的页面,布局如下:
<div>
<asp:Repeater ID="rpt_Message" runat="server" OnItemCommand="rpt_Message_ItemCommand">
<ItemTemplate>
<table width="600px" style="border: solid 1px #666666; font-size: 10pt; background-color: #f0f0f0">
<tr>
<td align="left" width="400px">
<%# Eval("Message")%>
</td>
<td align="right" width="200px">
<%# Eval("PostTime")%>
-
<%# Eval("UserName")%>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<hr width="300px" />
<asp:Button ID="btn_DeleteMessage" runat="server" Text="删除留言" CommandName="DeleteMessage"
CommandArgument='<%# Eval("ID")%>' />
管理员回复:<asp:TextBox runat="server" ID="tb_Reply" TextMode="MultiLine" Width="300px"
Text='<%# Eval("Reply")%>' />
<asp:Button ID="btn_SendReply" runat="server" Text="发表回复" CommandName="SendReply"
CommandArgument='<%# Eval("ID")%>' />
</td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:Repeater>
</div>
管理员后台实现,主要区别在于repeater行项目的操控,其他一样:
protected void rpt_Message_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "DeleteMessage")
{
StreamWriter sw = new StreamWriter(Server.MapPath("log_delete.txt"), true);
content.Log = sw;
tbGuestBook del_book = content.tbGuestBook.Single(p => p.ID == e.CommandArgument.ToString());
content.tbGuestBook.DeleteOnSubmit(del_book);
content.SubmitChanges();
setbind();
sw.Close();
}
else if (e.CommandName == "SendReply")
{
StreamWriter sw = new StreamWriter(Server.MapPath("log_update.txt"), true);
content.Log = sw;
tbGuestBook up_book = content.tbGuestBook.Single(p => p.ID == e.CommandArgument.ToString());
up_book.Reply = ((TextBox)e.Item.FindControl("tb_Reply")).Text;
up_book.IsReplied = true;
content.SubmitChanges();
setbind();
sw.Close();
}
}