学生用户管理系统(一)
这个东西搞了我两天呀,强大的度娘下加上qq群上大神的指点,终于弄懂了一些皮毛的东西,感觉关于数据库的连接有很多的知识点,等以后学到.net再归纳。
先上代码。
form1:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApp2 { public partial class Form1 : Form { int count; int time = 150; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //string sqlstring = "server=ZJX-PC;database=zjx;Trusted_Connection=SSPI"; try { string sqlstring = "server=ZJX-PC;uid=sa;pwd=zjx28963436;database=zjx"; SqlConnection conn = new SqlConnection(sqlstring); // SqlConnection comm = new SqlConnection(sqlstring); string sql = "Select * from info where id='" + this.textBox1.Text.Trim() + "' and sw='" + this.textBox2.Text.Trim() + "'"; SqlCommand cmd = new SqlCommand(sql, conn); try { conn.Open(); MessageBox.Show("成功"); } catch (Exception) { conn.Close(); MessageBox.Show("密码或用户名不对"); } SqlDataReader dateReader = cmd.ExecuteReader(); if (dateReader.Read()) { MessageBox.Show("即将进入画面"); timer1.Start(); conn.Dispose(); conn.Close(); } } catch (Exception) { MessageBox.Show("系统出错!"); } } private void button2_Click(object sender, EventArgs e) { Form2 aa = new Form2(); aa.Show(); } private void timer1_Tick(object sender, EventArgs e) { count++; progressBar1.Minimum = 0; progressBar1.Maximum = time; progressBar1.Visible = true; progressBar1.Value = count; Application.DoEvents(); if (count == time) { timer1.Stop(); MessageBox.Show("欢迎"); Form3 xx = new Form3(); xx.Show(); this.Hide(); count = 0; progressBar1.Value = count; progressBar1.Visible = false; } } } }
Form2:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApp2 { public partial class Form2 : Form { string bb; bool aa; public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string sqlstring = "server=ZJX-PC;uid=sa;pwd=zjx28963436;database=zjx"; SqlConnection conn = new SqlConnection(sqlstring); if (textBox1.Text != "" && textBox2.Text != "") { conn.Open(); string sql = " insert into info(id,sw) values ('" + textBox1.Text + "'," + "'" + textBox2.Text + "') "; //要执行的 sql 语句 string sql2 ="Select id from info where id ='" + this.textBox1.Text.Trim() + "'"; SqlCommand cmd = new SqlCommand(sql, conn);//同登录 SqlCommand cmd2 = new SqlCommand(sql2, conn); cmd.CommandText = sql; try { bb = cmd2.ExecuteScalar().ToString(); MessageBox.Show("用户已存在!"); textBox1.Clear(); textBox2.Clear(); //aa = false; } catch (Exception) { // MessageBox.Show("用户没用过"); // aa = true; int i = int.Parse(cmd.ExecuteNonQuery().ToString()); if (i > 0) { MessageBox.Show("恭喜你注册成功!", "!"); this.Close(); } } } else { MessageBox.Show("不能为空", "警告"); textBox1.Clear(); textBox2.Clear(); } conn.Close(); } } }
Form3:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } } }