學生登記系統的要求:
1)能夠錄入如下信息:學號,姓名,性別,手機號,家庭地址,報考系別,領取獎品,高考分數
2)能夠根據姓名或者手機查詢學生信息
3)能夠查詢獎品發放情況,如T恤發放多少件,U盤發放多少個,VIP充值卡發放多少張
4)能夠更改學生信息(這在學生信息錄入錯誤的情況下是有用的)
5)能夠選擇的就不要輸入
利用winform和數據庫編寫程序
第一步:數據庫以及表格的創建
create table information
(
name varchar(32),
Id varchar(32) primary key,
sex varchar(32),
Mobile varchar(32),
fasten varchar(32),
award varchar(32),
scord varchar(32),
address varchar(32)
)
第二步: windows窗體中進行拖拽控件,進行編寫碼
1)建立三個窗體:第一個是:主窗體(父窗體) 第二個是:學生信息錄入窗體(子窗體) 第三個是:學生信息查詢、更改窗體(子窗體)
2)主窗體編寫程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace xiangmu
{
public partial class 學生登錄系統 : Form
{
public 學生登錄系統()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void 信息錄入ToolStripMenuItem_Click(object sender, EventArgs e)
{
信息錄入 xinxi = new 信息錄入();
xinxi.Show();
xinxi.MdiParent = this;
}
private void xiToolStripMenuItem_Click(object sender, EventArgs e)
{
信息查詢 chaxun = new 信息查詢();
chaxun.Show();
chaxun.MdiParent = this;
}
}
}
3)學生信息錄入窗體編寫程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace xiangmu
{
public partial class 信息錄入 : Form
{
public 信息錄入()
{
InitializeComponent();
}
private void 信息錄入_Load(object sender, EventArgs e)
{
this.comboBox4.SelectedIndex = 0;
this.comboBox5.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "insert into information( name,Id, sex, Mobile, fasten, award, scord,address) values(@name,@Id, @sex,@Mobile,@fasten,@award,@scord,@address)";
comm.Parameters.AddWithValue("@name",textBox1.Text.ToString());
comm.Parameters.AddWithValue("@Id",textBox2.Text.ToString());
comm.Parameters.AddWithValue("@sex",comboBox6.Text.ToString());
comm.Parameters.AddWithValue("@Mobile",textBox3.Text.ToString());
comm.Parameters.AddWithValue("@fasten", comboBox4.Text.ToString());
comm.Parameters.AddWithValue("@award",comboBox5.Text.ToString());
comm.Parameters.AddWithValue("@scord",textBox4.Text.ToString());
comm.Parameters.AddWithValue("@address", textBox5.Text.ToString());
comm.ExecuteNonQuery();
MessageBox.Show("錄入成功");
conn.Close();
conn.Dispose();
comm.Dispose();
Console.Read();
}
}
}
4)學生信息查詢以及更改以及獎品的查詢編寫程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace xiangmu
{
public partial class 信息查詢 : Form
{
public 信息查詢()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void 信息查詢_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedIndex = 0;
this.comboBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
string id = textBox1.Text;
comm.CommandText = "select name, sex ,Mobile, fasten ,award ,scord ,address from information where
Id=@Id";
comm.Parameters.AddWithValue("@Id", id);
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataTable data = new DataTable();
adapter.Fill(data);
for (int i = 0; i < data.Rows.Count; i++)
{
textBox2.Text = data.Rows[i][0].ToString();
textBox6.Text = data.Rows[i][1].ToString();
textBox3.Text = data.Rows[i][2].ToString();
comboBox1.Text = data.Rows[i][3].ToString();
comboBox2.Text = data.Rows[i][4].ToString();
textBox4.Text = data.Rows[i][5].ToString();
textBox5.Text = data.Rows[i][6].ToString();
}
conn.Close();
conn.Dispose();
comm.Dispose();
MessageBox.Show("查詢成功");
}
private void button2_Click(object sender, EventArgs e)
{
string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select *from information";
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataTable data = new DataTable();
adapter.Fill(data);
this.dataGridView1.DataSource = data;
conn.Close();
conn.Dispose();
comm.Dispose();
}
private void button3_Click(object sender, EventArgs e)
{
string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
string id = textBox1.Text;
comm.CommandText = "update information set
name=@name, sex=@sex ,Mobile=@Mobile,
fasten=@fasten ,award=@award ,scord=@scord ,address=@address where Id=id";
comm.Parameters.AddWithValue("@name", textBox2.Text.ToString());
comm.Parameters.AddWithValue("@sex", textBox6.Text.ToString());
comm.Parameters.AddWithValue("@Mobile", textBox3.Text.ToString());
comm.Parameters.AddWithValue("@fasten", comboBox1.Text.ToString());
comm.Parameters.AddWithValue("@award", comboBox2.Text.ToString());
comm.Parameters.AddWithValue("@scord ", textBox4.Text.ToString());
comm.Parameters.AddWithValue("@address", textBox5.Text.ToString());
comm.ExecuteNonQuery();
conn.Close();
comm.Dispose();
conn.Dispose();
MessageBox.Show("更改成功");
}
private void button4_Click(object sender, EventArgs e)
{
string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select COUNT(Id) U盤 from information where
award=@award ";
comm.Parameters.AddWithValue("@award", "U盤");
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataTable data = new DataTable();
adapter.Fill(data);
this.dataGridView1.DataSource = data;
conn.Close();
comm.Dispose();
conn.Dispose();
}
private void button5_Click(object sender, EventArgs e)
{
string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select COUNT(Id) T恤 from information where
award=@award1 ";
comm.Parameters.AddWithValue("@award1", "T恤");
SqlDataAdapter adapter1 = new SqlDataAdapter(comm);
DataTable data1 = new DataTable();
adapter1.Fill(data1);
this.dataGridView1.DataSource = data1;
conn.Close();
comm.Dispose();
conn.Dispose();
}
private void button6_Click(object sender, EventArgs e)
{
string sql = "Data Source=(local);Initial Catalog=denglu;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
conn.Open();
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select COUNT(Id) CSDNVIP充值卡 from information where
award=@award2";
comm.Parameters.AddWithValue("@award2","CSDNVIP充值卡");
SqlDataAdapter adapter2 = new SqlDataAdapter(comm);
DataTable data2 = new DataTable();
adapter2.Fill(data2);
this.dataGridView1.DataSource = data2;
conn.Close();
comm.Dispose();
conn.Dispose();
}
}
}