发布网友 发布时间:2022-04-09 13:42
共2个回答
懂视网 时间:2022-04-09 18:03
<?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 </configSections> 5 <connectionStrings> 6 <add name="sql_homework_end.Properties.Settings.sql_homework_endConnectionString" 7 connectionString="Data Source=.;Initial Catalog=sql_homework_end;Integrated Security=True" 8 providerName="System.Data.SqlClient" /> 9 </connectionStrings> 10 <startup> 11 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 12 </startup> 13 </configuration> 连接字符串2.建立数据库公共类:
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Linq; 7 using System.Security.Cryptography; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace sql_homework_end 12 { 13 class sqlhelper 14 { 15 private static string connStr = ConfigurationManager.ConnectionStrings["sql_homework_end.Properties.Settings.sql_homework_endConnectionString"].ConnectionString; 16 /// <summary> 17 /// 返回受影响的数据行数 18 /// </summary> 19 /// <param name="sql"></param> 20 /// <returns></returns> 21 public static int ExecuteNoQuery(string sql) 22 { 23 using (SqlConnection conn=new SqlConnection(connStr)) 24 { 25 conn.Open(); 26 using (SqlCommand cmd=conn.CreateCommand()) 27 { 28 cmd.CommandText = sql; 29 return cmd.ExecuteNonQuery(); 30 31 } 32 } 33 } 34 /// <summary> 35 /// 返回一个数据集 36 /// </summary> 37 /// <param name="sql"></param> 38 /// <returns></returns> 39 public static DataSet ExecuteDataSet(string sql) 40 { 41 using (SqlConnection xonn=new SqlConnection(connStr)) 42 { 43 xonn.Open(); 44 using (SqlCommand cmd = xonn.CreateCommand()) 45 { 46 cmd.CommandText = sql; 47 SqlDataAdapter adapter = new SqlDataAdapter(cmd); 48 DataSet dataset = new DataSet(); 49 adapter.Fill(dataset); 50 return dataset; 51 } 52 } 53 } 54 public static object ExecuteScalar(string sql) 55 { 56 using (SqlConnection conn=new SqlConnection(connStr)) 57 { 58 conn.Open(); 59 using (SqlCommand cmd=conn.CreateCommand()) 60 { 61 cmd.CommandText = sql; 62 return cmd.ExecuteScalar(); 63 } 64 } 65 } 66 /// <summary> 67 /// md5加密 68 /// </summary> 69 /// <param name="strPwd"></param> 70 /// <returns></returns> 71 public static string GetMD5(string strPwd) 72 { 73 string pwd = ""; 74 //实例化一个md5对象 75 MD5 md5 = MD5.Create(); 76 // 加密后是一个字节类型的数组 77 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(strPwd)); 78 //翻转生成的MD5码 79 s.Reverse(); 80 //通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得 81 //只取MD5码的一部分,这样恶意访问者无法知道取的是哪几位 82 for (int i = 3; i < s.Length - 1; i++) 83 { 84 //将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 85 //进一步对生成的MD5码做一些改造 86 pwd = pwd + (s[i] < 198 ? s[i] + 28 : s[i]).ToString("X"); 87 } 88 return pwd; 89 } 90 91 92 } 93 }View Code
界面预览:
功能应用有:身份验证,退出。公共变量传值。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace sql_homework_end 12 { 13 public partial class login : Form 14 { 15 public login() 16 { 17 InitializeComponent(); 18 } 19 20 //界面传值 21 public static string GlobelValue; // 注意,必须申明为static变量 22 //登录按钮事件 23 private void btn_login_Click(object sender, EventArgs e) 24 { 25 if (cmb_identity.Text.ToString()=="管理员") 26 { 27 verify_identidy("tb_admin","admin_name"); 28 main a = new main(); 29 a.Show(); 30 } 31 else if (cmb_identity.Text.ToString()=="学生") 32 { 33 verify_identidy("tb_student","student_name"); 34 studnet s = new studnet(); 35 s.Show(); 36 } 37 38 } 39 //身份验证的函数 40 private void verify_identidy(string table,string name) 41 { 42 DataSet ds = new DataSet(); 43 ds = sqlhelper.ExecuteDataSet("select * from "+table+" where "+name+" = ‘" + tbx_loginname.Text + "‘"); 44 DataTable dt = new DataTable(); 45 dt = ds.Tables[0]; 46 if (dt.Rows.Count != 0) 47 { 48 GlobelValue = tbx_loginname.Text; 49 this.Visible = false; //隐藏当前窗体 50 } 51 else 52 { 53 MessageBox.Show("用户名不存在,请重新输入"); 54 } 55 } 56 //退出按钮 57 private void btn_close_Click(object sender, EventArgs e) 58 { 59 this.Close(); 60 } 61 62 63 64 } 65 }View Code
预览:
管理员信息:
学生信息:
主要使用控件:datagridview
相关代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace sql_homework_end 12 { 13 public partial class main : Form 14 { 15 public main() 16 { 17 InitializeComponent(); 18 19 } 20 //绑定并显示相关信息 21 DataSet ds = new DataSet(); 22 DataTable dt = new DataTable(); 23 private void 学生信息ToolStripMenuItem_Click(object sender, EventArgs e) 24 { 25 ds = sqlhelper.ExecuteDataSet("select * from tb_student"); 26 dt = ds.Tables[0]; 27 dataGridView1.DataSource = dt; 28 } 29 30 private void 课程信息ToolStripMenuItem_Click(object sender, EventArgs e) 31 { 32 ds = sqlhelper.ExecuteDataSet("select * from tb_course"); 33 dt = ds.Tables[0]; 34 dataGridView1.DataSource = dt; 35 } 36 37 private void 选课信息ToolStripMenuItem_Click(object sender, EventArgs e) 38 { 39 ds = sqlhelper.ExecuteDataSet("select * from tb_student_course"); 40 dt = ds.Tables[0]; 41 dataGridView1.DataSource = dt; 42 dataGridView1.Columns["sc_id"].DisplayIndex = 0; 43 } 44 private void 管理员信息ToolStripMenuItem_Click(object sender, EventArgs e) 45 { 46 ds = sqlhelper.ExecuteDataSet("select * from tb_admin"); 47 dt = ds.Tables[0]; 48 dataGridView1.DataSource = dt; 49 } 50 //添加按钮事件 51 private void btn_insert_Click(object sender, EventArgs e) 52 { 53 if (dataGridView1.Columns[0].HeaderText=="student_num") 54 { 55 string sql = @"insert tb_student (student_num,student_name,student_password,student_sex 56 ,student_age,student_dept)values(‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value + "‘,‘" + 57 dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[1].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[2].Value 58 + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[3].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[4].Value 59 + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[5].Value + "‘)"; 60 sqlhelper.ExecuteNoQuery(sql); 61 62 } 63 else if (dataGridView1.Columns[0].HeaderText=="course_num") 64 { 65 string sql = @"insert tb_course (course_num,course_name,course_credit,course_semester)values(‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[1].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[2].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[3].Value + "‘)"; 66 sqlhelper.ExecuteNoQuery(sql); 67 } 68 else if (dataGridView1.Columns[0].HeaderText=="sc_id") 69 { 70 try 71 { 72 string sql = @"insert tb_student_course (sc_id,student_num,course_num,grade)values(‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[1].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[2].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[3].Value + "‘)"; 73 sqlhelper.ExecuteNoQuery(sql); 74 } 75 catch (Exception) 76 { 77 MessageBox.Show("学号或姓名不存在,请重新添加。"); 78 } 79 80 } 81 else if (dataGridView1.Columns[0].HeaderText == "admin_id") 82 { 83 string sql = @"insert tb_admin (admin_id,admin_name,admin_password,remark)values(‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[1].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[2].Value + "‘,‘" + dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[3].Value + "‘)"; 84 sqlhelper.ExecuteNoQuery(sql); 85 } 86 MessageBox.Show("添加成功"); 87 } 88 //更新按钮事件 89 private void btn_update_Click(object sender, EventArgs e) 90 { 91 if (dataGridView1.Columns[0].HeaderText == "student_num") 92 { 93 update("tb_student","student_num"); 94 } 95 else if (dataGridView1.Columns[0].HeaderText == "course_num") 96 { 97 update("tb_course","course_num"); 98 } 99 else if (dataGridView1.Columns[0].HeaderText == "admin_id") 100 { 101 update("tb_admin","admin_id"); 102 } 103 else if (dataGridView1.Columns[0].HeaderText == "sc_id") 104 { 105 try 106 { 107 update("tb_studnet_course","sc_id"); 108 } 109 catch (Exception) 110 { 111 MessageBox.Show("学号或课程号不存在,请重新输入"); 112 throw; 113 } 114 } 115 } 116 //更新方法 117 private void update(string table,string head_id) 118 { 119 for (int i = 0; i < dataGridView1.RowCount; i++) 120 { 121 int id = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value); 122 for (int j = 1; j < dataGridView1.ColumnCount; j++) 123 { 124 if (dataGridView1.Columns[j].Visible == true) 125 { 126 string columnName = dataGridView1.Columns[j].Name.ToString(); 127 string sql = "update " + table + " set " + columnName + "=‘" + dataGridView1.Rows[i].Cells[j].Value + "‘ where " + head_id + "=‘" + id + "‘"; 128 sqlhelper.ExecuteNoQuery(sql); 129 } 130 } 131 } 132 MessageBox.Show("更新成功"); 133 } 134 135 //删除事件 136 private void btn_delete_Click(object sender, EventArgs e) 137 { 138 if (dataGridView1.Columns[0].HeaderText == "student_num") 139 { 140 string sql = "delete from tb_student where student_num=‘" + dataGridView1.SelectedCells[0].Value + "‘"; 141 sqlhelper.ExecuteNoQuery(sql); 142 } 143 else if (dataGridView1.Columns[0].HeaderText == "course_num") 144 { 145 string sql = "delete from tb_course where course_num=‘" + dataGridView1.SelectedCells[0].Value + "‘"; 146 sqlhelper.ExecuteNoQuery(sql); 147 } 148 else if (dataGridView1.Columns[0].HeaderText == "sc_id") 149 { 150 string sql = "delete from tb_student_course where sc_id=‘" + dataGridView1.SelectedCells[0].Value + "‘"; 151 sqlhelper.ExecuteNoQuery(sql); 152 } 153 else if (dataGridView1.Columns[0].HeaderText == "admin_id") 154 { 155 string sql = "delete from tb_admin where admin_id=‘" + dataGridView1.SelectedCells[0].Value + "‘"; 156 sqlhelper.ExecuteNoQuery(sql); 157 } 158 MessageBox.Show("删除成功"); 159 } 160 161 //界面载入显示身份和登录时间 162 private void main_Load(object sender, EventArgs e) 163 { 164 lbl_username.Text = "Welcome," + login.GlobelValue + ""; 165 lbl_logintime.Text ="登录时间:" +DateTime.Now.ToString()+""; 166 } 167 } 168 }View Code
界面预览:
个人信息显示:在这个界面,学生能看到自己的信息,并进行修改操作。
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace sql_homework_end 12 { 13 public partial class studnet : Form 14 { 15 public studnet() 16 { 17 InitializeComponent(); 18 } 19 20 DataSet ds = new DataSet(); 21 DataTable dt = new DataTable(); 22 private void studnet_Load(object sender, EventArgs e) 23 { 24 lbl_username.Text = "Welcome," + login.GlobelValue + ""; 25 lbl_logintime.Text = "登录时间:" + DateTime.Now.ToString() + ""; 26 27 28 } 29 //相关信息显示 30 private void 个人信息ToolStripMenuItem_Click(object sender, EventArgs e) 31 { 32 ds = sqlhelper.ExecuteDataSet("select * from tb_student where student_name=‘" + login.GlobelValue + "‘"); 33 dt = ds.Tables[0]; 34 dataGridView1.DataSource = dt; 35 btn_update.Visible = true; 36 btn_insert.Visible = false; 37 btn_delete.Visible = false; 38 } 39 40 private void 课程信息ToolStripMenuItem_Click(object<热心网友 时间:2022-04-09 15:11
挺好的一个题目啊
但我可不愿意帮人写作业
网上很多类似的,你可以参考下:
#include <stdio.h>
#include <string.h> struct student {
char name[30];
float math;
float chinese;
float english;
float average;
};
int MenuChoice(void);
int EnterAccount(void);
void Enter(struct student *fst);
void Amend(struct student *src);
void Delete(struct student *next);
void Print(struct student *out);
main()
{
struct student one[100] = {0}, *look = one;
int menu, t;
while ((menu = MenuChoice()) != 0)
if (menu >= 1 && menu <= 4 && (t = EnterAccount())) {
look = one + t;
switch (menu) {
case 1:
Enter(look);
break;
case 2:
if (strcmp(look->name, ""))
printf("学号:%d\n姓名: %s\n语文成绩: %.1f\n数学成绩:%.1f\n英语成绩: %f\n平均"成绩:%.1f\n",
t, look->name, look->chinese, look->math, look->english, look->average);
else
printf("对不起,该学号不存在!\n");
break;
case 3:
Amend(look);
break;
case 4:
Delete(look);
break;
}
}
else if (menu == 5)
Print(one);
else
printf("错误的请求!\n");
return 0;
}
int MenuChoice(void)
{
int choice;
printf("请选择:\n"
"1 - 新建学生信息\n"
"2 - 查找学生信息\n"
"3 - 修改学生信息\n"
"4 - 删除学生信息\n"
"5 - 打印学生信息\n"
"0 - 退出\n");
scanf("%d", &choice);
return choice;
}
int EnterAccount(void)
{
int ret;
printf("请输入学号:\n");
scanf("%d", &ret);
if (ret >= 1 && ret <= 100)
return ret -1;
return 0;
}
void Enter(struct student *fst)
{
printf("请输入学生姓名、语文成绩、数学成绩、英语成绩和平均成绩\n");
scanf("%s%f%f%f%f", fst->name, &fst->chinese, &fst->math, &fst->english, &fst->average);
}
void Amend(struct student *src)
{
int t;
printf("请选择:\n"
"1 - 修改学生姓名\n"
"2 - 修改学生语文成绩\n"
"3 - 修改学生数学成绩\n"
"4 - 修改学生英语成绩\n"
"5 - 修改学生平均成绩\n");
scanf("%d", &t);
switch (t) {
case 1:
printf("请输入学生的新名字\n");
scanf("%s", src->name);
break;
case 2:
printf("请输入学生新的语文成绩\n");
scanf("%f", &src->chinese);
break;
case 3:
printf("请输入学生新的数学成绩\n");
scanf("%f", &src->math);
break;
case 4:
printf("请输入学生新的英语成绩\n");
scanf("%f", &src->english);
break;
case 5:
printf("请输入学生新的平均成绩\n");
scanf("%f", &src->average);
break;
}
}
void Delete(struct student *next)
{
strcpy(next->name, "");
next->math = next->english = next->chinese = next->average = 0.0;
}
void Print(struct student *out)
{
struct student *up;
for (up = out; out < &up[100]; out++)
if (strcmp(out->name, ""))
printf("学号:%d\n姓名: %s\n语文成绩: %.1f\n数学成绩:%.1f\n英语成绩: %.1f\n平均成绩:%.1f\n",
out-up + 1, out->name, out->chinese, out->math, out->english, out->average);
}