简单的学生信息管理系统
2015-05-25 10:48
411 查看
初学C#、asp.net和sql server,小有所感。下面这个小程序主要提供了以下一些操作的实例:
<1>如何将Gridview与sql数据库中的表绑定
<2>数据的增删改
<3>数据的条件查询
话不多说,上代码
前台
后台
设计表
学生信息表
具体的功能大家可以试下。当然这个程序并不完善,比如查询功能是不能对“姓名”和“性别”进行查询的,虽然有这个选项。再当然这是非常容易完善的,我比较懒^ - ^
PS 虽然是个超简单的程序,但是还是能说明一些问题的,如果不是自己编,很多细节是注意不到的,比如单引号和双引号在查询语句中的使用。犯了不少错,很多原理我也不甚解。过程中求助了论坛的很多大神,在此不点名致谢,但是请收下我的膝盖。
<1>如何将Gridview与sql数据库中的表绑定
<2>数据的增删改
<3>数据的条件查询
话不多说,上代码
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication21.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>学生信息</title> <script language="javascript" type="text/javascript"> // <![CDATA[ function test_onclick() { } // ]]> </script> </head> <body> <form id="form1" runat="server"> <div align="left"> <fieldset style="width:50%;display:inline;"> <legend> 学生信息 </legend> <table> <tr> <td colspan="2"> <asp:GridView ID="GridView1" runat="server" > </asp:GridView> </td> </tr> <tr> <td colspan="2"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </td> </tr> </table> </fieldset> <fieldset style="width:50%;display:inline;"> <legend> 搜索 </legend> <asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"> </asp:DropDownList> <asp:Label ID="Label13" runat="server" Visible="False"></asp:Label> <asp:TextBox ID="TextBox12" runat="server" Visible="False"></asp:TextBox> <asp:Label ID="Label14" runat="server" Text="—" Visible="False"></asp:Label><asp:TextBox ID="TextBox13" runat="server" Visible="False"></asp:TextBox> <asp:Button ID="Button7" runat="server" Text="给我搜" Visible="False" onclick="Button7_Click" /> <asp:GridView ID="GridView2" runat="server"> </asp:GridView> <asp:Label ID="Label15" runat="server"></asp:Label> <br /> </fieldset> </div> <div id="div1" > <fieldset> <legend> 编辑按钮 </legend> <input type="button" value="新建" id="Button1" onclick="return Button1_onclick()"></input> <script type="text/javascript"> document.getElementById("Button1").onclick = function () { document.getElementById("div2").style.display = "block"; document.getElementById("div3").style.display = "none"; document.getElementById("div4").style.display = "none"; } </script> <input type="button" value="修改" id="Button2" onclick="return Button2_onclick()"></input> <script type="text/javascript"> document.getElementById("Button2").onclick = function () { document.getElementById("div2").style.display = "none"; document.getElementById("div3").style.display = "block"; document.getElementById("div4").style.display = "none"; } </script> <input type="button" value="删除" id="Button3" onclick="return Button3_onclick()"></input> <script type="text/javascript"> document.getElementById("Button3").onclick = function () { document.getElementById("div2").style.display = "none"; document.getElementById("div3").style.display = "none"; document.getElementById("div4").style.display = "block"; } </script> </fieldset> </div> <div id="div2" style="display:none";> <asp:Label ID="Label2" runat="server" Text="学号:"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </br> <asp:Label ID="Label3" runat="server" Text="姓名:"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </br> <asp:Label ID="Label4" runat="server" Text="年级:"></asp:Label> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </br> <asp:Label ID="Label5" runat="server" Text="名次:"></asp:Label> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </br> <asp:Label ID="Label6" runat="server" Text="性别:"></asp:Label> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> </br> <asp:Button ID="Button4" runat="server" Text="插入" onclick="Button4_Click" /> </div> <div id="div3" style="display:none";> <asp:Label ID="Label7" runat="server" Text="学号:"></asp:Label> <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox> </br> <asp:Label ID="Label8" runat="server" Text="姓名:"></asp:Label> <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox> </br> <asp:Label ID="Label9" runat="server" Text="年级:"></asp:Label> <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox> </br> <asp:Label ID="Label10" runat="server" Text="名次:"></asp:Label> <asp:TextBox ID="TextBox9" runat="server"></asp:TextBox> </br> <asp:Label ID="Label11" runat="server" Text="性别:"></asp:Label> <asp:TextBox ID="TextBox10" runat="server"></asp:TextBox> </br> <asp:Button ID="Button5" runat="server" Text="更新" onclick="Button5_Click" /> </div> <div id="div4" style="display:none";> <asp:Label ID="Label12" runat="server" Text="学号:"></asp:Label> <asp:TextBox ID="TextBox11" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button6" runat="server" Text="删除" onclick="Button6_Click" /> </div> </form> </body> </html>
后台
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace WebApplication21 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { string conStr = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=242318"; string sqlStr = "select * from 学生信息"; using (SqlConnection con = new SqlConnection(conStr)) { DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con); ada.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); Label1.Text = "绑定成功"; } } catch (Exception ee) { Label1.Text = "错误信息:" + ee.Message; } try { string conStr1 = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=242318"; string sqlStr1 = "select * from 设计"; using (SqlConnection con = new SqlConnection(conStr1)) { DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr1, con); ada.Fill(ds, "设计"); DropDownList1.DataSource = ds.Tables["设计"].DefaultView; DropDownList1.DataValueField = ds.Tables["设计"].Columns[0].ColumnName; DropDownList1.DataTextField = ds.Tables["设计"].Columns[0].ColumnName; DropDownList1.DataBind(); DropDownList1.Items.Insert(0, new ListItem("----", "0")); } } catch (Exception ee) { } } } protected void Button4_Click(object sender, EventArgs e) { //int k = this.GridView1.Rows.Count; try { string conStr = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=242318"; string sqlStr = "select * from 学生信息"; using (SqlConnection con = new SqlConnection(conStr)) { /* DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con); ada.Fill(ds); DataTable dt =ds.Tables[0]; DataRow dr = dt.NewRow(); dr[0] = TextBox1.Text; dr[1] = TextBox2.Text; dr[2] = TextBox3.Text; dr[3] = TextBox4.Text; dr[4] = TextBox5.Text; ada.Update(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); Label1.Text = "绑定成功";*/ string str = "server=.;database=学生信息管理系统;uid=sa;pwd=242318"; SqlConnection conn; conn = new SqlConnection(str); conn.Open(); SqlCommand cmd = new SqlCommand("Insert into 学生信息 (学号,姓名,年级,名次,性别) values( '" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')", conn); cmd.ExecuteNonQuery(); DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con); ada.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); Label1.Text = "绑定成功"; } } catch (Exception ee) { Label1.Text = "错误信息:" + ee.Message; } } protected void Button5_Click(object sender, EventArgs e) { try { string conStr = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=242318"; string sqlStr = "select * from 学生信息"; using (SqlConnection con = new SqlConnection(conStr)) { /* DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con); ada.Fill(ds); DataTable dt =ds.Tables[0]; DataRow dr = dt.NewRow(); dr[0] = TextBox1.Text; dr[1] = TextBox2.Text; dr[2] = TextBox3.Text; dr[3] = TextBox4.Text; dr[4] = TextBox5.Text; ada.Update(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); Label1.Text = "绑定成功";*/ string str = "server=.;database=学生信息管理系统;uid=sa;pwd=242318"; SqlConnection conn; conn = new SqlConnection(str); conn.Open(); SqlCommand cmd = new SqlCommand("Update 学生信息 set 姓名='" + TextBox7.Text + "',年级='" + TextBox8.Text + "',名次='" + TextBox9.Text + "',性别='" + TextBox10.Text + "' where 学号='" + TextBox6.Text + "'", conn); cmd.ExecuteNonQuery(); DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con); ada.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); Label1.Text = "绑定成功"; } } catch (Exception ee) { Label1.Text = "错误信息:" + ee.Message; } } protected void Button6_Click(object sender, EventArgs e) { try { string conStr = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=242318"; string sqlStr = "select * from 学生信息"; using (SqlConnection con = new SqlConnection(conStr)) { string str = "server=.;database=学生信息管理系统;uid=sa;pwd=242318"; SqlConnection conn; conn = new SqlConnection(str); conn.Open(); SqlCommand cmd = new SqlCommand("Delete 学生信息 where 学号='" + TextBox11.Text + "'", conn); cmd.ExecuteNonQuery(); DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con); ada.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); Label1.Text = "绑定成功"; } } catch (Exception ee) { Label1.Text = "错误信息:" + ee.Message; } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label13.Visible = true; Label14.Visible = true; TextBox12.Visible = true; TextBox13.Visible = true; Button7.Visible = true; } protected void Button7_Click(object sender, EventArgs e) { /* GridView2.Visible = true; try { string conStr = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=242318"; string sqlStr = "select 学号,姓名,年级,名次,性别 from 学生信息 where '" + DropDownList1.Text + "'>'" + TextBox12.Text + "' and '" + DropDownList1.Text + "'<'"+TextBox13.Text+"'"; using (SqlConnection con = new SqlConnection(conStr)) { DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con); ada.Fill(ds); GridView2.DataSource = ds.Tables[0]; GridView2.DataBind(); } } catch (Exception ee) { }*/ try { string conStr = @"Data Source=.;Initial Catalog=学生信息管理系统;User ID=sa;Password=242318"; string sqlStr ="select * from 学生信息 where " + DropDownList1.Text +" >='" + TextBox12.Text + "' and "+ DropDownList1.Text + "<="+TextBox13.Text+""; using (SqlConnection con = new SqlConnection(conStr)) { DataSet ds = new DataSet(); con.Open(); SqlDataAdapter ada = new SqlDataAdapter(sqlStr, con); ada.Fill(ds); GridView2.DataSource = ds.Tables[0]; GridView2.DataBind(); Label15.Text = "绑定成功"; } } catch (Exception ee) { Label15.Text = "错误信息:" + ee.Message; } } } }数据库中的表是这样的
设计表
学生信息表
具体的功能大家可以试下。当然这个程序并不完善,比如查询功能是不能对“姓名”和“性别”进行查询的,虽然有这个选项。再当然这是非常容易完善的,我比较懒^ - ^
PS 虽然是个超简单的程序,但是还是能说明一些问题的,如果不是自己编,很多细节是注意不到的,比如单引号和双引号在查询语句中的使用。犯了不少错,很多原理我也不甚解。过程中求助了论坛的很多大神,在此不点名致谢,但是请收下我的膝盖。
相关文章推荐
- 实现简单的学生选课信息管理系统
- 程序阅读:简单C++学生信息管理系统
- 使用List集合写的一个简单的学生信息管理系统!
- PHP实现简单的学生信息管理系统
- hibernate+struts2学生信息管理系统简单整合
- 一个简单的学生信息管理系统(循环双向链表)
- JavaSwing+Hibernate实现简单的学生信息管理系统
- C#窗体应用程序 vs + sql server2008连接举例,简单的学生信息管理系统
- 第九周阅读程序简单的学生信息管理系统
- C下学生管理系统:从文件中读取30位学生的信息(含邮箱),并实现简单的增、删、查找、统计(邮箱使用人数)。---附程序哦!
- 基于Xpath的简单的学生信息管理系统
- C++项目(1):学生信息管理系统(简单教学版)
- 使用python实现一个简单的学生信息管理系统
- C#窗体应用程序 vs + sql server2008连接举例,简单的学生信息管理系统 补充注册页面,如有侵权,请联系本人删除
- 基于linux socket 简单学生信息管理系统server程序
- python下学生管理系统:从文件中读取30位学生的信息(含邮箱),并实现简单的增、删、查找、统计(邮箱使用人数)。---附程序哦!
- <我的C++程序>简单的学生信息管理系统
- c#简单的学生信息管理系统(垃圾版),适合初学者
- java SE 简单的学生信息管理系统
- 基于linux socket 简单学生信息管理系统client程序