我想添加数据到accdb数据库文件,求指教
新人刚学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收集用户输入的信息和当前时间组成新的留言,但是无法更新数据库。
求帮助,我一次修改数据库成功的经验都没有,代码理解的也不深刻。