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 0000000000000000000000000000000000000000..0e6efe4ef4efaafa8b1291bf027f34532ea99993 --- /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 0000000000000000000000000000000000000000..3014c7584a50de26874ffafbbd2ed7dd8ec5d54b --- /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 0000000000000000000000000000000000000000..cae8d25cec0b0dbe1f72988e61a5a777e7ed8550 --- /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 0000000000000000000000000000000000000000..13ede066a731ec13d9162fbba78e6313ac7ddd40 --- /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 0000000000000000000000000000000000000000..dc0b5f4342483c3765f1925e135a39d921ad3044 --- /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 0000000000000000000000000000000000000000..cdfea1c5fa1eee7694e0465990fe067699679c25 --- /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 0000000000000000000000000000000000000000..01b14b0ebf09d755b1c7cb479ae0112994bc38d5 --- /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("