From d2accb0ae84a2ef3d850373b4fdd9ab492735340 Mon Sep 17 00:00:00 2001 From: WhosYourDaddy Date: Sat, 26 Jun 2021 00:07:23 +0800 Subject: [PATCH] 2021-6-26 0:07 --- .../SqlAssistant.cs" | 112 +++++++++++++++++ .../WebForm1.aspx" | 43 +++++++ .../WebForm1.aspx.cs" | 117 ++++++++++++++++++ .../WebForm1.aspx.designer.cs" | 98 +++++++++++++++ 4 files changed, 370 insertions(+) create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SqlAssistant.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx.cs" create mode 100644 "\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx.designer.cs" diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SqlAssistant.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SqlAssistant.cs" new file mode 100644 index 0000000..d54d380 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/SqlAssistant.cs" @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; +using System.Linq; +using System.Web; + +namespace _2021_6_24 +{ + public class SqlAssistant + { + static SqlConnection con; + /// + /// 自定义链接的数据库,默认为学校机房的Student_db数据库 + /// + /// 服务器地址默认为本机 + /// 账号 + /// 密码 + /// 要链接的数据库 + public SqlAssistant( string uid , string pwd , string database , string serverAddress = "." ) + { + con = new SqlConnection($"server = {serverAddress} ; uid = {uid} ; pwd = {pwd} ; database = {database}"); + } + + public SqlAssistant( string database , string serverAddress = "." ) + { + con = new SqlConnection($"Data Source = {serverAddress} ; Initial Catalog = {database} ; Trusted_Connection = true"); + } + /// + /// 执行SQL非查询语句 + /// + /// 需要执行的sql语句 + /// 可选的参数 + /// 执行是否有返回记录 + public bool Execute(string sql , SqlParameter[] pars = null) + { + try + { + if (con.State == ConnectionState.Closed) + { + con.Open(); + } + + + SqlCommand cmd = new SqlCommand(sql,con); + + if (pars != null && pars.Length != 0) + { + cmd.Parameters.AddRange(pars); + } + + return cmd.ExecuteNonQuery() > 0; + } + catch(Exception e) + { + throw new Exception(e.Message); + } + finally + { + if (con != null) + { + con.Close(); + } + } + + } + + /// + /// 执行数据库查询语句 + /// + /// 查询语句 + /// 可选参数 + /// 数据表 + public DataTable Querty(string sql, SqlParameter[] pars = null) + { + try + { + if (con.State == ConnectionState.Closed) + { + con.Open(); + } + + + SqlCommand cmd = new SqlCommand(sql,con); + if (pars != null && pars.Length != 0) + { + cmd.Parameters.AddRange(pars); + } + + SqlDataAdapter adapter = new SqlDataAdapter(cmd); + DataSet ds = new DataSet(); + adapter.Fill(ds); + + return ds.Tables[0]; + + } + catch (Exception e) + { + throw new Exception(e.Message); + } + 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/\351\231\210\350\257\227\346\235\260/WebForm1.aspx" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx" new file mode 100644 index 0000000..5e19b0c --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx" @@ -0,0 +1,43 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_2021_6_25.WebForm1" %> + + + + + + + + + +
+
+ 姓名:
+ 密码:
+ 邮箱:
+ 自我介绍:
+ +
+
+
+ 搜索: + +
+
+
+ + + + + + + + + + + + + + +
+
+ + diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx.cs" new file mode 100644 index 0000000..5d0158d --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx.cs" @@ -0,0 +1,117 @@ +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; +using _2021_6_24; + +namespace _2021_6_25 +{ + public partial class WebForm1 : System.Web.UI.Page + { + //SqlAssistant assistant = new SqlAssistant(uid:"sa",pwd:"123456",database:"Student_db"); + SqlAssistant assistant = new SqlAssistant(database:"Student_db"); + protected void Page_Load(object sender, EventArgs e) + { + if (!IsPostBack) + { + BindGV(); + } + } + + public void BindGV() + { + StudentInfo.DataSource = assistant.Querty("select * from studentInfo",null); + StudentInfo.DataKeyNames = new string[] { "stu_id" }; + StudentInfo.DataBind(); + } + + protected void search_Click(object sender, EventArgs e) + { + + SqlParameter[] pars = + { + new SqlParameter("@name",name.Text) + }; + + StudentInfo.DataSource = assistant.Querty("select * from StudentInfo where stu_name like '%'+ @name + '%'",pars); + + StudentInfo.DataBind(); + } + + protected void add_Click(object sender, EventArgs e) + { + SqlParameter[] pars = + { + new SqlParameter("@name",stuName.Text), + new SqlParameter("@pass",password.Text), + new SqlParameter("@email",email.Text), + new SqlParameter("@intro",intro.Text) + }; + + assistant.Execute("insert into StudentInfo (stu_name,password,email,intro) values ( @name , @pass , @email , @intro)",pars); + + BindGV(); + } + + protected void StudentInfo_RowEditing(object sender, GridViewEditEventArgs e) + { + StudentInfo.EditIndex = e.NewEditIndex; + BindGV(); + } + + protected void StudentInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) + { + StudentInfo.EditIndex = -1; + BindGV(); + } + + protected void StudentInfo_RowUpdating(object sender, GridViewUpdateEventArgs e) + { + string id = StudentInfo.DataKeys[e.RowIndex].Value.ToString(); + + string name = (StudentInfo.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text; + string pass = (StudentInfo.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text; + string email = (StudentInfo.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text; + string intro = (StudentInfo.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text; + + SqlParameter[] pars = + { + new SqlParameter("@id",id), + new SqlParameter("@name",name), + new SqlParameter("@pass",pass), + new SqlParameter("@email",email), + new SqlParameter("@intro",intro) + }; + + assistant.Execute("update StudentInfo set stu_name = @name , password = @pass , email = @email , intro = @intro where stu_id = @id ",pars); + + StudentInfo.EditIndex = -1; + BindGV(); + } + + protected void StudentInfo_RowDeleting(object sender, GridViewDeleteEventArgs e) + { + string id = StudentInfo.DataKeys[e.RowIndex].Value.ToString(); + + SqlParameter[] pars = + { + new SqlParameter("@id",id) + }; + + if (assistant.Execute("Delete from StudentInfo where stu_id = @id", pars)) + { + Response.Write("删除成功!"); + + BindGV(); + } + else + { + Response.Write(""); + } + + } + } +} \ No newline at end of file diff --git "a/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx.designer.cs" "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx.designer.cs" new file mode 100644 index 0000000..3878fe0 --- /dev/null +++ "b/\347\254\2547\346\254\241\344\275\234\344\270\232/\351\231\210\350\257\227\346\235\260/WebForm1.aspx.designer.cs" @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// <自动生成> +// 此代码由工具生成。 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + +namespace _2021_6_25 +{ + + + public partial class WebForm1 + { + + /// + /// form1 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.HtmlControls.HtmlForm form1; + + /// + /// stuName 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.TextBox stuName; + + /// + /// password 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.TextBox password; + + /// + /// email 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.TextBox email; + + /// + /// intro 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.TextBox intro; + + /// + /// add 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.Button add; + + /// + /// name 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.TextBox name; + + /// + /// search 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.Button search; + + /// + /// StudentInfo 控件。 + /// + /// + /// 自动生成的字段。 + /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。 + /// + protected global::System.Web.UI.WebControls.GridView StudentInfo; + } +} -- Gitee