目录
“用户登录”为“groupBox控件”
双击“确定”按钮:
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "sa")
{
if (textBox2.Text == "123") Program.show();
else MessageBox.Show("密码错误!");
}
else MessageBox.Show("用户名错误!");
}
学生数据编辑界面:
加载窗体以及初始化时的代码:
public Form1()
{
InitializeComponent();
}
public static string mystr = "server=DESKTOP-NA2OPPJ\\SQLEXPRESS;database=学生选课4;uid=sa;pwd=123";
public static string sqlstr = "select*from student";
static SqlConnection myconn = new SqlConnection(mystr);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, myconn);
DataSet myds = new DataSet();
DataView mydv = new DataView();
int x;
int y;
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Visible = false;
groupBox1.Visible = false;
groupBox2.Visible = false;
label6.Visible = true;
button4.Visible = false;
}
Programs.cs:
using System;
using System.Windows.Forms;
namespace XueShengXuanKeXiTong
{
static class Program
{
static Form1 form1;
static Form2 form2;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
form2 = new Form2();
form2.ShowDialog();
}
public static void show()
{
form2.Dispose();
form1 = new Form1();
form1.ShowDialog();
}
public static void close()
{
form1.Dispose();
form2 = new Form2();
form2.ShowDialog();
}
}
}
双击“新增”按钮:
private void XZbutton_Click(object sender, EventArgs e)
{
myconn.Open();
string insertCmd;
try
{
switch (y)
{
case 0:
{
insertCmd = "Insert Into student(学号,姓名,性别,年龄,班级) Values(@s1,@s2,@s3,@s4,@s5)";
SqlCommand cmd;
cmd = new SqlCommand(insertCmd, myconn);
cmd.Parameters.Add(new SqlParameter("@s1", SqlDbType.Char));
cmd.Parameters.Add(new SqlParameter("@s2", SqlDbType.Char));
cmd.Parameters.Add(new SqlParameter("@s3", SqlDbType.Char));
cmd.Parameters.Add(new SqlParameter("@s4", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("@s5", SqlDbType.Char));
cmd.Parameters["@s1"].Value = textBox1.Text;
cmd.Parameters["@s2"].Value = textBox2.Text;
cmd.Parameters["@s3"].Value = textBox4.Text;
cmd.Parameters["@s4"].Value = int.Parse(textBox3.Text);
cmd.Parameters["@s5"].Value = textBox5.Text;
cmd.ExecuteNonQuery();
myds.Clear();
sqlstr = "select*from student";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, myconn);
myda.Fill(myds, "student");
mydv = myds.Tables["student"].DefaultView;
dataGridView1.DataSource = mydv;
}
break;
}
}
catch
{
MessageBox.Show("请输入正确信息!", "错误");
}
myconn.Close();
}
双击“删除”按钮(注意:这是根据学号删除,即只需输入学号即可删除记录):
private void SCbutton_Click(object sender, EventArgs e)
{
myconn.Open();
string delCmd;
try
{
switch (y)
{
case 0:
{
delCmd = "Delete From student Where 学号=@s1";
SqlCommand cmd;
cmd = new SqlCommand(delCmd, myconn);
cmd.Parameters.Add(new SqlParameter("s1", SqlDbType.Char));
cmd.Parameters["s1"].Value = textBox1.Text;
cmd.ExecuteNonQuery();
myds.Clear();
sqlstr = "select*from student";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, myconn);
myda.Fill(myds, "student");
mydv = myds.Tables["student"].DefaultView;
dataGridView1.DataSource = mydv;
myconn.Close();
}
break;
}
}
catch
{
MessageBox.Show("请输入正确信息!", "错误");
}
}
双击“排序”下的“确定”按钮:
private void button3_Click(object sender, EventArgs e)
{
string order = "";
if (comboBox3.Text != "")
{
if (radioButton1.Checked) order = comboBox3.Text + " ASC";
else order = comboBox3.Text + " DESC";
}
mydv.Sort = order;
}
“更新”按钮:
private void button4_Click(object sender, EventArgs e)
{
SqlCommandBuilder mycom = new SqlCommandBuilder(myda);
switch (x)
{
case 0: myda.Update(myds, "student"); break;
}
}
学生数据查询界面:
查询时的“确定”按钮:
private void button1_Click(object sender, EventArgs e)
{
string condstr = "";
switch (x)
{
case 0:
{
if (textBox1.Text != "") condstr = "学号 = " + textBox1.Text + "";
if (textBox2.Text != "")
{
if (condstr != "") condstr = condstr + "and 姓名 like'" + textBox2.Text + "%'";
else condstr = "姓名 like'" + textBox2.Text + "%'";
}
if (comboBox1.Text != "")
{
if (condstr != "") condstr = condstr + "and 性别 like'" + comboBox1.Text + "'";
else condstr = "性别 like'" + comboBox1.Text + "'";
}
if (textBox3.Text != "")
{
if (condstr != "") condstr = condstr + "and 年龄 " + textBox3.Text + "";
else condstr = "年龄 =" + textBox3.Text + "";
}
if (comboBox2.Text != "")
{
if (condstr != "") condstr =condstr+ "and 班级 like '" + comboBox2.Text + "'";
else condstr = "班级 like'" + comboBox2.Text + "'";
}
}
break;
}
mydv.RowFilter = condstr;
}
查询时的“重置”按钮:
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
comboBox4.Text = "";
}
运行结果:
项目文档
链接:https://pan.baidu.com/s/1fwgEVsDBer9c1SQUCO2AeQ
提取码:prdj
项目源码
项目源码下载地址:https://download.csdn.net/download/qq_40323256/10777385
(这个下载积分是系统随着资源下载量的变化而变化的,并不是我自己设置的。下载积分有一点点高,建议在淘宝上面买(不是打广告哈),大概0.5元左右就可以下载任意积分的资源)
附截图: