目錄
利用SQL Sever和 VS C#實(shí)現(xiàn)
一、程序流程圖
二、具體實(shí)現(xiàn):利用SQL Sever和 VS實(shí)現(xiàn),使用C#連接數(shù)據(jù)庫
1、新建一個(gè)名為MySchool的數(shù)據(jù)庫???????
2、C#連接數(shù)據(jù)庫,并實(shí)現(xiàn)對(duì)MySchool數(shù)據(jù)庫的增、刪、改、查操作
(1)主界面?
(2)學(xué)生登錄頁面
(3)個(gè)人主頁
(4)注冊(cè)頁面
(5)管理員登錄頁面
(6)管理員主頁?
(7)登錄日志頁面?
(8)學(xué)生信息頁面
(9)課程信息頁面
(10)學(xué)生成績信息頁面
(11)成績統(tǒng)計(jì)頁面
(12)在主界面點(diǎn)擊右上角的“×”即退出系統(tǒng)。
利用SQL Sever和 VS C#實(shí)現(xiàn)
一、程序流程圖
?圖1:程序流程圖
二、具體實(shí)現(xiàn):利用SQL Sever和 VS實(shí)現(xiàn),使用C#連接數(shù)據(jù)庫
1、新建一個(gè)名為MySchool的數(shù)據(jù)庫
(1)首先,連接數(shù)據(jù)庫,一定一定要選擇“SQL Sever身份驗(yàn)證”方式建立連接,不懂怎么用這種方式連接的可以自行搜索一下,很簡單。
圖2:數(shù)據(jù)庫連接?
?(2)新建一個(gè)名為“MySchool”的數(shù)據(jù)庫,其中含有的表如下:
圖3:MySchool數(shù)據(jù)庫?
對(duì)應(yīng)的SQL語句如下:
USE MySchool;
DROP TABLE IF EXISTS SC /*成績*/
DROP TABLE IF EXISTS Student /*學(xué)生信息*/
DROP TABLE IF EXISTS Course /*課程*/
DROP TABLE IF EXISTS StudentUser /*學(xué)生用戶信息*/
DROP TABLE IF EXISTS Administrator /*管理員用戶信息*/
DROP TABLE IF EXISTS SysLog /*注冊(cè)日志*/
DROP TABLE IF EXISTS SysLog1 /*登陸日志*/
DROP TABLE IF EXISTS AVG1 /*登陸日志*/
CREATE TABLE StudentUser
(
ID NCHAR(20) PRIMARY KEY, /*學(xué)號(hào)*/
PassWord NCHAR(32) , /*密碼*/
Sex CHAR(2) , /*性別*/
Birthday datetime, /*生日*/
UserMobile NCHAR(11), /*電話號(hào)碼*/
);
CREATE TABLE Administrator
(
ID NCHAR(20) PRIMARY KEY, /*工號(hào)*/
PassWord NCHAR(32) , /*密碼*/
Sex CHAR(2), /*性別*/
Birthday datetime, /*生日*/
UserMobile NCHAR(11), /*電話號(hào)碼*/
);
CREATE TABLE SysLog
( [UserMobile]
UserID NCHAR(20) , /*id*/
dentity CHAR(20), /*學(xué)生或管理員*/
DateAndTime datetime, /*注冊(cè)時(shí)間*/
UserOperation NCHAR(200) /*操作方式*/
);
CREATE TABLE SysLog1
(
UserID NCHAR(20) , /*id*/
dentity CHAR(20), /*學(xué)生或管理員*/
DateAndTime datetime, /*登陸時(shí)間*/
UserOperation NCHAR(200) /*登陸操作方式*/
);
CREATE TABLE Student
(
Sno CHAR(9) PRIMARY KEY, /* 列級(jí)完整性約束條件,Sno是主碼*/
Sname CHAR(20) UNIQUE, /* Sname取唯一值*/
Ssex CHAR(2), /*性別*/
Sage SMALLINT, /*年齡*/
Sdept CHAR(20) /*專業(yè)*/
);
CREATE TABLE Course
(
Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40),
Cpno CHAR(4),
Ccredit SMALLINT,
);
CREATE TABLE SC
(
Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT,
PRIMARY KEY (Sno,Cno), /* 主碼由兩個(gè)屬性構(gòu)成,必須作為表級(jí)完整性進(jìn)行定義*/
FOREIGN KEY (Sno) REFERENCES Student(Sno), /* 表級(jí)完整性約束條件,Sno是外碼,被參照表是Student */
FOREIGN KEY (Cno)REFERENCES Course(Cno) /* 表級(jí)完整性約束條件, Cno是外碼,被參照表是Course*/
);
INSERT INTO StudentUser VALUES ('20181101111','123456','女',1999-1-1,'13812345678',NULL);
INSERT INTO Administrator VALUES ('2018110',substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','123456')),3,32),'女',1989-1-1,'13812345687',NULL);
INSERT INTO Administrator VALUES ('2018111',substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','123456')),3,32),'女',1989-2-1,'13812655687',NULL);
INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215121','李勇','男','CS',20);
INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215122','劉晨','女','CS',19);
INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215123','王敏','女','MA',18);
INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215125','張立','男','IS',19);
INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215128','陳冬','男','IS',20);
SELECT * FROM Student
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('1','數(shù)據(jù)庫',NULL,4);
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('2','數(shù)學(xué)',NULL,4);
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('3','信息系統(tǒng)',NULL,4);
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('4','操作系統(tǒng)',NULL,4);
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('5','數(shù)據(jù)結(jié)構(gòu)',NULL,4);
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('6','數(shù)據(jù)處理',NULL,4);
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('7','Pascal語言',NULL,4);
UPDATE Course SET Cpno = '5' WHERE Cno = '1'
UPDATE Course SET Cpno = '1' WHERE Cno = '3'
UPDATE Course SET Cpno = '6' WHERE Cno = '4'
UPDATE Course SET Cpno = '7' WHERE Cno = '5'
UPDATE Course SET Cpno = '6' WHERE Cno = '7'
SELECT * FROM Course
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','1',92);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','2',85);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215121 ','3',88);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215122 ','2',90);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215122 ','3',80);
SELECT * FROM SC
--當(dāng)學(xué)生用戶信息更新,觸發(fā)器啟動(dòng),將更新的內(nèi)容存至注冊(cè)日志
IF(OBJECT_ID('regist_recorder1') is not null) -- 判斷名為 regist_recorder 的觸發(fā)器是否存在
DROP TRIGGER regist_recorder1 -- 刪除觸發(fā)器
GO
CREATE TRIGGER regist_recorder1
ON StudentUser
AFTER
INSERT
AS
declare @UserName nchar(20)
declare @DateTime datetime
declare @UserOperation nchar(200)
declare @dentity CHAR(20)
select @UserName = ID FROM StudentUser
select @DateTime = CONVERT(datetime,GETDATE(),120)
select @dentity ='StudentUser'
declare @op varchar(10)
select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Update'
when exists(select 1 from inserted) and not exists(select 1 from deleted)
then 'Insert'
when not exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Delete' end
select @UserOperation = @op
INSERT INTO SysLog(UserID,dentity,DateAndTime,UserOperation)
VALUES (@UserName,@dentity,@DateTime,@UserOperation)
--當(dāng)管理員信息更新,觸發(fā)器啟動(dòng),將更新的內(nèi)容存至注冊(cè)日志
IF(OBJECT_ID('regist_recorder2') is not null) -- 判斷名為 regist_recorder 的觸發(fā)器是否存在
DROP TRIGGER regist_recorder2 -- 刪除觸發(fā)器
GO
CREATE TRIGGER regist_recorder2
ON Administrator
AFTER
INSERT
AS
declare @UserName nchar(20)
declare @DateTime datetime
declare @UserOperation nchar(200)
declare @dentity CHAR(20)
select @UserName = ID FROM Administrator
select @DateTime = CONVERT(datetime,GETDATE(),120)
select @dentity ='Administrator'
declare @op varchar(10)
select @op=case when exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Update'
when exists(select 1 from inserted) and not exists(select 1 from deleted)
then 'Insert'
when not exists(select 1 from inserted) and exists(select 1 from deleted)
then 'Delete' end
select @UserOperation = @op
INSERT INTO SysLog(UserID,dentity,DateAndTime,UserOperation)
VALUES (@UserName,@dentity,@DateTime,@UserOperation)
--建一個(gè)表存儲(chǔ)各科平均分
--建一個(gè)表存儲(chǔ)各科平均分
CREATE TABLE AVG1
(
Cname CHAR(10), /* 科目*/
avg1 INT
);
INSERT INTO AVG1(Cname,avg1) VALUES ('數(shù)據(jù)庫',NULL);
INSERT INTO AVG1(Cname,avg1) VALUES ('數(shù)學(xué)',NULL);
INSERT INTO AVG1(Cname,avg1) VALUES ('信息系統(tǒng)',NULL);
INSERT INTO AVG1(Cname,avg1) VALUES ('操作系統(tǒng)',NULL);
INSERT INTO AVG1(Cname,avg1) VALUES ('數(shù)據(jù)結(jié)構(gòu)',NULL);
INSERT INTO AVG1(Cname,avg1) VALUES ('數(shù)據(jù)處理',NULL);
INSERT INTO AVG1(Cname,avg1) VALUES ('Pascal語言',NULL);
--將成績信息通過下列存儲(chǔ)過程算出各科平均分并存儲(chǔ)至AVG表
IF (exists (select * from sys.objects where name = 'COURSE_AVG1'))
DROP PROCEDURE COURSE_AVG1
GO
CREATE PROCEDURE COURSE_AVG1
AS
BEGIN TRANSACTION TRANS
DECLARE
@SX INT, /* 數(shù)學(xué)總分 */
@XXXT INT, /* 信息系統(tǒng)總分 */
@CZXT INT, /* 操作系統(tǒng)總分 */
@SJJG INT, /* 數(shù)據(jù)結(jié)構(gòu)總分 */
@SJK_C INT, /* 數(shù)據(jù)庫總分 */
@SJCL INT, /*數(shù)據(jù)處理總分*/
@P INT /*Pascal語言*/
SELECT @SX=AVG(Grade) FROM SC
WHERE Cno='2 '
SELECT @XXXT=AVG(Grade) FROM SC
WHERE Cno='3'
SELECT @CZXT=AVG(Grade) FROM SC
WHERE Cno='4'
SELECT @SJJG=AVG(Grade) FROM SC
WHERE Cno='5'
SELECT @SJK_C=AVG(Grade) FROM SC
WHERE Cno='1'
SELECT @SJCL=AVG(Grade) FROM SC
WHERE Cno='6'
SELECT @P=AVG(Grade) FROM SC
WHERE Cno='7'
BEGIN
UPDATE AVG1 SET avg1=@SJK_C WHERE Cname='數(shù)據(jù)庫'
UPDATE AVG1 SET avg1=@SX WHERE Cname='數(shù)學(xué)'
UPDATE AVG1 SET avg1=@XXXT WHERE Cname='信息系統(tǒng)'
UPDATE AVG1 SET avg1=@CZXT WHERE Cname='操作系統(tǒng)'
UPDATE AVG1 SET avg1=@SJJG WHERE Cname='數(shù)據(jù)結(jié)構(gòu)'
UPDATE AVG1 SET avg1=@SJCL WHERE Cname='數(shù)據(jù)處理'
UPDATE AVG1 SET avg1=@P WHERE Cname='Pascal語言'
COMMIT TRANSACTION TRANS;
END
2、C#連接數(shù)據(jù)庫,并實(shí)現(xiàn)對(duì)MySchool數(shù)據(jù)庫的增、刪、改、查操作
(1)主界面:主界面包含歡迎文本和兩個(gè)可選項(xiàng),如下圖3所示分別為“學(xué)生登錄”和“管理員登錄”,選擇不同的選項(xiàng)則分別進(jìn)入不同的頁面。
?圖4:主界面
對(duì)應(yīng)C#實(shí)現(xiàn)如下:?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class FormLogin : Form
{
public FormLogin()
{
InitializeComponent();
}
private void buttonStuLogin_Click(object sender, EventArgs e)
{
FormStuLogin FormStuLogin = new FormStuLogin(); //學(xué)生登錄窗體
FormStuLogin.Show();//跳轉(zhuǎn)至學(xué)生登錄窗體
this.Hide(); //隱藏原窗體
}
private void buttonManagerLogin_Click(object sender, EventArgs e)
{
FormManagerLogin FormManagerLogin = new FormManagerLogin(); //管理員登錄窗體
FormManagerLogin.Show();//跳轉(zhuǎn)至管理員登錄窗體
this.Hide();
}
private void FormLogin_FormClosing_1(object sender, FormClosingEventArgs e)
{
}
private void FormLogin_FormClosed(object sender, FormClosedEventArgs e)
{
while (MessageBox.Show("即將退出系統(tǒng),您確認(rèn)退出嗎?", "提示!", MessageBoxButtons.YesNo)== DialogResult.Yes)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
}
}
(2)學(xué)生登錄頁面:在主界面選擇學(xué)生登錄按鈕則進(jìn)入學(xué)生登錄頁面,頁面功能如下圖所示。若用戶有該系統(tǒng)的賬號(hào),則可選擇直接登錄:先輸入用戶名,密碼以及驗(yàn)證碼,三者均正確才能登陸成功。
圖5:學(xué)生登錄頁面
圖6:學(xué)生登錄成功
若其中一項(xiàng)不正確則會(huì)彈窗提示你重新輸入,如下圖所示:
圖7:學(xué)生登錄失敗
對(duì)應(yīng)C#實(shí)現(xiàn)如下(連接數(shù)據(jù)庫的代碼實(shí)現(xiàn)部分大家根據(jù)自身情況自行修改,下同):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class FormStuLogin : Form
{
public FormStuLogin()
{
InitializeComponent();
}
public string code;
private string EncryptWithMD5(string source)//MD5加密
{
byte[] sor = Encoding.UTF8.GetBytes(source);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
strbul.Append(result[i].ToString("x2"));//加密結(jié)果"x2"結(jié)果為32位,"x3"結(jié)果為48位,"x4"結(jié)果為64位
}
return strbul.ToString();
}
private void buttonLogin_Click(object sender, EventArgs e)
{
string username = tBUserName.Text.Trim(); //取出賬號(hào)
string password = EncryptWithMD5(tBPassword.Text.Trim()); //取出密碼并加密
string myConnString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";
SqlConnection sqlConnection = new SqlConnection(myConnString); //實(shí)例化連接對(duì)象
sqlConnection.Open();
string sql = "select ID,PassWord from StudentUser where ID = '" + username + "' and PassWord = '" + password + "'"; //編寫SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();//讀取數(shù)據(jù)
if (sqlDataReader.HasRows && tBVerifyCode.Text == code)//驗(yàn)證是否有該用戶及密碼且驗(yàn)證碼正確
{
MessageBox.Show("歡迎使用!"); //登錄成功
StuMain stuMain = new StuMain();
stuMain.GetId(username);
stuMain.Show();//顯示下一界面
this.Hide();
}
else if (sqlDataReader.HasRows && tBVerifyCode.Text != code)
{
MessageBox.Show("驗(yàn)證碼錯(cuò)誤,登錄失??!");
return;
}
else
{
MessageBox.Show("賬號(hào)密碼有誤,登錄失敗!");
return;
}
sqlDataReader.Close();
sql = "insert into SysLog1 values ( '" + username + "' , 'Student','" + DateTime.Now + "' , '" + "Login" + "')"; //編寫SQL命令,把登陸信息存入登錄日志
SqlCommand sqlCommand1 = new SqlCommand(sql, sqlConnection);
sqlCommand1.ExecuteNonQuery();
sqlConnection.Close();
}
private void buttonReg_Click(object sender, EventArgs e)
{
this.Hide();
RegisterForm registerForm = new RegisterForm();
registerForm.Show();
}
private void FormStuLogin_Load_1(object sender, EventArgs e)
{
//隨機(jī)實(shí)例化
Random ran = new Random();
int number;
char code1;
//取五個(gè)數(shù)
for (int i = 0; i < 5; i++)
{
number = ran.Next();
if (number % 2 == 0)
code1 = (char)('0' + (char)(number % 10));
else
code1 = (char)('A' + (char)(number % 26)); //轉(zhuǎn)化為字符
this.code += code1.ToString();
}
lbVerifyCode.Text = code;
}
private void lbVerifyCode_Click(object sender, EventArgs e)
{
}
private void buttonCancel_Click_1(object sender, EventArgs e)
{
this.Close();
}
private void FormStuLogin_FormClosing_1(object sender, FormClosingEventArgs e)
{
FormLogin m2 = new FormLogin();
m2.Show();
}
private void lbPassword_Click(object sender, EventArgs e)
{
}
}
}
(3)個(gè)人主頁:學(xué)生用戶若成功登錄則進(jìn)入個(gè)人主頁,個(gè)人主頁共包含“個(gè)人基本信息”和“成績單”兩個(gè)分頁,所展示的信息如下圖所示,學(xué)生能夠看到自己的個(gè)人信息和個(gè)人成績,以及各門課程的詳細(xì)信息。
圖8:個(gè)人主頁——個(gè)人信息頁
圖9:個(gè)人主頁——成績單頁
對(duì)應(yīng)C#實(shí)現(xiàn)如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class StuMain : Form
{
public StuMain()
{
InitializeComponent();
}
private void buttonExit_Click(object sender, EventArgs e)
{
this.Close();
}
string Id;
public void GetId(string id)//上個(gè)窗體調(diào)用此函數(shù),將id值傳過來
{
lbStuId.Text = id;
lbStuId.Refresh();
Id = id;
}
private void StuMain_Load_1(object sender, EventArgs e)
{
// TODO: 這行代碼將數(shù)據(jù)加載到表“mySchoolDataSet.Course”中。您可以根據(jù)需要移動(dòng)或移除它。
this.courseTableAdapter.Fill(this.mySchoolDataSet.Course);
string name, sex, dept, tel;
int age;
string connString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";//數(shù)據(jù)庫連接字符串
SqlConnection conn = new SqlConnection(connString);//創(chuàng)建connection對(duì)象
conn.Open();//打開數(shù)據(jù)庫
//創(chuàng)建數(shù)據(jù)庫命令
SqlCommand cmd = conn.CreateCommand();
//創(chuàng)建查詢語句
cmd.CommandText = "select * from Student where Sno = '" + Id + "';select * from StudentUser where ID = '" + Id + "'";
//從數(shù)據(jù)庫中讀取數(shù)據(jù)流存入reader中
SqlDataReader reader = cmd.ExecuteReader();
//從reader中讀取下一行數(shù)據(jù),如果沒有數(shù)據(jù),reader.Read()返回flase
while (reader.Read())
{
name = reader.GetString(reader.GetOrdinal("Sname"));
sex = reader.GetString(reader.GetOrdinal("Ssex"));
age = reader.GetInt16(reader.GetOrdinal("Sage"));
dept = reader.GetString(reader.GetOrdinal("Sdept"));
lbStuName.Text = name;
lbStuName.Refresh();
lbStuSex.Text = sex;
lbStuAge.Text = age.ToString();
lbStuDept.Text = dept;
break;
}
reader.NextResult();//執(zhí)行下一句操作
//從reader中讀取下一行數(shù)據(jù),如果沒有數(shù)據(jù),reader.Read()返回flase
while (reader.Read())
{
tel = reader.GetString(reader.GetOrdinal("UserMobile"));
lbPhone.Text = tel;
break;
}
reader.Close();
SqlDataAdapter dap = new SqlDataAdapter("select * from SC where Sno='" + Id + "'", conn);//查詢
DataSet ds = new DataSet();//創(chuàng)建DataSet對(duì)象
dap.Fill(ds);//填充DataSet數(shù)據(jù)集
dataGridView1.DataSource = ds.Tables[0].DefaultView;//顯示查詢后的數(shù)據(jù)
conn.Close();
int i = 0;
int x = 0, h = 0;
int a;
for (; i < ds.Tables[0].Rows.Count; i++)//讀取DataSet中的指定數(shù)據(jù)
{
x += int.Parse(ds.Tables[0].Rows[i][2].ToString());
if (int.Parse(ds.Tables[0].Rows[i][2].ToString()) > 59)
h++;
}
if (i == 0) a = 0;
else a = x / i;
lbPassnum.Text = h.ToString();
lbAvgSco.Text = a.ToString();
}
private void StuMain_FormClosing_1(object sender, FormClosingEventArgs e)
{
FormLogin m2 = new FormLogin();
m2.Show();
}
private void StuMain_FormClosed(object sender, FormClosedEventArgs e)
{
}
}
}
(4)注冊(cè)頁面:若學(xué)生用戶沒有登陸賬號(hào),則需在學(xué)生登錄界面選擇注冊(cè)按鈕,進(jìn)入注冊(cè)界面輸入相應(yīng)信息進(jìn)行注冊(cè)。成功注冊(cè)的前提是你為該校的學(xué)生且各信息已填寫完整。
圖10:注冊(cè)頁面
圖11:注冊(cè)成功
圖12:注冊(cè)失敗
對(duì)應(yīng)C#實(shí)現(xiàn)如下:??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class RegisterForm : Form
{
public RegisterForm()
{
InitializeComponent();
}
private void buttonRegister_Click(object sender, EventArgs e)
{
if (tBStuId.Text.Trim() != "" && tBPassword.Text.Trim() != "" && cBSex.Text.Trim() != "" && tBPhoneNum.Text.Trim() != "")
{//確認(rèn)一些信息輸入非空
try
{
string connString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";//數(shù)據(jù)庫連接字符串
SqlConnection connection = new SqlConnection(connString);//創(chuàng)建connection對(duì)象
string username = tBStuId.Text.Trim(); //取出賬號(hào)
string sex = cBSex.Text.Trim(); //取出性別
connection.Open();
string sql1 = "select Sno,Ssex from Student where Sno = '" + username + "' and Ssex = '" + sex + "'"; //編寫SQL命令,查找學(xué)生信息中是否有該用戶
SqlCommand sqlCommand1 = new SqlCommand(sql1, connection);//確認(rèn)是否有該學(xué)員
SqlDataReader sqlDataReader1 = sqlCommand1.ExecuteReader();
bool a = sqlDataReader1.HasRows;
sqlDataReader1.Close();
if (a)
{
string sql2 = "select ID from StudentUser where ID = '" + username + "'"; //編寫SQL命令
SqlCommand sqlCommand2 = new SqlCommand(sql2, connection);//確認(rèn)是否已經(jīng)注冊(cè)過
SqlDataReader sqlDataReader2 = sqlCommand2.ExecuteReader();
if (!sqlDataReader2.HasRows)
{
string sql3 = "insert into StudentUser (ID, PassWord ,Sex, UserMobile, Birthday) " +
"values (@userid, @userpassword,@sex,@usermobile,@Birthday)";
SqlCommand command = new SqlCommand(sql3, connection);
SqlParameter sqlParameter = new SqlParameter("@userid", tBStuId.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@userpassword", EncryptWithMD5(tBPassword.Text));
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@sex", cBSex.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@usermobile", tBPhoneNum.Text);
command.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@Birthday", dateTimePicker1.Value);
command.Parameters.Add(sqlParameter);
sqlDataReader2.Close();
//打開數(shù)據(jù)庫連接
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("注冊(cè)成功");
}
else
{
MessageBox.Show("該用戶已注冊(cè)。");
}
}
else
{
MessageBox.Show("我校無該學(xué)員");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Close();
}
else
{
MessageBox.Show("請(qǐng)將信息填寫完整!");
}
}
public Byte[] mybyte = new byte[0];
public static string EncryptWithMD5(string source)
{
byte[] sor = Encoding.UTF8.GetBytes(source);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
strbul.Append(result[i].ToString("x2"));//加密結(jié)果"x2"結(jié)果為32位,"x3"結(jié)果為48位,"x4"結(jié)果為64位
}
return strbul.ToString();
}
private void tBPassword_Leave(object sender, EventArgs e)//校驗(yàn)密碼格式
{
if (tBStuId.Text.Trim() != "")
{
//使用regex(正則表達(dá)式)進(jìn)行格式設(shè)置 至少有數(shù)字、大寫字母、小寫字母各一個(gè)。最少3個(gè)字符、最長20個(gè)字符。
Regex regex = new Regex(@"(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{3,20}");
if (regex.IsMatch(tBPassword.Text))//判斷格式是否符合要求
{
//MessageBox.Show("輸入密碼格式正確!");
}
else
{
MessageBox.Show("至少有數(shù)字、大寫字母、小寫字母各一個(gè)。最少3個(gè)字符、最長20個(gè)字符!");
tBPassword.Focus();
}
}
else
{
MessageBox.Show("請(qǐng)?zhí)顚懨艽a!");
}
}
private void RegisterForm_FormClosing(object sender, FormClosingEventArgs e)
{
FormStuLogin m2 = new FormStuLogin();
m2.Show();
}
private void buttonCancel_Click_1(object sender, EventArgs e)
{
this.Close();
}
}
}
(5)管理員登錄頁面:若您的身份為管理員,則可在主界面選擇“管理員登錄”選項(xiàng),進(jìn)入管理員登錄界面。與學(xué)生登錄相似,管理員登錄成功的前提是正確輸入各類信息。但管理員登錄不提供“注冊(cè)”選項(xiàng),因?yàn)楣芾韱T身份必須由系統(tǒng)授予——即在數(shù)據(jù)庫中填入相應(yīng)信息才可成為管理員。
圖13:管理員登錄頁面
圖14:管理員登錄成功
對(duì)應(yīng)C#實(shí)現(xiàn)如下:??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class FormManagerLogin : Form
{
public FormManagerLogin()
{
InitializeComponent();
}
public string code;
private void buttonLogin_Click(object sender, EventArgs e)
{
string username = tBWorknum.Text.Trim(); //取出賬號(hào)
string password = EncryptWithMD5(tBPassword.Text.Trim()); //取出密碼并加密
string myConnString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";
SqlConnection sqlConnection = new SqlConnection(myConnString); //實(shí)例化連接對(duì)象
sqlConnection.Open();
string sql = "select ID,PassWord from Administrator where ID = '" + username + "' and PassWord = '" + password + "'"; //編寫SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows && tBVerifyCode.Text == code)
{
MessageBox.Show("歡迎使用!"); //登錄成功
ManagerMain managerMain = new ManagerMain();
managerMain.Show();
this.Hide();
}
else if (sqlDataReader.HasRows && tBVerifyCode.Text != code)
{
MessageBox.Show("驗(yàn)證碼錯(cuò)誤,登錄失敗!");
return;
}
else
{
MessageBox.Show("賬號(hào)密碼有誤,登錄失??!");
return;
}
sqlDataReader.Close();
sql = "insert into SysLog1 values ( '" + username + "' , 'Administrator','" + DateTime.Now + "' , '" + "Login" + "')"; //編寫SQL命令
SqlCommand sqlCommand1 = new SqlCommand(sql, sqlConnection);
sqlCommand1.ExecuteNonQuery();
sqlConnection.Close();
}
private string EncryptWithMD5(string source)
{
byte[] sor = Encoding.UTF8.GetBytes(source);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
strbul.Append(result[i].ToString("x2"));//加密結(jié)果"x2"結(jié)果為32位,"x3"結(jié)果為48位,"x4"結(jié)果為64位
}
return strbul.ToString();
}
private void FormManagerLogin_Load_1(object sender, EventArgs e)
{
//隨機(jī)實(shí)例化
Random ran = new Random();
int number;
char code1;
//取五個(gè)數(shù)
for (int i = 0; i < 5; i++)
{
number = ran.Next();
if (number % 2 == 0)
code1 = (char)('0' + (char)(number % 10));
else
code1 = (char)('A' + (char)(number % 26)); //轉(zhuǎn)化為字符
this.code += code1.ToString();
}
lbVerifyCode.Text = code;
}
private void FormManagerLogin_FormClosing_1(object sender, FormClosingEventArgs e)
{
FormLogin m2 = new FormLogin();
m2.Show();
}
private void buttonCancel_Click_1(object sender, EventArgs e)
{
this.Close();
}
}
}
(6)管理員主頁:管理員主頁一共包含五個(gè)可選項(xiàng),如下圖所示。其中,對(duì)于“學(xué)生信息”,“課程信息”,“學(xué)生成績”,管理員可按需進(jìn)行增、刪、改、查操作。
圖15:管理員主頁
對(duì)應(yīng)C#實(shí)現(xiàn)如下:??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class ManagerMain : Form
{
public ManagerMain()
{
InitializeComponent();
}
private void buttonLog_Click(object sender, EventArgs e)
{
this.Hide();
LogMain m = new LogMain();
m.Show();
}
private void buttonStu_Click(object sender, EventArgs e)
{
this.Hide();
StuMessageMain m = new StuMessageMain();
m.Show();
}
private void buttonCourse_Click(object sender, EventArgs e)
{
this.Hide();
CourseMessageMain m = new CourseMessageMain();
m.Show();
}
private void buttonGrade_Click(object sender, EventArgs e)
{
this.Hide();
StuGradeMain m = new StuGradeMain();
m.Show();
}
private void buttonExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void ManagerMain_FormClosed(object sender, FormClosedEventArgs e)
{
//Application.Exit();
}
private void ManagerMain_FormClosing(object sender, FormClosingEventArgs e)
{
FormLogin m2 = new FormLogin();
m2.Show();
}
private void ManagerMain_Load(object sender, EventArgs e)
{
}
}
}
(7)登錄日志頁面:登錄日志為管理員提供該系統(tǒng)所有用戶的登錄記錄
圖16:登錄日志頁面
對(duì)應(yīng)C#實(shí)現(xiàn)如下:??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class LogMain : Form
{
public LogMain()
{
InitializeComponent();
}
private void LogMain_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數(shù)據(jù)加載到表“mySchoolDataSet1.SysLog1”中。您可以根據(jù)需要移動(dòng)或移除它。
this.sysLog1TableAdapter.Fill(this.mySchoolDataSet1.SysLog1);
}
private void buttonReturn_Click(object sender, EventArgs e)
{
this.Close();
}
private void LogMain_FormClosing(object sender, FormClosingEventArgs e)
{
ManagerMain m2 = new ManagerMain();
m2.Show();
}
}
}
(8)學(xué)生信息頁面:為管理員提供學(xué)生信息,且管理員可按需進(jìn)行增、刪、改、查操作。
圖17:學(xué)生信息頁面
對(duì)應(yīng)C#實(shí)現(xiàn)如下:??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class StuMessageMain : Form
{
public StuMessageMain()
{
InitializeComponent();
}
private void StuMessageMain_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數(shù)據(jù)加載到表“mySchoolDataSet2.Student”中。您可以根據(jù)需要移動(dòng)或移除它。
this.studentTableAdapter.Fill(this.mySchoolDataSet2.Student);
}
private void buttonReturn_Click(object sender, EventArgs e)
{
this.Close();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
String StuID = tBStuId.Text.Trim();//讀取文本框的值
String StuName = tBStuName.Text.Trim();
String StuSex = tBStuSex.Text.Trim();
String StuSdept = tBStuDept.Text.Trim();
int StuAge = int.Parse(tBStuAge.Text.Trim());
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
try
{
con.Open();
string insertStr = "INSERT INTO Student (Sno,Sname,Ssex,Sdept,Sage) " +
"VALUES ('" + StuID + "','" + StuName + "','" + StuSex + "','" + StuSdept + "'," + StuAge + ")";
SqlCommand cmd = new SqlCommand(insertStr, con);//通過sql語句對(duì)表添加數(shù)值
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("輸入數(shù)據(jù)違反要求!");
}
finally
{
con.Dispose();
tBStuId.Text = "";
tBStuName.Text = "";
tBStuSex.Text = "";
tBStuAge.Text = "";
tBStuDept.Text = "";
}
this.studentTableAdapter.Fill(this.mySchoolDataSet2.Student);//刷新表
}
private void buttonDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當(dāng)前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno=" + select_id;//sql刪除語句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("請(qǐng)正確選擇行!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.mySchoolDataSet2.Student);
}
private void buttonEdit_Click(object sender, EventArgs e)
{
String StuID = tBStuId.Text.Trim();
String StuName = tBStuName.Text.Trim();
String StuSex = tBStuSex.Text.Trim();
String StuSdept = tBStuDept.Text.Trim();
int StuAge = 0;
if (tBStuAge.Text != "")
{
StuAge = int.Parse(tBStuAge.Text.Trim());
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
try
{
con.Open();
if(StuName != "")
{
string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if (StuSex != "")
{
string insertStr = "UPDATE Student SET Ssex = '" + StuSex + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if (StuSdept != "")
{
string insertStr = "UPDATE Student SET Sdept = '" + StuSdept + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if (tBStuAge.Text != "")
{
string insertStr = "UPDATE Student SET Sage = '" + StuAge + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
}
catch
{
MessageBox.Show("輸入數(shù)據(jù)違反要求!");
}
finally
{
con.Dispose();
tBStuId.Text = "";
tBStuName.Text = "";
tBStuSex.Text = "";
tBStuAge.Text = "";
tBStuDept.Text = "";
}
this.studentTableAdapter.Fill(this.mySchoolDataSet2.Student);
}
private void buttonSearch_Click(object sender, EventArgs e)
{
String StuID = tBStuId.Text.Trim();
String conn = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword";
SqlConnection sqlConnection = new SqlConnection(conn); //實(shí)例化連接對(duì)象
try
{
sqlConnection.Open();
String select_by_id = "select * from Student where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查詢語句有誤,請(qǐng)認(rèn)真檢查SQL語句!");
}
finally
{
sqlConnection.Close();
tBStuId.Text = "";
}
}
private void StuMessageMain_FormClosing(object sender, FormClosingEventArgs e)
{
ManagerMain m2 = new ManagerMain();
m2.Show();
}
}
}
(9)課程信息頁面:為管理員提供課程信息,且管理員可按需進(jìn)行增、刪、改、查操作。
圖18:課程信息頁面
對(duì)應(yīng)C#實(shí)現(xiàn)如下:??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class CourseMessageMain : Form
{
public CourseMessageMain()
{
InitializeComponent();
}
private void CourseMessageMain_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數(shù)據(jù)加載到表“mySchoolDataSet3.Course”中。您可以根據(jù)需要移動(dòng)或移除它。
this.courseTableAdapter.Fill(this.mySchoolDataSet3.Course);
}
private void buttonAdd_Click(object sender, EventArgs e)
{
String cno = tBCno.Text.Trim();
String cn = tBCname.Text.Trim();
String cpo = tBCpno.Text.Trim();
int cd = 0;
if(tBCcredit.Text != "")
{
cd = int.Parse(tBCcredit.Text.Trim());
}
if (cpo == "")
{
cpo = "NULL";
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
try
{
if(cno == "" || cn =="" || cd == 0)
{
MessageBox.Show("輸入數(shù)據(jù)違反要求!");
}
else
{
con.Open();
string insertStr = "INSERT INTO Course (Cno,Cname,Cpno,Ccredit) " +
"VALUES ('" + cno + "','" + cn + "','" + cpo + "','" + cd + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
}
catch
{
}
finally
{
con.Dispose();
tBCno.Text = "";
tBCname.Text = "";
tBCpno.Text = "";
tBCcredit.Text = "";
}
this.courseTableAdapter.Fill(this.mySchoolDataSet3.Course);
}
private void buttonDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當(dāng)前行第一列的值,也就是ID
string delete_by_id = "delete from Course where Cno=" + select_id;//sql刪除語句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("請(qǐng)正確選擇行!");
}
finally
{
con.Dispose();
}
this.courseTableAdapter.Fill(this.mySchoolDataSet3.Course);
}
private void buttonEdit_Click(object sender, EventArgs e)
{
String cno = tBCno.Text.Trim();
String cn = tBCname.Text.Trim();
String cpo = tBCpno.Text.Trim();
int cd = 0;
if(tBCcredit.Text != "")
{
cd = int.Parse(tBCcredit.Text.Trim());
}
if (cpo == "")
{
cpo = "NULL";
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
con.Open();
if(cn != "")
{
string insertStr = "UPDATE Course SET Cname = '" + cn + "' WHERE Cno = '" + cno + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if (cpo != "")
{
string insertStr = "UPDATE Course SET Cpno = '" + cpo + "' WHERE Cno = '" + cno + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if (tBCcredit.Text != "")
{
string insertStr = "UPDATE Course SET Ccredit = '" + cd + "' WHERE Cno = '" + cno + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
if(cno == "")
{
MessageBox.Show("輸入數(shù)據(jù)違反要求!");
}
con.Dispose();
tBCno.Text = "";
tBCname.Text = "";
tBCpno.Text = "";
tBCcredit.Text = "";
this.courseTableAdapter.Fill(this.mySchoolDataSet3.Course);
}
private void buttonSearch_Click(object sender, EventArgs e)
{
String cno = tBCno.Text.Trim();
String conn = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword";
SqlConnection sqlConnection = new SqlConnection(conn); //實(shí)例化連接對(duì)象
try
{
sqlConnection.Open();
String select_by_id = "select * from Course where Cno='" + cno + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查詢語句有誤,請(qǐng)認(rèn)真檢查SQL語句!");
}
finally
{
sqlConnection.Close();
tBCno.Text = "";
}
}
private void buttonReturn_Click(object sender, EventArgs e)
{
this.Close();
}
private void CourseMessageMain_FormClosing(object sender, FormClosingEventArgs e)
{
ManagerMain m2 = new ManagerMain();
m2.Show();
}
}
}
(10)學(xué)生成績信息頁面:為管理員提供學(xué)生成績信息,且管理員可按需進(jìn)行增、刪、改、查操作。
圖19:學(xué)生成績信息頁面
對(duì)應(yīng)C#實(shí)現(xiàn)如下:??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class StuGradeMain : Form
{
public StuGradeMain()
{
InitializeComponent();
}
private void StuGradeMain_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數(shù)據(jù)加載到表“mySchoolDataSet4.SC”中。您可以根據(jù)需要移動(dòng)或移除它。
this.sCTableAdapter.Fill(this.mySchoolDataSet4.SC);
}
private void buttonAdd_Click(object sender, EventArgs e)
{
String sno = tBSId.Text.Trim();
String cno = tBCno.Text.Trim();
int gra = int.Parse(tBGrade.Text.Trim());
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
try
{
con.Open();
string insertStr = "INSERT INTO SC (Sno,Cno,Grade) " +
"VALUES ('" + sno + "','" + cno + "','" + gra + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("輸入數(shù)據(jù)違反要求!");
}
finally
{
con.Dispose();
tBSId.Text = "";
tBCno.Text = "";
tBGrade.Text = "";
}
this.sCTableAdapter.Fill(this.mySchoolDataSet4.SC);
}
private void buttonDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當(dāng)前行第一列的值,也就是ID
string select_cid = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//選擇的當(dāng)前行第二列的值,也就是CID
string deletesql = "delete from SC where Sno='" + select_id + "' AND Cno='" + select_cid + "'";//sql刪除語句
SqlCommand cmd = new SqlCommand(deletesql, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("請(qǐng)正確選擇行!");
}
finally
{
con.Dispose();
}
this.sCTableAdapter.Fill(this.mySchoolDataSet4.SC);
}
private void buttonEdit_Click(object sender, EventArgs e)
{
String sno = tBSId.Text.Trim();
String cno = tBCno.Text.Trim();
int gra = int.Parse(tBGrade.Text.Trim());
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword");
try
{
con.Open();
string insertStr = "UPDATE SC SET Grade='" + gra + "' WHERE Sno = '" + sno + "' AND Cno = '" + cno + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("輸入數(shù)據(jù)違反要求!");
}
finally
{
con.Dispose();
tBSId.Text = "";
tBCno.Text = "";
tBGrade.Text = "";
}
this.sCTableAdapter.Fill(this.mySchoolDataSet4.SC);
}
private void buttonSearch_Click(object sender, EventArgs e)
{
String sno = tBSId.Text.Trim();
String cno = tBCno.Text.Trim();
String conn = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=yourpassword";
SqlConnection sqlConnection = new SqlConnection(conn); //實(shí)例化連接對(duì)象
try
{
String selectsql = "select * from SC where Sno='" + sno + "'";
sqlConnection.Open();
if(sno != "" && cno == "")
{
selectsql = "select * from SC where Sno='" + sno + "'";
}
if (sno != "" && cno != "")
{
selectsql = "select * from SC where Sno='" + sno + "' AND Cno='" + cno + "'";
}
SqlCommand sqlCommand = new SqlCommand(selectsql, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查詢語句有誤,請(qǐng)認(rèn)真檢查SQL語句!");
}
finally
{
sqlConnection.Close();
tBSId.Text = "";
tBCno.Text = "";
}
}
private void buttonMes_Click(object sender, EventArgs e)
{
string connString = "Data Source=.;Initial Catalog=MySchool;Persist Security Info=True;User ID=sa;Password=yourpassword";//數(shù)據(jù)庫連接字符串
SqlConnection connection = new SqlConnection(connString);//創(chuàng)建connection對(duì)象
string sql1 = "EXEC COURSE_AVG1";//編寫SQL命令
SqlCommand sqlCommand1 = new SqlCommand(sql1, connection);
connection.Open();
sqlCommand1.ExecuteNonQuery();
connection.Close();
this.Hide();
StatisiticMain m2 = new StatisiticMain();
m2.Show();
}
private void buttonReturn_Click(object sender, EventArgs e)
{
this.Close();
}
private void StuGradeMain_FormClosing(object sender, FormClosingEventArgs e)
{
ManagerMain m2 = new ManagerMain();
m2.Show();
}
}
}
(11)成績統(tǒng)計(jì)頁面:為管理員提供統(tǒng)計(jì)信息。
圖20:成績統(tǒng)計(jì)頁面
對(duì)應(yīng)C#實(shí)現(xiàn)如下:??
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SchoolManage
{
public partial class StatisiticMain : Form
{
public StatisiticMain()
{
InitializeComponent();
}
private void StatisiticMain_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數(shù)據(jù)加載到表“mySchoolDataSet6.AVG1”中。您可以根據(jù)需要移動(dòng)或移除它。
this.aVG1TableAdapter.Fill(this.mySchoolDataSet6.AVG1);
}
private void buttonReturn_Click(object sender, EventArgs e)
{
this.Close();
}
private void StatisiticMain_FormClosing(object sender, FormClosingEventArgs e)
{
StuGradeMain m2 = new StuGradeMain();
m2.Show();
}
}
}
(12)在主界面點(diǎn)擊右上角的“×”即退出系統(tǒng)。
(注:以上頁面中的“返回”、“退出”、“取消”、“×”選項(xiàng)均使得頁面從當(dāng)前界面返回到上一界面或上一級(jí)界面,并不使程序退出,僅主界面的“×”號(hào)可退出系統(tǒng)。)
圖21:退出系統(tǒng)文章來源:http://www.zghlxwxcb.cn/news/detail-800344.html
以上就是本次數(shù)據(jù)庫課程設(shè)計(jì)的全部內(nèi)容啦,歡迎小伙伴們交流心得,批評(píng)指正。文章來源地址http://www.zghlxwxcb.cn/news/detail-800344.html
轉(zhuǎn)載請(qǐng)注明出處,未經(jīng)同意禁止轉(zhuǎn)載!
到了這里,關(guān)于數(shù)據(jù)庫課程設(shè)計(jì)——學(xué)生信息管理系統(tǒng)C#,SQL Sever的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!