📄 studentinformation.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _7计应的班级管理系统
{
public partial class studentinformation : Form{
DataSet dbrs = new DataSet();
private BindingManagerBase bmgr;
private static string nowpower;
public string Nowpower
{
get
{
return nowpower;
}
set
{
nowpower = value;
}
}
public studentinformation()
{
InitializeComponent();
}
private void studentinformation_Load(object sender, EventArgs e)
{
refresh();
SqlConnection conn = new SqlConnection("user id=sa;data source=localhost;integrated security=false;initial catalog=学生数据库1");
SqlDataAdapter da = new SqlDataAdapter("select * from student_information", conn);
da.Fill(dbrs, "student_information");
textBoxname.DataBindings.Add("text", dbrs, "student_information.姓名");
textBoxsex.DataBindings.Add("text", dbrs, "student_information.姓别");
textBoxclass.DataBindings.Add("text", dbrs, "student_information.班级");
textBoxnumber.DataBindings.Add("text", dbrs, "student_information.学号");
textBoxbirthday.DataBindings.Add("text", dbrs, "student_information.出生年月");
textBoxzzmm.DataBindings.Add("text", dbrs, "student_information.政治面貌");
textBoxsslh.DataBindings.Add("text", dbrs, "student_information.宿舍楼号");
// textboxssh.DataBindings.Add("text", dbrs, "student_information.宿舍号");
}
void refresh()
{
listViewdisplay.Clear();
SqlConnection conn = new SqlConnection("user id=sa;data source=localhost;integrated security=false;initial catalog=学生数据库1");
SqlCommand cmd = new SqlCommand();
cmd = conn.CreateCommand();
cmd.CommandText = "select * from student_information";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
//if(dr==null)
//{
for (int i = 0; i < dr.FieldCount; i++)
{
ColumnHeader ch = new ColumnHeader();
ch.Text = dr.GetName(i);
listViewdisplay.Columns.Add(ch);
}
while (dr.Read())
{
ListViewItem item = new ListViewItem();
item.Text = dr.GetValue(0).ToString();
for (int i = 1; i < dr.FieldCount; i++)
{
item.SubItems.Add(dr.GetValue(i).ToString());
}
listViewdisplay.Items.Add(item);
}
//}
//else
// MessageBox.Show("没有这样的记录!");
dr.Close();
conn.Close();
}
void bind()
{
SqlConnection conn = new SqlConnection("user id=sa;data source=localhost;integrated security=false;initial catalog=学生数据库1");
SqlDataAdapter da = new SqlDataAdapter("select * from student_information", conn);
dbrs = new DataSet();
textBoxname.DataBindings.Clear();
textBoxsex.DataBindings.Clear();
textBoxclass.DataBindings.Clear();
textBoxnumber.DataBindings.Clear();
textBoxbirthday.DataBindings.Clear();
textBoxzzmm.DataBindings.Clear();
textBoxsslh.DataBindings.Clear();
//textboxssh.DataBindings.Clear();
da.Fill(dbrs, "student_information");
textBoxname.DataBindings.Add("text", dbrs, "student_information.姓名");
textBoxsex.DataBindings.Add("text", dbrs, "student_information.姓别");
textBoxclass.DataBindings.Add("text", dbrs, "student_information.班级");
textBoxnumber.DataBindings.Add("text", dbrs, "student_information.学号");
textBoxbirthday.DataBindings.Add("text", dbrs, "student_information.出生年月");
textBoxzzmm.DataBindings.Add("text", dbrs, "student_information.政治面貌");
textBoxsslh.DataBindings.Add("text", dbrs, "student_information.宿舍楼号");
// textboxssh.DataBindings.Add("text", dbrs, "student_information.宿舍号");
}
private void buttonsave_Click(object sender, EventArgs e)
{
studentinformation load = new studentinformation();
if (load.Nowpower != "学生")
{
string strconn = "user id=sa;data source=localhost;integrated security=false;initial catalog=学生数据库1";
SqlConnection conn = new SqlConnection(strconn);
SqlCommand cmd = conn.CreateCommand();
conn.Open();
string name = textBoxname.Text;
string sex = textBoxsex.Text;
string tbclass = textBoxclass.Text;
string number = textBoxnumber.Text;
string birthday = textBoxbirthday.Text;
string zzmm = textBoxzzmm.Text;
string sslh = textBoxsslh.Text;
// string ssh = textboxssh.Text;
try
{
if (radioButtonadd.Checked)
{
cmd.CommandText = "insert into student_information values('" + name + "','" + sex + "','" + tbclass + "','" + number + "','" + birthday + "','" + zzmm + "','" + sslh + "')";
}
else if (radioButtonchange.Checked)
{
//使用这条语句没行通-_-b
cmd.CommandText = "update student_information set 姓名='" + name + "',姓别='" + sex + "',班级='" + tbclass + "',出生年月='" + birthday + "',政治面貌='" + zzmm + "',宿舍楼号='" + sslh + "', where 学号='" + number + "'";
//cmd.CommandText="delete from student_information where 学号='"+number+"'";
//cmd.ExecuteNonQuery();
//cmd.CommandText="insert into student_information values('"+name+"','"+sex+"','"+tbclass+"','"+number+"','"+birthday+"','"+zzmm+"','"+sslh+"','"+ssh+"')";
}
else if (radioButtondelete.Checked)
{
cmd.CommandText = "delete from student_information where 学号='" + number + "'";
}
else
MessageBox.Show("请选择一个功能!");
SqlDataReader dr = cmd.ExecuteReader();
for (int i = 0; i < dr.FieldCount; i++)
{
ColumnHeader ch = new ColumnHeader();
ch.Text = dr.GetName(i);
listViewdisplay.Columns.Add(ch);
}
while (dr.Read())
{
ListViewItem item = new ListViewItem();
item.Text = dr.GetValue(0).ToString();
for (int i = 1; i < dr.FieldCount; i++)
{
item.SubItems.Add(dr.GetValue(i).ToString());
}
listViewdisplay.Items.Add(item);
}
dr.Close();
refresh();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
finally
{
conn.Close();
}
}
else
MessageBox.Show("只有管理员有此权限!");
}
private void buttonclear_Click(object sender, EventArgs e)
{
textBoxname.Clear();
textBoxsex.Clear();
textBoxclass.Clear();
textBoxnumber.Clear();
textBoxbirthday.Clear();
textBoxzzmm.Clear();
textBoxsslh.Clear();
// textboxssh.Clear();
listViewdisplay.Clear();
}
private void buttonfresh_Click(object sender, EventArgs e)
{
refresh();
bind();
}
private void buttonleft_Click(object sender, EventArgs e)
{
bmgr = this.BindingContext[dbrs, "student_information"];
bmgr.Position -= 1;
}
private void buttonright_Click(object sender, EventArgs e)
{
bmgr = this.BindingContext[dbrs, "student_information"];
bmgr.Position += 1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -