From 84bf282bb9c8708c5b91c8580da6ee3fbec7797b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E7=A8=8B=E7=A5=A5?= <842143662@qq.com> Date: Sat, 26 Jun 2021 09:25:00 +0000 Subject: [PATCH] WebForm --- .../SqlHelper.cs" | 85 ++++++++++++ .../WebForm1.aspx" | 68 +++++++++ .../WebForm1.aspx.cs" | 130 ++++++++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SqlHelper.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/WebForm1.aspx" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/WebForm1.aspx.cs" diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SqlHelper.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SqlHelper.cs" new file mode 100644 index 0000000..c10c5e7 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/SqlHelper.cs" @@ -0,0 +1,85 @@ +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 static string constr = "server=. ; uid=sa ; pwd=123456 ; database=Student_db"; + + private SqlConnection con = null; + + public SqlHelper() + { + con = new SqlConnection(constr); + } + + public DataTable Get(string sql , SqlParameter[] pars) + { + try + { + if (con.State == ConnectionState.Closed) + { + con.Open(); + } + SqlCommand cmd = new SqlCommand(sql, con); + + if (pars != null) + { + cmd.Parameters.AddRange(pars); + } + SqlDataAdapter sda = new SqlDataAdapter(cmd); + DataSet ds = new DataSet(); + sda.Fill(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) + { + 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(); + 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/\344\270\207\347\250\213\347\245\245/WebForm1.aspx" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/WebForm1.aspx" new file mode 100644 index 0000000..295fc20 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/WebForm1.aspx" @@ -0,0 +1,68 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> + + + + + + + + + +
+
+ +

添加信息

+ + + + + + + + + + + + + + + + + +
姓名:
密码:
电子邮箱:
个人信息:
+
+ + +
+ +
+
+
+ 姓名: + +
+ +
+
+ + + + + + + + + +   + + + +   + + + + +
+ + + diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/WebForm1.aspx.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/WebForm1.aspx.cs" new file mode 100644 index 0000000..b478900 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\344\270\207\347\250\213\347\245\245/WebForm1.aspx.cs" @@ -0,0 +1,130 @@ +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 WebApplication1 +{ + public partial class WebForm1 : System.Web.UI.Page + { + private SqlHelper helper = new SqlHelper(); + protected void Page_Load(object sender, EventArgs e) + { + if (!IsPostBack) + { + BrindGV(); + } + } + + private void BrindGV() + { + GridView1.DataSource = helper.Get("select * from StudentInfo ", null); + GridView1.DataKeyNames = new string[] { "stu_id" }; + GridView1.DataBind(); + } + + protected void Button1_Click(object sender, EventArgs e) + { + string str = TextBox1.Text; + string sql = "select * from StudentInfo where stu_name like '%'+@name+'%'"; + + SqlParameter[] pars = + { + new SqlParameter ("@name",str) + }; + + GridView1.DataSource = helper.Get(sql, pars); + GridView1.DataBind(); + } + + protected void Button3_Click(object sender, EventArgs e) + { + string name = TextBox2.Text; + string password = TextBox3.Text; + string email = TextBox4.Text; + string intro = TextBox5.Text; + + string sql = "insert into StudentInfo (stu_name ,password , email ,intro ) values (@name ,@password , @email ,@intro)"; + + SqlParameter[] pars = + { + new SqlParameter("@name",name), + new SqlParameter("@password", password), + new SqlParameter("@email", email), + new SqlParameter("@intro", intro) + }; + + if (helper.Execute(sql, pars)) + { + Label1.Text = "添加成功"; + BrindGV(); + } + else + { + Label1.Text = "添加失败"; + } + } + + protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) + { + GridView1.EditIndex = e.NewEditIndex; + BrindGV(); + } + + protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) + { + GridView1.EditIndex = -1; + BrindGV(); + } + + protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) + { + string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); + string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text; + string password = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; + string email = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; + string intro = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text; + + string sql = "update StudentInfo set stu_name=@name ,password=@password , email=@email , intro=@intro where stu_id=@id"; + + SqlParameter[] pars = + { + new SqlParameter("@name",name), + new SqlParameter("@password",password), + new SqlParameter("@email",email), + new SqlParameter("@intro",intro), + new SqlParameter("@id",id) + }; + + if (helper.Execute(sql,pars)) + { + GridView1.EditIndex = -1; + BrindGV(); + } + } + + protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) + { + string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); + + string sql = "delete from StudentInfo where stu_id=@id"; + SqlParameter[] pars = + { + new SqlParameter("@id",id) + }; + if (helper.Execute(sql,pars)) + { + GridView1.EditIndex = -1; + BrindGV(); + } + } + + protected void Button4_Click(object sender, EventArgs e) + { + + } + } +} \ No newline at end of file -- Gitee