using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data.OleDb;
namespace model3
{
public partial class UpdateData : System.Web.UI.Page
{
//protected System.Web.UI.WebControls.TextBox TextBox1;
//protected System.Web.UI.WebControls.TextBox TextBox2;
//protected System.Web.UI.WebControls.Button Button1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ showDBData();}
}
private void showDBData()
{
string accessString = ConfigurationSettings.AppSettings["accessconnectionstring"].ToString() + Server.MapPath("MyFirstAccessDB.mdb");
OleDbConnection conn = new OleDbConnection(accessString);
string cmdText = "select * from MyDataTable";
OleDbCommand cmd = new OleDbCommand(cmdText,conn);
string dataString = "<br>MyID MyName MyType MyLength<br>";
try
{
conn.Open();
Response.Write("连接数据库成功");
OleDbDataReader reca = cmd.ExecuteReader();
while(reca.Read())
{
dataString += reca["MyID"].ToString() + " " + reca["MyName"].ToString() + " " + reca["MyType"].ToString() + " " + reca["MyLength"].ToString();
dataString+="<br>";
}
reca.Close();
Response.Write(dataString);
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
private void Button1_Click(object sender,EventArgs e)
{
if (TextBox1.Text.Trim().Length > 0 && TextBox2.Text.Trim().Length > 0)
{
UpdataDBData(TextBox1.Text.Trim(), TextBox2.Text.Trim());
showDBData();
}
}
private void UpdataDBData(String myid,String myname)
{
string accessString = ConfigurationSettings.AppSettings["accessconnectionstring"].ToString() + Server.MapPath("MyFirstAccessDB.mdb");
string cmdText = "UPDATE MyDataTable SET MyName='"+myname+"'WHERE MyID="+myid;
OleDbConnection conn = new OleDbConnection(accessString);
OleDbCommand cmd = new OleDbCommand(cmdText,conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
这是主页的代码, 数据库是access的很简单 相信你们高手大哥们会明白的
就是 单击按钮的时候更新不了 请高手赐教