初学者的问题??请求帮忙。。。多谢!
本人做了个修改和删除的功能模块:其源代码如下:
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的方法了,直接操作数据库,进行修改和删除。。。请问如何实现???还请各位指点!!!
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的方法了,直接操作数据库,进行修改和删除。。。请问如何实现???还请各位指点!!!
...全文
相关推荐
每个JAVA初学者都应该搞懂的问题!
一些java方面的基础性的问题,面试的时候容易忽略的
hello-world:你是初学者吗? 在这里发送您的测试请求!
你好,世界 你是初学者吗? 在这里发送您的测试请求! 有人会审核(并合并)它们。 如果您正在寻找学习Web编程的资源,请查看@jlord的 。 不知道该怎么办? 当您不确定要做什么时,很难上手,所以这里有一些建议: 尝试在humans目录中添加文件,并在文件标题中输入您的用户名。 例如,我的用户名是muan ,因此我将创建一个名为muan.html的文件,例如 。 它可以是您想要的任何类型的文件,可以是muan.md , muan.txt或仅仅是muan 。 将您的用户名添加到human.txt 。 ( :thumbs_up: 奖励)对human.txt排序,因此文件按字母顺序排列。 ( :thumbs_up: 奖励)在您自己的文件中添加一些有趣的内容。 看看我的: : 不知道该怎么做? 您应该使用右上方的Fork按钮来分叉该存储库,请参阅本指南。 根据需要在叉子上进行更改。 最简单的方法是通过GitH
HTML代码大全(初学者必备!必背!)
很多初学网页制作的人,都可能会为了记不住HTML代码。这里提供HTML代码。熟练HTML的人这个没有用,不熟练的人还是拿去背一背比较好!
MFC常见问题集。。。
学习MFC,遇到常见问题,这里看看。。。
http请求框架,初学者简单易用,VvHttp更新
超简单http请求,http,post,图片请求


