📄 admin管理.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 mybook
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.Button Button4;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
string strConn="server=.;uid=sa;pwd=sa;database=mybook";
SqlConnection myConnection=new SqlConnection();
myConnection.ConnectionString=strConn;
string strCommand="select * from xyadmin";
SqlDataAdapter da=new SqlDataAdapter(strCommand,myConnection);
DataSet ds=new DataSet();
da.Fill(ds,"scores");
DataGrid1.DataSource=ds.Tables["scores"].DefaultView;
DataGrid1.DataBind();
myConnection.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Button4.Click += new System.EventHandler(this.Button4_Click);
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand_1);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand_1);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button2_Click(object sender, System.EventArgs e)
{
string strConn="server=localhost;uid=sa;pwd=sa;database=mybook";
SqlConnection myConnection=new SqlConnection();
myConnection.ConnectionString=strConn;
if(!IsPostBack)
{
DataGrid1.DataBind();
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string strConn="server=.;uid=sa;pwd=sa;database=mybook";
SqlConnection myConnection=new SqlConnection();
myConnection.ConnectionString=strConn;
string strCommand="insert into xyadmin values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"')";
SqlCommand addScore=new SqlCommand(strCommand,myConnection);
myConnection.Open();
addScore.ExecuteNonQuery();
myConnection.Close();
DataGrid1.DataBind();
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strConn="server=.;uid=sa;pwd=sa;database=mybook";
SqlConnection myConnection=new SqlConnection();
myConnection.ConnectionString=strConn;
string strCommand="select * from xyadmin";
SqlDataAdapter da=new SqlDataAdapter(strCommand,myConnection);
DataSet ds=new DataSet();
da.Fill(ds,"scores");
if(!IsPostBack)
{
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
}
private void Button3_Click(object sender, System.EventArgs e)
{
string strConn="server=.;uid=sa;pwd=sa;database=mybook";
SqlConnection myConnection=new SqlConnection();
myConnection.ConnectionString=strConn;
string strCommand="delete from xyadmin where id='"+TextBox4.Text+"'";
// Response.Write(strCommand);
SqlCommand addScore=new SqlCommand(strCommand,myConnection);
myConnection.Open();
addScore.ExecuteNonQuery();
myConnection.Close();
//DataGrid1.DataBind();
}
private void DataGrid1_ItemCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
private void DataGrid1_DeleteCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "uid=sa;pwd=sa;initial catalog=mybook;data source=localhost;Connect Timeout=90";
myConnection.Open();
string sql_Del = "DELETE FROM xyadmin " +
" WHERE username = '" + e.Item.Cells[1].Text+"'";
//Response.Write(sql_Del);
SqlCommand sqlCommandDel = new SqlCommand(sql_Del,myConnection);
sqlCommandDel.ExecuteNonQuery();
myConnection.Close();
string mysql = "Select * FROM xyadmin";
SqlDataAdapter myadaptor = new SqlDataAdapter(mysql,myConnection);
DataSet ds = new DataSet();
myadaptor.Fill(ds,"MyDataResult");
DataView myView = ds.Tables["MyDataResult"].DefaultView;
DataGrid1.DataSource=myView;
DataGrid1.DataBind();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
switch(((LinkButton)e.CommandSource).CommandName)
{
case "update":
Response.Redirect("WebForm3.aspx?firstname="+e.Item.Cells[1].Text+"&lastname="+e.Item.Cells[2].Text+"&id="+e.Item.Cells[0].Text);
break;
// Add other cases here, if there are multiple ButtonColumns in
// the DataGrid control.
default:
// Do nothing.
break;
}
}
private void Button4_Click(object sender, System.EventArgs e)
{
Response.Redirect("WebForm4.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -