初学者的问题??请求帮忙。。。多谢!
本人做了个修改和删除的功能模块:其源代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace IRM.Admin
{
/// <summary>
/// history 的摘要说明。
/// </summary>
public class history : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox txt_mgid;
protected System.Web.UI.WebControls.TextBox txt_title;
protected System.Web.UI.WebControls.TextBox txt_memo;
protected System.Web.UI.WebControls.TextBox txt_adddate;
protected System.Web.UI.WebControls.TextBox txt_enddate;
//公共变量,数据集保存在会话中
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter;
SqlCommandBuilder commandBuilder;
protected System.Web.UI.WebControls.DataGrid grid1;
DataSet dataSet;
public void Bind()
{
grid1.DataSource=dataSet;
grid1.DataBind();
}
protected void PrepareConnection()
{
string dataSource="Data Source=localhost;";
string security="user id=irm;password=irm;";
string initialCatalog="initial catalog=irm;";
string cnnString=dataSource + security + initialCatalog;
connection=new SqlConnection(cnnString);
string strSql="select mg_id,title,memo,adddate,enddate from notes where datediff(day,getdate(),enddate)<=0";
command=new SqlCommand(strSql,connection);
adapter=new SqlDataAdapter();
adapter.SelectCommand=command;
commandBuilder=new SqlCommandBuilder(adapter);
adapter.UpdateCommand=commandBuilder.GetUpdateCommand();
adapter.DeleteCommand=commandBuilder.GetDeleteCommand();
adapter.InsertCommand=commandBuilder.GetInsertCommand();
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
PrepareConnection();
dataSet=new DataSet();
try
{
connection.Open();
adapter.FillSchema(dataSet,SchemaType.Mapped);
adapter.Fill(dataSet);
}
catch(SqlException ee)
{
Response.Write(ee.ToString());
return;
}
finally
{
connection.Close();
}
Session["dataSet"]=dataSet;
Bind();
}
dataSet=(DataSet)Session["dataSet"];
}
protected void OnClick(object sender,EventArgs e)
{
string mg_id=txt_mgid.Text;
DataRow row=dataSet.Tables[0].Rows.Find(mg_id);
row["title"]=txt_title.Text;
row["memo"]=txt_memo.Text;
row["adddate"]=txt_adddate.Text;
row["enddate"]=txt_enddate.Text;
//更新数据库
PrepareConnection();
try
{
connection.Open();
adapter.Update(dataSet.Tables[0]);
}
catch(SqlException ex)
{
Response.Write(ex.ToString());
return;
}
finally
{
connection.Close();
}
dataSet.Tables[0].AcceptChanges();
Session["dataSet"]=dataSet;
Table1.Visible=false;
Bind();
}
public void OnItemCommand(object sender,DataGridCommandEventArgs e)
{
if(((Button)e.CommandSource).CommandName=="Modify")
{
Table1.Visible=true;
txt_mgid.Text=e.Item.Cells[0].Text;
txt_title.Text=e.Item.Cells[1].Text;
txt_memo.Text=e.Item.Cells[2].Text;
txt_adddate.Text=e.Item.Cells[3].Text;
txt_enddate.Text=e.Item.Cells[4].Text;
}
else if(((Button)e.CommandSource).CommandName=="Delete")
{
Table1.Visible=false;
string mg_id=e.Item.Cells[0].Text;
DataRow row=dataSet.Tables[0].Rows.Find(mg_id);
row.Delete();
PrepareConnection();
try
{
connection.Open();
adapter.Update(dataSet.Tables[0]);
}
catch(SqlException ex)
{
Response.Write(ex.ToString());
return;
}
finally
{
connection.Close();
}
dataSet.Tables[0].AcceptChanges();
Table1.Visible=true;
Session["dataSet"]=dataSet;
Bind();
}
}
代码运行正常,由于需要,现在不用Session的方法了,直接操作数据库,进行修改和删除。。。请问如何实现???还请各位指点!!!