From 0888379547be93f10878b3dddef18365ad5104fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=83=E6=96=87=E6=B5=A9?= <1208984334@qq.com> Date: Wed, 23 Jun 2021 17:09:31 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=BB=83=E6=96=87?= =?UTF-8?q?=E6=B5=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\273\203\346\226\207\346\265\251/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/.keep" diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/.keep" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From b4046d4b6ca6481d19a2b4f716ee465cb948fa08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=83=E6=96=87=E6=B5=A9?= <1208984334@qq.com> Date: Wed, 23 Jun 2021 17:09:48 +0000 Subject: [PATCH 2/2] zy --- .../SqlHeper.cs" | 76 +++++++++ .../WebForm1.aspx" | 43 +++++ .../WebForm1.aspx.cs" | 151 ++++++++++++++++++ 3 files changed, 270 insertions(+) create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SqlHeper.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/WebForm1.aspx" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/WebForm1.aspx.cs" diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SqlHeper.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SqlHeper.cs" new file mode 100644 index 0000000..7904509 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SqlHeper.cs" @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Web; + +namespace WebApplication1 +{ + public class SQLHelper + { + private string constr = "server=.;uid=sa;pwd=123456;database=Student_db"; + private SqlConnection con; + + public SQLHelper() + { + con = new SqlConnection(constr); + } + + public DataSet Get(string sql, SqlParameter[] pars) + { + DataSet ds = null; + try + { + Console.WriteLine((int)con.State); + if ((int)con.State == 0) + { + con.Open(); + } + SqlCommand cmd = new SqlCommand(sql, con); + if (pars != null) + { + cmd.Parameters.AddRange(pars); + } + SqlDataAdapter adapter = new SqlDataAdapter(cmd); + ds = new DataSet(); + adapter.Fill(ds); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + finally + { + con.Close(); + } + return ds; + } + + public int Set(string sql, SqlParameter[] pars) + { + int result = -1; + try + { + Console.WriteLine((int)con.State); + if ((int)con.State == 0) + { + + con.Open(); + } + SqlCommand cmd = new SqlCommand(sql, con); + cmd.Parameters.AddRange(pars); + result = cmd.ExecuteNonQuery(); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + finally + { + con.Close(); + } + return result; + } + } +} \ No newline at end of file diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/WebForm1.aspx" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/WebForm1.aspx" new file mode 100644 index 0000000..546e5c3 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/WebForm1.aspx" @@ -0,0 +1,43 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> + + + + + + + + + + +
+
+ +
+ +
+
+ +
+ 姓名:
+ 密码:
+ 邮箱:
+ 介绍:
+ +
+ +
+ + + + + + + + + + +
+ +
+ + \ No newline at end of file diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/WebForm1.aspx.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/WebForm1.aspx.cs" new file mode 100644 index 0000000..d0a73ef --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/WebForm1.aspx.cs" @@ -0,0 +1,151 @@ +using System; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; +using WebApplication1; + +namespace WebApplication1 +{ + + public partial class StuList : System.Web.UI.Page + { + SQLHelper helper = new SQLHelper(); + protected void Page_Load(object sender, EventArgs e) + { + if (!IsPostBack) + { + GvBind(); + } + } + + private void GvBind() + { + string sql = "select * from StudentInfo"; + DataSet ds = helper.Get(sql, null); + GridView1.DataSource = ds.Tables[0]; + GridView1.DataKeyNames = new string[] { "stu_id" }; + GridView1.DataBind(); + } + + protected void Button1_Click(object sender, EventArgs e) + { + string name = TextBox1.Text; + string sql = "select * from StudentInfo where stu_name like '%'+@name+'%' "; + SqlParameter[] pars = { + new SqlParameter("name",name) + }; + DataSet ds = helper.Get(sql, pars); + GridView1.DataSource = ds.Tables[0]; + GridView1.DataBind(); + } + + protected void Button2_Click(object sender, EventArgs e) + { + string name = UserName.Text; + string pwd = Password.Text; + string email = Email.Text; + string intro = Intro.Text; + + string sql = "insert into StudentInfo (stu_name,password,email,intro) values (@name,@pwd,@email,@intro)"; + SqlParameter[] pars = + { + new SqlParameter("@name",name), + new SqlParameter("@pwd",pwd), + new SqlParameter("@email",email), + new SqlParameter("@intro",intro) + }; + + int result = helper.Set(sql, pars); + if (result != -1) + { + GvBind(); + } + else + { + Response.Write("注册失败!"); + } + } + + protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) + { + GridView1.EditIndex = e.NewEditIndex; + GvBind(); + + } + + protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) + { + GridView1.EditIndex = -1; + GvBind(); + } + + protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) + { + //传递当前行的索引 获取主键的值 + string stu_id = GridView1.DataKeys[e.RowIndex].Value.ToString(); + string sql = "delete from StudentInfo where stu_id=@stu_id"; + SqlParameter[] pars = { + new SqlParameter("stu_id",stu_id) + }; + int result = helper.Set(sql, pars); + if (result != -1) + { + GvBind(); + } + else + { + Response.Write("删除失败!"); + } + } + + protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) + { + + string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); + /* + * GridView1. Rows[e.RowIndex]. Cells[0].Controls[0] + * GridView1下的正在被编辑的行中的单元格中的第一个对象 + * + * as TextBox + * 转换为TextBox控件 + * + * 控件内显示的文本 + * .Text + * + * (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text + * 获取被编辑的‘行>单元格>控件’的文本 + */ + string name = (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text; + string pwd = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text; + string email = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text; + string intro = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text; + + string sql = "update StudentInfo set stu_name=@name,password=@pwd,email=@email,intro=@intro where stu_id=@id"; + SqlParameter[] pars = + { + new SqlParameter("@name",name), + new SqlParameter("@pwd",pwd), + new SqlParameter("@email",email), + new SqlParameter("@intro",intro), + new SqlParameter("@id",id) + }; + int result = helper.Set(sql, pars); + if (result != -1) + { + GridView1.EditIndex = -1; + GvBind(); + } + else + { + Response.Write("修改失败!"); + } + } + + } + +} -- Gitee