数据库期末考源代码

USE master;
IF DB_ID('EduBase') IS NOT NULL
	BEGIN
		ALTER DATABASE EduBase
			SET SINGLE_USER
			WITH ROLLBACK IMMEDIATE;
		DROP DATABASE EduBase;
	END
CREATE DATABASE EduBase							
	ON						
		(NAME='DataFile_1'					
		,FILENAME='E:\EduBase\DataFile_1.mdf')					
	LOG ON						
		(NAME='LogFile_1'					
		,FILENAME='E:\EduBase\LogFile_1.ldf');	
		
CREATE TABLE tb_Department
	(No
		INT
		NOT NULL
		PRIMARY KEY
	,Name
		VARCHAR(20)
		NOT NULL)
INSERT tb_Department  
	(No,Name)
	VALUES
	(1,'管理学院')
	,(2,'中医学院');
	
CREATE TABLE tb_Major
	(No
		INT
		NOT NULL
		PRIMARY KEY
	,Name
		VARCHAR(20)
		NOT NULL
	,DepartmentNo
		INT
		NOT NULL)
INSERT tb_Major
	(No,Name,DepartmentNo)
	VALUES
	(1,'卫管',1)
	,(2,'信管',1)
	,(3,'中医',2)
	,(4,'临床',2);
	
CREATE TABLE tb_Class
	(No
		INT
		NOT NULL
		PRIMARY KEY
	,Name
		VARCHAR(20)
		NOT NULL
	,MajorNo
		INT
		NOT NULL)
		
INSERT tb_Class
	(No,Name,MajorNo)
	VALUES
	(1,'17卫管',1)
	,(2,'17信管',2)
	,(3,'17中医',3)
	,(4,'17临床',4);	
	
CREATE TABLE tb_Student
	(No
		CHAR(10)
		NOT NULL
		PRIMARY KEY
	,Name
		VARCHAR(20)
		NOT NULL
	,Gender
		VARCHAR(4)
		NOT NULL
	,BirthDate
		DATE
		NULL
	,ClassNo
		INT
		NULL
		FOREIGN KEY 
			REFERENCES tb_Class(No)
	,Speciality
		VARCHAR(100)
		NULL
	);
	
INSERT tb_Student
	(No,Name,Gender,BirthDate,ClassNo,Speciality)
	VALUES
	('3170707023','何少平','男','1999-05-07',2,'敲代码')
	,('3170707024','杨颜霞','女','1997-04-07',2,'吃货')
	,('3170707026','宋务琦','男','1998-03-03',2,NULL) 
	,('3170707027','李广生','男','1998-09-15',1,NULL)
	,('3170707028','薛朝鹏','男','1998-09-18',1,NULL)
	,('3170707029','李雅婷','女','1998-04-03',1,NULL) 
	,('3170707031','林荷秀','女','1997-06-28',3,NULL)
	,('3170707032','任可欣','女','1998-09-18',3,NULL)
	,('3170707033','王昭晖','女','1998-09-19',3,NULL) 
	,('3170707034','张洁','女','1998-09-11',4,NULL)
	,('3170707036','林晨艺','女','1998-09-12',4,NULL)
	,('3170707038','许婷婷','女','1998-09-13',4,NULL) 	
	
----完整备份作业
CREATE PROCEDURE usp_Full
AS
BEGIN
 DECLARE
@FullBkDesc VARCHAR(255);  
 SELECT  
 @FullBkDesc = 'Weekly full backup for '+CONVERT(VARCHAR,GETDATE(),120);
 BACKUP DATABASE EduBase
  TO DISK='E:\EduBase\Full\Fullbackup.bak'
 WITH  
   DESCRIPTION = @FullBkDesc;
END					
						
EXEC msdb.dbo.sp_add_job								
		@job_name = 'jb_EB_FullWeekly'	
														
EXEC msdb.dbo.sp_add_jobstep								
		@job_name = 'jb_EB_FullWeekly'				
		,@step_name = 'js_EB_FullBackup'				
		,@step_id = 1						
		,@database_name = 'EduBase'				
		,@subsystem = 'TSQL'					
		,@command = 						
			'EXEC EduBase.dbo.usp_Full'				
							
EXEC msdb.dbo.sp_add_jobschedule							
		@job_name = 'jb_EB_FullWeekly'				
		,@name = 'jc_EB_FullBackup_Sunday2200'			
		,@freq_type = 8						
		,@freq_interval = 1						
		,@freq_recurrence_factor = 1							
		,@active_start_time = 220000	
															
EXEC msdb.dbo.sp_add_jobserver							
		@job_name = 'jb_EB_FullWeekly'				
		,@server_name = @@SERVERNAME;	
		

CREATE PROCEDURE usp_log
AS
BEGIN
DECLARE 
@LogBkDesc VARCHAR(MAX)		
 SELECT  
@LogBkDesc = 'Hourly log backup for '+CONVERT(VARCHAR,GETDATE(),120);
BACKUP LOG EduBase 
TO DISK='E:\EduBase\Log\logBackup.bak'
WITH  
DESCRIPTION = @LOGBkDesc;
END

EXEC msdb.dbo.sp_add_job							
		@job_name ='jobhour'					

EXEC msdb.dbo.sp_add_jobstep								
		@job_name = 'jobhour'					
		,@step_name = 'LogBackup'					
		,@database_name = 'EduBase'				
		,@subsystem = 'TSQL'					
		,@command = 						
			'EXEC EduBase.dbo.usp_log'							
EXEC msdb.dbo.sp_add_jobschedule
    	@job_name = 'jobhour'					
	    ,@name = 'jc_EduBase_LOGBackup'					
		,@freq_type = 4			
		,@freq_interval = 1			
		,@freq_subday_type = 0x8			
		,@freq_subday_interval = 1					

EXEC msdb.dbo.sp_add_jobserver						
		@job_name = 'jobhour'					
		,@server_name = @@SERVERNAME;


----执行还原
RESTORE DATABASE EduBase		
		FROM DISK = 'E:\EduBase\Full\FullBackup.bak'
		WITH	
			FILE = 1   
			,REPLACE	
			,NORECOVERY;	
RESTORE DATABASE EduBase		
		FROM DISK = 'E:\EduBase\Log\logBackup.bak'	
		WITH	
			FILE = 1
			,NORECOVERY;
RESTORE DATABASE EduBase		
		FROM DISK = 'E:\EduBase\Log\logBackup.bak'	
		WITH	
			FILE = 2
			,RECOVERY;	
			
RESTORE database   EduBase with recovery          ----(立刻还原)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 期末考
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=EduBase;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText =
                "SELECT * FROM tb_Department;"
                + "SELECT * FROM tb_Major;"
                + "SELECT * FROM tb_Class;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataSet dataSet = new DataSet();
            sqlConnection.Open();
            sqlDataAdapter.Fill(dataSet);
            sqlConnection.Close();
            DataTable departmentTable = dataSet.Tables[0];
            DataTable majorTable = dataSet.Tables[1];
            DataTable classTable = dataSet.Tables[2];
            DataRelation[] dataRelations =                                                                
            {
                new DataRelation                                                                            
                    ("Department_Major"                                                                     
                    , departmentTable.Columns["No"]                                                         
                    , majorTable.Columns["DepartmentNo"]                                                    
                    , false)                                                                               
                , new DataRelation                                                                         
                    ("Major_Class"                                                                          
                    , majorTable.Columns["No"]                                                             
                    , classTable.Columns["MajorNo"]                                                         
                    , false)                                                                               
            };
            dataSet.Relations.AddRange(dataRelations);
            this.trv_EducationUnit.Nodes.Clear();
            foreach (DataRow departmentRow in departmentTable.Rows)
            {
                TreeNode departmentNode = new TreeNode();
                departmentNode.Text = departmentRow["Name"].ToString();
                this.trv_EducationUnit.Nodes.Add(departmentNode);
                foreach (DataRow majorRow in departmentRow.GetChildRows("Department_Major"))
                {
                    TreeNode majorNode = new TreeNode();
                    majorNode.Text = majorRow["Name"].ToString();
                    departmentNode.Nodes.Add(majorNode);
                    foreach (DataRow classRow in majorRow.GetChildRows("Major_Class"))
                    {
                        TreeNode classNode = new TreeNode();
                        classNode.Text = classRow["Name"].ToString();
                        classNode.Tag = classRow["No"];
                        majorNode.Nodes.Add(classNode);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=EduBase;Integrated Security=sspi";
            SqlCommand insertCommand = new SqlCommand();
            insertCommand.Connection = sqlConnection;
            insertCommand.CommandText =
                "INSERT tb_Student"
                + "(No,Name,Gender,BirthDate,ClassNo,Speciality)"
                + " VALUES(@No,@Name,@Gender,@BirthDate,@Class,@Speciality);";
            insertCommand.Parameters.Add("@No", SqlDbType.Char, 10, "No");
            insertCommand.Parameters.Add("@Name", SqlDbType.VarChar, 0, "Name");
            insertCommand.Parameters.Add("@Gender", SqlDbType.VarChar, 0, "Gender");
            insertCommand.Parameters.Add("@BirthDate", SqlDbType.VarChar, 0, "BirthDate");
            insertCommand.Parameters.Add("@Class", SqlDbType.VarChar, 0, "ClassNo");
            insertCommand.Parameters.Add("@Speciality", SqlDbType.VarChar, 0, "Speciality");
            SqlCommand updateCommand = new SqlCommand();
            updateCommand.Connection = sqlConnection;
            updateCommand.CommandText =
                "UPDATE tb_Student"
                + " SET No=@NewNo,Name=@Name,Gender=@Gender,BirthDate=@BirthDate,ClassNo=@Class,Speciality=@Speciality"
                + " WHERE No=@OldNo;";
            updateCommand.Parameters.Add("@NewNo", SqlDbType.Char, 10, "No");
            updateCommand.Parameters.Add("@Name", SqlDbType.VarChar, 0, "Name");
            updateCommand.Parameters.Add("@Gender", SqlDbType.VarChar, 0, "Gender");
            updateCommand.Parameters.Add("@BirthDate", SqlDbType.VarChar, 0, "BirthDate");
            updateCommand.Parameters.Add("@Class", SqlDbType.VarChar, 0, "ClassNo");
            updateCommand.Parameters.Add("@Speciality", SqlDbType.VarChar, 0, "Speciality");
            updateCommand.Parameters.Add("@OldNo", SqlDbType.Char, 10, "No");
            updateCommand.Parameters["@OldNo"].SourceVersion = DataRowVersion.Original;
            SqlCommand deleteCommand = new SqlCommand();
            deleteCommand.Connection = sqlConnection;
            deleteCommand.CommandText =
                "DELETE tb_Student"
                + " WHERE No=@No;";
            deleteCommand.Parameters.Add("@No", SqlDbType.Char, 10, "No");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.InsertCommand = insertCommand;
            sqlDataAdapter.UpdateCommand = updateCommand;
            sqlDataAdapter.DeleteCommand = deleteCommand;
            DataTable studentTable1 = (DataTable)this.dgv_Student.DataSource;
            sqlConnection.Open();
            int rowAffected = sqlDataAdapter.Update(studentTable1);
            sqlConnection.Close();
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");  
        }

        private void trv_EducationUnit_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.trv_EducationUnit.SelectedNode.Level == 2)
            {
                int classNo = (int)this.trv_EducationUnit.SelectedNode.Tag;

                SqlConnection sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString =
                    "Server=(local);Database=EduBase;Integrated Security=sspi";
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandText = "SELECT * FROM tb_Student WHERE ClassNo=@ClassNo;";
                sqlCommand.Parameters.AddWithValue("@ClassNo", classNo);
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                sqlDataAdapter.SelectCommand = sqlCommand;
                DataTable studentTable = new DataTable();
                sqlConnection.Open();
                sqlDataAdapter.Fill(studentTable);
                sqlConnection.Close();
                this.dgv_Student.DataSource = studentTable;
                this.dgv_Student.Columns["No"].HeaderText = "学号";
                this.dgv_Student.Columns["Name"].HeaderText = "姓名";
                this.dgv_Student.Columns["Gender"].HeaderText = "性别";
                this.dgv_Student.Columns["BirthDate"].HeaderText = "出生日期";
                this.dgv_Student.Columns["ClassNo"].HeaderText = "班级编号";
                this.dgv_Student.Columns["Speciality"].HeaderText = "爱好";
                this.dgv_Student.Columns[this.dgv_Student.Columns.Count - 1].AutoSizeMode =
                    DataGridViewAutoSizeColumnMode.Fill;
            }
        }
    }
}

源代码:https://gitee.com/DiJun1/QuKuaiLianShiLi

你可能感兴趣的:(数据库期末考源代码)