📄 manageline.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace hotel.manage
{
/// <summary>
/// manageline 的摘要说明。
/// </summary>
public class manageline : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
protected hotel.manage.DataSet4 dataSet41;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
bindData();
}
}
void bindData()
{
sqlDataAdapter1.Fill(dataSet41);
DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataSet41 = new hotel.manage.DataSet4();
((System.ComponentModel.ISupportInitialize)(this.dataSet41)).BeginInit();
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=\"TWJ-9490D5784F6\";packet size=4096;user id=sa;data source=\".\";pers" +
"ist security info=True;initial catalog=twj;password=sa";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "line", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("lineid", "lineid"),
new System.Data.Common.DataColumnMapping("linename", "linename"),
new System.Data.Common.DataColumnMapping("lineprice", "lineprice"),
new System.Data.Common.DataColumnMapping("datatime", "datatime"),
new System.Data.Common.DataColumnMapping("journey", "journey"),
new System.Data.Common.DataColumnMapping("picture", "picture")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = @"DELETE FROM line WHERE (lineid = @Original_lineid) AND (datatime = @Original_datatime OR @Original_datatime IS NULL AND datatime IS NULL) AND (journey = @Original_journey OR @Original_journey IS NULL AND journey IS NULL) AND (linename = @Original_linename OR @Original_linename IS NULL AND linename IS NULL) AND (lineprice = @Original_lineprice OR @Original_lineprice IS NULL AND lineprice IS NULL) AND (picture = @Original_picture OR @Original_picture IS NULL AND picture IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_lineid", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "lineid", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_datatime", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "datatime", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_journey", System.Data.SqlDbType.VarChar, 8000, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "journey", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_linename", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "linename", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_lineprice", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "lineprice", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_picture", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "picture", System.Data.DataRowVersion.Original, null));
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO line(lineid, linename, lineprice, datatime, journey, picture) VALUES " +
"(@lineid, @linename, @lineprice, @datatime, @journey, @picture); SELECT lineid, " +
"linename, lineprice, datatime, journey, picture FROM line WHERE (lineid = @linei" +
"d)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@lineid", System.Data.SqlDbType.Int, 4, "lineid"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@linename", System.Data.SqlDbType.VarChar, 50, "linename"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@lineprice", System.Data.SqlDbType.Int, 4, "lineprice"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@datatime", System.Data.SqlDbType.VarChar, 50, "datatime"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@journey", System.Data.SqlDbType.VarChar, 8000, "journey"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@picture", System.Data.SqlDbType.VarChar, 50, "picture"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT lineid, linename, lineprice, datatime, journey, picture FROM line";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE line SET lineid = @lineid, linename = @linename, lineprice = @lineprice, datatime = @datatime, journey = @journey, picture = @picture WHERE (lineid = @Original_lineid) AND (datatime = @Original_datatime OR @Original_datatime IS NULL AND datatime IS NULL) AND (journey = @Original_journey OR @Original_journey IS NULL AND journey IS NULL) AND (linename = @Original_linename OR @Original_linename IS NULL AND linename IS NULL) AND (lineprice = @Original_lineprice OR @Original_lineprice IS NULL AND lineprice IS NULL) AND (picture = @Original_picture OR @Original_picture IS NULL AND picture IS NULL); SELECT lineid, linename, lineprice, datatime, journey, picture FROM line WHERE (lineid = @lineid)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@lineid", System.Data.SqlDbType.Int, 4, "lineid"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@linename", System.Data.SqlDbType.VarChar, 50, "linename"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@lineprice", System.Data.SqlDbType.Int, 4, "lineprice"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@datatime", System.Data.SqlDbType.VarChar, 50, "datatime"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@journey", System.Data.SqlDbType.VarChar, 8000, "journey"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@picture", System.Data.SqlDbType.VarChar, 50, "picture"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_lineid", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "lineid", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_datatime", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "datatime", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_journey", System.Data.SqlDbType.VarChar, 8000, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "journey", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_linename", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "linename", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_lineprice", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "lineprice", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_picture", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "picture", System.Data.DataRowVersion.Original, null));
//
// dataSet41
//
this.dataSet41.DataSetName = "DataSet4";
this.dataSet41.Locale = new System.Globalization.CultureInfo("zh-CN");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet41)).EndInit();
}
#endregion
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.bindData();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
((LinkButton)(e.Item.Cells[6].Controls[0])).Attributes.Add("onclick","return confirm('确认要删除吗?');");
((LinkButton)(e.Item.Cells[7].Controls[0])).Attributes.Add("onclick","return confirm('确认要删除吗?');");
}
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int lineid=Convert.ToInt32(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
SqlConnection con=DB.createCon();
SqlCommand cmd=new SqlCommand("delete from line where lineid='"+lineid+"'",con);
con.Open();
cmd.ExecuteNonQuery();
this.bindData();
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
bindData();
}
private void LinkButton1_Click(object sender, System.EventArgs e)
{
Response.Redirect("addline.aspx");
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
bindData();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection con =DB.createCon();
con.Open ();
int lineid= Convert.ToInt32(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
//非只读绑定列,处于编辑状态
string linename = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;
string lineprice= ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string datatime= ((TextBox)(e.Item.Cells[3].Controls[0])).Text;
string journey= ((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string picture= ((TextBox)(e.Item.Cells[5].Controls[0])).Text;
// this.Response.Write(readersex);
string strSql = "update line set linename = '"+linename+"',lineprice='"+lineprice+"',datatime='"+datatime+"',journey='"+journey+"',picture='"+picture+"'where lineid =" +lineid ;
// string strSql = "update 读者信息表 set readername = '" + readername + "',readsex='"+readersex+"',readerdep='"+readerdep+"',readertype='"+readertype+"'where readerid =" + readerid;
SqlCommand cmd=new SqlCommand (strSql,con);
try
{
cmd.ExecuteNonQuery();
this.Response.Write(" <script language ='javascript'> alert ('更新成功!')</script>");
this.DataGrid1.EditItemIndex=-1; //退出行的编辑模式
}
catch
{
this.Response.Write(" <script language ='javascript'> alert ('更新失败!')</script>");
}
this.DataGrid1.EditItemIndex=-1;
bindData();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -