Sql Server数据库的添加、查询、修改功能

这篇博客介绍了如何在SQL Server中进行学生信息管理。包括创建学生成绩表T_Students,展示前后台页面的代码实现,以及添加、查询和修改学生信息的具体步骤。通过示例代码展示了如何使用C#连接数据库,执行SQL命令进行数据操作。

学生成绩表T_Students
学生编号 主键 guid类型
学生姓名
班级编号
班主任姓名
联系电话
家庭住址

功能
添加学生信息
根据学生编号查询学生信息
更改学生信息


StudentsId,StudentName,ClassId,ClassTeacher,TelPhone,Address1


--添加数据库的前台页面
--添加数据库的后台页面
--查询和编辑前台页面
--查询和编辑的后台页面

 

 

 

--数据库的前台页面:

SELECT * FROM T_Students

--数据库的后台页面:

CREATE DATABASE Class

USE Class
CREATE TABLE T_Students
(
StudentsId UNIQUEIDENTIFIER PRIMARY KEY,
StudentName NVARCHAR(20),
ClassId INT,
ClassTeacher NVARCHAR(20),
TelPhone VARCHAR(20),
Address1 NVARCHAR(50)
)

--添加、查询、修改的前台页面

<body>
    <form id="form1" runat="server">
    <div>
   
        1、添加学生信息:<br />
        <br />
        学生姓名:<asp:TextBox ID="txtStuName" runat="server"></asp:TextBox>
        <br />
        班级编号:<asp:TextBox ID="txtClassId" runat="server"></asp:TextBox>
        <br />
        班主任姓名:<asp:TextBox ID="txtTeaName1" runat="server"></asp:TextBox>
        <br />
        联系电话:<asp:TextBox ID="txtPhone1" runat="server"></asp:TextBox>
        <br />
        家庭住址:<asp:TextBox ID="txtAddress1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnadd" runat="server" Text="添加" onclick="btnadd_Click" />
        <br />
        <br />
        <br />
        2、查询学生信息:<br />
        <br />
        学生姓名:<asp:TextBox ID="txtStuName1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="查询" onclick="Button1_Click" />
        <br />
        <br />
        3、更改学生信息:<br />
        <br />
        学生编号:<asp:TextBox ID="txtStuId" runat="server" ReadOnly="true" Enabled="false"></asp:TextBox>
        <br />
        学生姓名:<asp:TextBox ID="txtStuName2" runat="server"></asp:TextBox>
        <br />
        班级编号:<asp:TextBox ID="txtClassId1" runat="server"></asp:TextBox>
        <br />
        班主任姓名:<asp:TextBox ID="txtTeaName" runat="server"></asp:TextBox>
        <br />
        联系电话:<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
        <br />
        家庭住址:<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button2" runat="server" Text="修改并保存" onclick="Button2_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.SqlClient;
using System.Data;

namespace _2013_3_1大项目
{
    public partial class default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        string strcon = @"data source=连接数据库的机名;Initial catalog=数据库名称;User Id=联机数据库的名称;password=联机数据库的密码";
        protected void btnadd_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(strcon);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "insert into T_Students(StudentsId,StudentName,ClassId,ClassTeacher,TelPhone,Address1)values(NEWID(),@studentname,@classid,@calssteacher,@telphone,@address1)";
            cmd.Parameters.AddWithValue("@studentname", txtStuName.Text);
            cmd.Parameters.AddWithValue("@classid", txtClassId.Text);
            cmd.Parameters.AddWithValue("@calssteacher", txtTeaName1.Text);
            cmd.Parameters.AddWithValue("@telphone", txtPhone1.Text);
            cmd.Parameters.AddWithValue("@address1", txtAddress1.Text);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Dispose();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd1 = new SqlCommand();
            cmd1.Connection = con;
            cmd1.CommandText = "select StudentsId,StudentName,ClassId,ClassTeacher,TelPhone,Address1 from T_Students whereStudentName=@studentname";
            cmd1.Parameters.AddWithValue("@studentname", txtStuName1.Text);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd1);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            con.Dispose();
            txtStuId.Text = Convert.ToString(dt.Rows[0]["StudentsId"]);
            txtStuName2.Text = Convert.ToString(dt.Rows[0]["StudentName"]);
            txtClassId1.Text = Convert.ToString(dt.Rows[0]["ClassId"]);
            txtTeaName.Text = Convert.ToString(dt.Rows[0]["ClassTeacher"]);
            txtPhone.Text = Convert.ToString(dt.Rows[0]["TelPhone"]);
            txtAddress.Text = Convert.ToString(dt.Rows[0]["Address1"]);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string stuid = txtStuId.Text;
            string stuname = txtStuName2.Text;
            string calssid = txtClassId1.Text;
            string teaname = txtTeaName.Text;
            string phone = txtPhone.Text;
            string address = txtAddress.Text;

            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "update T_Students set StudentsId=@studentsid,StudentName=@studentname,ClassId=@calssid,ClassTeacher=@calssteacher,TelPhone=@telphone,Address1=@address";
            cmd.Parameters.AddWithValue("@studentsid", stuid);
            cmd.Parameters.AddWithValue("@studentname", stuname);
            cmd.Parameters.AddWithValue("@calssid", calssid);
            cmd.Parameters.AddWithValue("@calssteacher", teaname);
            cmd.Parameters.AddWithValue("@telphone", phone);
            cmd.Parameters.AddWithValue("@address", address);
            if (cmd.ExecuteNonQuery() > 0)
            {
                Response.Write("哈哈,成功啦");
            }
            con.Dispose();
            cmd.Dispose();
        }
    }
}

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值