初学者的问题??请求帮忙。。。多谢!

loveatree 2004-06-17 12:03:56
本人做了个修改和删除的功能模块:其源代码如下:
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的方法了,直接操作数据库,进行修改和删除。。。请问如何实现???还请各位指点!!!
...全文
84 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dbase 2004-06-17
  • 打赏
  • 举报
回复
不用session就每次用的时候去取数据库填充dataset
13880079673 2004-06-17
  • 打赏
  • 举报
回复
奇怪,为什么不用session,而采用比session第效N倍的杜数据库方法(而且反复读去数据库信息会增加数据库压力)
chrovan 2004-06-17
  • 打赏
  • 举报
回复
同上

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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