《c#用户登录界面程序》由会员分享,可在线阅读,更多相关《c#用户登录界面程序(8页珍藏版)》请在金锄头文库上搜索。
1、一、数据库连接方式:由于使用Acess2000 连接数据库,所以要加上 using System.Data.OleDb;空间,可以使用如下方式连接到数据库 string str = string str = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xsxx.mdb;程序总体如下:OleDbDataAdapter adapter;DataTable table = new DataTable();string str = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=liqi.mdb;OleDbConn
2、ection con = new OleDbConnection();二、界面设计:1、登入界面界面框图如下:图 3-1 登入界面2、注册界面框图如下:图 3-2 注册界面3、学生信息管理系统界面框图如下:图 3-3 学生信息管理系统界面三、主要功能代码1 登入界面代码:public partial class Form1 : FormOleDbDataAdapter adapter; /数据库连接DataTable table = new DataTable();string str = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=liqi.md
3、b;OleDbConnection con = new OleDbConnection();public Form1()InitializeComponent();private void button1_Click(object sender, EventArgs e)if (textBox1.Text != & textBox2.Text != )string sql = select * from 信息 where 学号= + textBox1.Text + and 密码= + textBox2.Text + ;adapter = new OleDbDataAdapter(sql, st
4、r);OleDbCommandBuilder buider = new OleDbCommandBuilder(adapter);adapter.InsertCommand = buider.GetInsertCommand();table.Clear();adapter.Fill(table);if (table.Rows.Count 0)Form f3 = new Form3();f3.Show();this.Hide();elseMessageBox.Show(用户名或密码不能空); /登入private void button3_Click(object sender, EventAr
5、gs e)Form f2 = new Form2();/打开注册界面f2.Show();this.Hide();private void button2_Click(object sender, EventArgs e)Application.Exit();功能分析:当在登入界面时,可以使用数据库中有的数据登入学生信息管理系统,也可以进入注册界面成为新的用户。2、注册界面代码:public partial class Form2 : FormOleDbDataAdapter adapter; /数据库连接DataTable table = new DataTable();string str
6、= Provider=Microsoft.Jet.OLEDB.4.0;Data Source=liqi.mdb;OleDbConnection con = new OleDbConnection();public Form2()InitializeComponent(); con.ConnectionString = str;private void button2_Click(object sender, EventArgs e)textBox1.Text = ;textBox2.Text = ;textBox4.Text = ;textBox5.Text = ;textBox3.Text
7、= ; /取消注册private void button1_Click(object sender, EventArgs e)if (textBox2.Text = & textBox3.Text = )label4.Text = 请输入密码;else if (textBox2.Text = textBox3.Text)string b = textBox2.Text;string c = textBox1.Text;string a = textBox4.Text;string d = textBox5.Text;OleDbCommand cmd = new OleDbCommand(ins
8、ert into 信息(学号,姓名,专业,密码) values( + c + , + a + , + d + , + b + ), con);con.Open();cmd.Connection = con;cmd.ExecuteNonQuery();con.Close();Form1 f3 = new Form1();f3.Show();this.Hide(); /注册新用户功能分析:当不能登入学生管理系统时,可以使用注册界面注册成为用户,而注册的信息将会进入数据库中。注册成功后将会回到登入界面,可以使用新用户登入。3、学生管理系统界面代码:public partial class Form3
9、 : FormOleDbDataAdapter adapter; /数据库连接DataTable table = new DataTable();string str = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=liqi.mdb; OleDbConnection con = new OleDbConnection();public Form3()InitializeComponent();private void Form3_Load(object sender, EventArgs e)string sql = select * from 信
10、息;adapter = new OleDbDataAdapter(sql, str);private void 浏览ToolStripMenuItem_Click(object sender, EventArgs e)con.ConnectionString = str;string sql = select * from 信息;adapter = new OleDbDataAdapter(sql, str);OleDbCommandBuilder buider = new OleDbCommandBuilder(adapter);table.Clear();adapter.Fill(tabl
11、e);dataGridView1.DataSource = table; /浏览功能,可以看到数据信息private void 精确查询ToolStripMenuItem_Click(object sender, EventArgs e)adapter.SelectCommand.CommandText = Select * from 信息 where 学号 = + textBox1.Text + ;table.Clear();adapter.Fill(table);dataGridView1.DataSource = table;/精确查询功能,可输入完整学号查询信息private void
12、 模糊查询ToolStripMenuItem_Click(object sender, EventArgs e)adapter.SelectCommand.CommandText = Select * from 信息 where 学号 like + textBox1.Text + %;table.Clear(); adapter.Fill(table);dataGridView1.DataSource = table; /模糊查询功能,输入学号前几个数字就可查询private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)O
13、leDbConnection con = new OleDbConnection();con.ConnectionString = str;string sex;if (radioButton1.Checked = true) sex = 男;elsesex = 女;OleDbCommand cmd = new OleDbCommand(insert into 信息(学号,姓名,性别,生日,籍贯,专业,密码) values( + textBox1.Text + , + textBox2.Text + , + sex + , + textBox3.Text + , + textBox4.Text
14、 + , + textBox5.Text + , + textBox6.Text + ), con);con.Open();cmd.Connection = con;cmd.ExecuteNonQuery();con.Close();adapter.SelectCommand.CommandText = select * from 信息 ;table.Clear();adapter.Fill(table);adapter.Update(table);/可添加数据信息private void 修改ToolStripMenuItem_Click(object sender, EventArgs e
15、)OleDbConnection con = new OleDbConnection();con.ConnectionString = str;string sex;if (radioButton1.Checked = true) sex = 男;elsesex = 女;OleDbCommand cmd = new OleDbCommand(update 信息 set 姓名= + textBox2.Text + ,性别= + sex + ,生日= + textBox3.Text + ,籍贯= + textBox4.Text + ,专业= + textBox5.Text + ,密码= + textBox6.Text + where 学号= + textBox1.Text + , con); con.Open();cmd.Connection = con;cmd.ExecuteNonQuery();con.Close();adapter.SelectCommand.CommandText = select * from 信息 ;table.Cle