基於sqlserver資料庫的學生成績管理系統
阿新 • • 發佈:2018-11-13
目錄
完整專案原始碼:https://download.csdn.net/download/qq_40323256/10777385
文件介紹:https://wenku.baidu.com/view/96a3284c53d380eb6294dd88d0d233d4b04e3f76
“使用者登入”為“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 學號[email protected]";
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://download.csdn.net/download/qq_40323256/10777385
文件介紹:https://wenku.baidu.com/view/96a3284c53d380eb6294dd88d0d233d4b04e3f76
附截圖: