From e49474b489cc72146937c48ec983f542b963ab85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=BF=E6=A3=AE?= <2287431003@qq.com> Date: Sat, 26 Jun 2021 07:45:27 +0000 Subject: [PATCH] =?UTF-8?q?=E6=AD=A4=E6=97=B6=E4=B8=8D=E6=90=8F=E4=BD=95?= =?UTF-8?q?=E6=97=B6=E6=90=8F=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\273\225\351\221\253/SqlHelper.cs" | 79 +++++++++++ .../\346\273\225\351\221\253/Student.cs" | 18 +++ .../\346\273\225\351\221\253/WebForm1.aspx" | 19 +++ .../WebForm1.aspx.cs" | 37 +++++ .../WebForm1.aspx.designer.cs" | 44 ++++++ .../\346\273\225\351\221\253/WebForm2.aspx" | 68 ++++++++++ .../WebForm2.aspx.cs" | 127 ++++++++++++++++++ .../WebForm2.aspx.designer.cs" | 107 +++++++++++++++ 8 files changed, 499 insertions(+) create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SqlHelper.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/Student.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx.designer.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx.designer.cs" diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SqlHelper.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SqlHelper.cs" new file mode 100644 index 0000000..0e6efe4 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/SqlHelper.cs" @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Web; + +namespace WebApplication3 +{ + public class SqlHelper + { + private static string constr = "server=LAPTOP-8HS8J7NL;uid=sa;pwd=2287431003a;database=Student_db"; + private SqlConnection con = null;//连接 + public SqlHelper() + { + con = new SqlConnection(constr);//连接数据库 + } + public DataTable Get(string sql, SqlParameter[] pars)//Parameter参数 + { + try + { + if (con.State == ConnectionState.Closed)//state状态 服务器是否为关闭状态 + { + con.Open();//如果是就打开 + } + SqlCommand cmd = new SqlCommand(sql, con);//Command指令 + if (pars != null)//参数不为空 + { + cmd.Parameters.AddRange(pars); + } + SqlDataAdapter sda = new SqlDataAdapter(cmd);//Adapter适配器 + DataSet ds = new DataSet();//DataSet数据在内存中的缓存 + sda.Fill(ds);//添加或刷新ds + return ds.Tables[0]; + } + catch (Exception e) + { + + throw new Exception(e.Message.ToString());//丢出一个错误信息 + } + finally + { + if (con != null) + { + con.Close();//关闭数据库 + } + } + } + public bool Execute(string sql, SqlParameter[] pars)//Execute执行 + { + try + { + if (con.State == ConnectionState.Closed) + { + con.Open(); + } + SqlCommand cmd = new SqlCommand(sql, con); + if (pars != null) + { + cmd.Parameters.AddRange(pars); + } + int result = cmd.ExecuteNonQuery();//影响行数返回一个int值 + return result > 0 ? true : false; + } + catch (Exception e) + { + + throw new Exception(e.Message.ToString()); + } + finally + { + if (con != null) + { + con.Close(); + } + } + } + } +} \ No newline at end of file diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/Student.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/Student.cs" new file mode 100644 index 0000000..3014c75 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/Student.cs" @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace WebApplication3 +{ + public class Student + { + public int Num { get; set; } + public string Name { get; set; } + public Student(int num,string name) + { + Num = num; + Name = name; + } + } +} \ No newline at end of file diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx" new file mode 100644 index 0000000..cae8d25 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx" @@ -0,0 +1,19 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %> + + + + + + + + + +
+
+

<%# Name %>

<%--数据绑定控件--%> + + +
+
+ + diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx.cs" new file mode 100644 index 0000000..13ede06 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx.cs" @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; + +namespace WebApplication3 +{ + public partial class WebForm1 : System.Web.UI.Page + { + private string name; + public string Name + { + get + { + return "爱偷看?!"; + } + } + protected void Page_Load(object sender, EventArgs e) + { + List list = new List(); + list.Add(new Student(1, "张三")); + list.Add(new Student(2, "不及格")); + list.Add(new Student(3, "重开")); + DropDownList1.DataSource = list;//下拉框的数据源 + DropDownList1.DataTextField = "Name";//提供文本的数据源字段 + DropDownList1.DataValueField = "Num";//各列表项提供值的数据源字段 + DropDownList1.DataBind();//数据绑定 + DataBind();//数据绑定!!!! + } + public string Test() + { + return "再不偷学就挂科了!"; + } + } +} \ No newline at end of file diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx.designer.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx.designer.cs" new file mode 100644 index 0000000..dc0b5f4 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm1.aspx.designer.cs" @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +// <自动生成> +// 此代码由工具生成。 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace WebApplication3 +{ + + + public partial class WebForm1 + { + + /// + /// form1 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.HtmlControls.HtmlForm form1; + + /// + /// TextBox1 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.TextBox TextBox1; + + /// + /// DropDownList1 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.DropDownList DropDownList1; + } +} diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx" new file mode 100644 index 0000000..cdfea1c --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx" @@ -0,0 +1,68 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication3.WebForm2" %> + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + +
姓名: + +
密码: + +
Email: + +
自我介绍: + +
+ +
+ +
+
+ 姓名: +
+
+
+ + + + + + + + + + + + + + +
+
+ + diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx.cs" new file mode 100644 index 0000000..01b14b0 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\346\273\225\351\221\253/WebForm2.aspx.cs" @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Linq; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; + +namespace WebApplication3 +{ + public partial class WebForm2 : System.Web.UI.Page + { + private SqlHelper helper = new SqlHelper(); + protected void Page_Load(object sender, EventArgs e) + { + if (!IsPostBack) + { + BindGV(); + } + } + + private void BindGV() + { + GridView1.DataSource = helper.Get("select * from StudentInfo",null); + GridView1.DataKeyNames = new string[] { "stu_id" };//主键名称字段中显示的Gridview控件 + GridView1.DataBind(); + } + + protected void Button1_Click(object sender, EventArgs e) + { + string str = TextBox1.Text;//第一步,获取数据,执行SQL语句要用的 + string sql= "select * from studentInfo where stu_name like '%'+@name+'%'";//第二步,写要执行的SQL语句 + SqlParameter[] pars = + { + new SqlParameter("@name",str) + };//第三步,参数存起来,一会执行SQL语句的时候要用的 + //第四步,调用SqlHelper中的方法,拿到结果判断下或者,拿到结果作为GridView的新数据源,然后绑定下,让效果体现在页面。 + GridView1.DataSource = helper.Get(sql,pars); + 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) + }; //参数存起来,一会执行SQL语句的时候要用的 + if (helper.Execute(sql,pars))//要计算的表达式 + { + Label1.Text = "添加成功"; + BindGV(); + } + else + { + Label1.Text = "添加失败"; + } + } + + protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//edit编辑 + { + //告诉GridView,哪一行要进入编辑状态。 + GridView1.EditIndex = e.NewEditIndex; + BindGV(); + } + + protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//Cancel取消 + { + GridView1.EditIndex = -1; + BindGV(); + } + + protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//Updata更新 EventArgs事件参数 + { + string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); + string name = (GridView1.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text;//row行 cells单元格 + 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) + };//参数存起来,一会执行SQL语句的时候要用的 + if (helper.Execute(sql,pars))//要计算的表达式 + { + GridView1.EditIndex = -1; + BindGV(); + } + else + { + Response.Write(""); + } + } + + protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//delete删除 + { + string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); + string sql = "delete from StudentInfo where stu_id=@id"; + SqlParameter[] pars = + { + new SqlParameter("@id",id) + };//Parameter 参数 参数存起来,一会执行SQL语句的时候要用的 + if (helper.Execute(sql,pars)) + { + BindGV(); + } + else + { + Response.Write("