我想添加数据到accdb数据库文件,求指教

qq449224404 2012-12-02 02:44:53
新人刚学asp.net,想做个网页留言板。但是不会添加收集的信息到数据库。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" Visible="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="用户昵称" HeaderText="用户昵称" SortExpression="用户昵称" />
<asp:BoundField DataField="用户留言" HeaderText="用户留言" SortExpression="用户留言" />
<asp:BoundField DataField="留言时间" HeaderText="留言时间" SortExpression="留言时间" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\website\Leavemessage\Leavemessage.accdb" ProviderName="System.Data.OleDb" SelectCommand="SELECT * FROM [latestmessage] ORDER BY [留言时间]"></asp:SqlDataSource>
</div>
<div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label2" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label3" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label4" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label5" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label6" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label7" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label8" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label9" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label10" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label11" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label12" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label13" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label14" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label15" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label16" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label17" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label18" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label19" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="Label20" runat="server"></asp:Label>
</div>
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="19px" Width="69px">匿名游客</asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" Width="322px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
<div>
<asp:Label ID="message" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

cs代码部分:
using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class 留言板前台 : System.Web.UI.Page
{
public static string a, b;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = GridView1.Rows[0].Cells[1].Text + ":" + GridView1.Rows[0].Cells[2].Text;
Label2.Text = GridView1.Rows[0].Cells[3].Text;
Label3.Text = GridView1.Rows[1].Cells[1].Text + ":" + GridView1.Rows[1].Cells[2].Text;
Label4.Text = GridView1.Rows[1].Cells[3].Text;
Label5.Text = GridView1.Rows[2].Cells[1].Text + ":" + GridView1.Rows[2].Cells[2].Text;
Label6.Text = GridView1.Rows[2].Cells[3].Text;
Label7.Text = GridView1.Rows[3].Cells[1].Text + ":" + GridView1.Rows[3].Cells[2].Text;
Label8.Text = GridView1.Rows[3].Cells[3].Text;
Label9.Text = GridView1.Rows[4].Cells[1].Text + ":" + GridView1.Rows[4].Cells[2].Text;
Label10.Text = GridView1.Rows[4].Cells[3].Text;
Label11.Text = GridView1.Rows[5].Cells[1].Text + ":" + GridView1.Rows[5].Cells[2].Text;
Label12.Text = GridView1.Rows[5].Cells[3].Text;
Label13.Text = GridView1.Rows[6].Cells[1].Text + ":" + GridView1.Rows[6].Cells[2].Text;
Label14.Text = GridView1.Rows[6].Cells[3].Text;
Label15.Text = GridView1.Rows[7].Cells[1].Text + ":" + GridView1.Rows[7].Cells[2].Text;
Label16.Text = GridView1.Rows[7].Cells[3].Text;
Label17.Text = GridView1.Rows[8].Cells[1].Text + ":" + GridView1.Rows[8].Cells[2].Text;
Label18.Text = GridView1.Rows[8].Cells[3].Text;
Label19.Text = GridView1.Rows[9].Cells[1].Text + ":" + GridView1.Rows[9].Cells[2].Text;
Label20.Text = GridView1.Rows[9].Cells[3].Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
if ((TextBox1.Text != "") && (TextBox2.Text != ""))
{ a = TextBox1.Text; b = TextBox2.Text; }

string conn= "Provider=Microsoft.ace.oledb.12.0; Data Source=" + Server.MapPath("Leavemessage.accdb");
OleDbConnection db= new OleDbConnection(conn);
db.Open();
string sql = "insert into latestmessage(用户昵称,用户留言,留言时间) values('" + a + "','" + b + "','" + DateTime.Now + "')";
OleDbDataAdapter cmd= new OleDbDataAdapter(sql, conn);
db.Close();

}
}


通过不可见的Gridview调出数据库表格中的最近10条留言和对应留言时间,并显示成功。
2个TextBox收集用户输入的信息和当前时间组成新的留言,但是无法更新数据库。
求帮助,我一次修改数据库成功的经验都没有,代码理解的也不深刻。
...全文
978 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dalmeeme 2012-12-02
  • 打赏
  • 举报
回复
这样写: db.Open(); string sql = "insert into latestmessage(用户昵称,用户留言,留言时间) values('" + a + "','" + b + "','" + DateTime.Now + "')"; OleDbCommand cmd=new OleDbCommand(sql,db); cmd.ExecuteNonQuery(); db.Close();
qq449224404 2012-12-02
  • 打赏
  • 举报
回复
执行我写的aspx,载入后点击BUTTON没反应,嗯...明天来看有解答没
CSMSDN 2012-12-02
  • 打赏
  • 举报
回复
如果更新没成功,说明没有写入数据库,你打开数据库看一下,数据写入没
qq449224404 2012-12-02
  • 打赏
  • 举报
回复
我渣渣计算机应用技术专业的,大三发的ASP.Net网页制作的书有用的东西很少,将的都是控件的使用而且还是很久以前的东西,才发现这个网站。

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧