使用C#和SQL语句来制作一个简单的后台登录系统
开发工具主要是运用到了VS2017和Sql大数据
登录界面,通过输入账号和密码进行登录 代码如下:
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 WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 登录按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
string pwd = textBox2.Text;
if (name==""||pwd=="")
{
MessageBox.Show("用户名或密码为空,请重新输入!!!");
}
else
{
string sql = string.Format("select *from Users where Uname='{0}' and Upwd='{1}'",name,pwd);
DataTable dt = DBHelper.GetDataTable(sql);
if (dt.Rows.Count>0)
{
MessageBox.Show("登陆成功!!");
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
else
{
MessageBox.Show("登录失败!!");
}
}
}
/// <summary>
/// 取消按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
登录成功之后可以自动显示数据库中的数据,我们也可以对数据进行查询和修改等操作 代码如下:
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 WindowsFormsApp3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 窗体加载显示表格中的数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form2_Load(object sender, EventArgs e)
{
//将数据显示到表格中
string sql = string.Format("select*from Students");
DataTable dt = DBHelper.GetDataTable(sql);
dataGridView1.DataSource = dt;
}
/// <summary>
/// 搜索按钮 判断搜索内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "姓名")
{
string sql = string.Format("select * from Students where name like '%{0}%'", textBox1.Text);
DataTable dt = DBHelper.GetDataTable(sql);
dataGridView1.DataSource = dt;
}
else if (comboBox1.Text == "班级")
{
string sql = string.Format("select*from Students where class like '%{0}%'", textBox1.Text);
DataTable dt = DBHelper.GetDataTable(sql);
dataGridView1.DataSource = dt;
}
else if (comboBox1.Text == "学号")
{
string sql = string.Format("select*from Students where xuehao like'%{0}%'", textBox1.Text);
DataTable dt = DBHelper.GetDataTable(sql);
dataGridView1.DataSource = dt;
}
else if (comboBox1.Text == "地址")
{
string sql = string.Format("select*from Students where address like'%{0}%'", textBox1.Text);
DataTable dt = DBHelper.GetDataTable(sql);
dataGridView1.DataSource = dt;
}
else
{
MessageBox.Show("下拉框中的值不可以修改和为空!!!!");
}
}
/// <summary>
/// 添加按钮 跳转到添加窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 添加学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
}
/// <summary>
/// 刷新修改后的数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 刷新学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2_Load(sender, e);
}
/// <summary>
/// 将表格中的值显示到textBox当中进行修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string sql = string.Format("select * from students where id ={0}",
dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
DataTable dt = DBHelper.GetDataTable(sql);
//赋值textBox2.Text = dt.Rows[0]["xuehao"].ToString();
if (dt !=null && dt.Rows.Count>0)
{
textBox2.Text = dt.Rows[0]["xuehao"].ToString();
textBox3.Text = dt.Rows[0]["name"].ToString();
textBox4.Text = dt.Rows[0]["sex"].ToString();
textBox5.Text = dt.Rows[0]["class"].ToString();
textBox6.Text = dt.Rows[0]["address"].ToString();
}
}
/// <summary>
/// 修改按钮的代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text=="")
{
MessageBox.Show("请输入您需要修改的值!!!");
}
else if(MessageBox.Show("是否进行修改???","修改",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
string sql = string.Format("update Students set xuehao='{0}',name='{1}',sex='{2}',class='{3}',address='{4}' where id='{5}'",
Convert.ToString(textBox2.Text), Convert.ToString(textBox3.Text), Convert.ToString(textBox4.Text),
Convert.ToString(textBox5.Text), Convert.ToString(textBox6.Text), dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
if (DBHelper.Zsg(sql))
{
MessageBox.Show("修改成功!!!请进行刷新!!");
}
else
{
MessageBox.Show("修改失败!!!请重新输入!!");
}
}
}
/// <summary>
/// 删除代码样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否删除??","删除:",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
string sql = string.Format("delete from Students where id='{0}'", dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
if (DBHelper.Zsg(sql))
{
MessageBox.Show("删除成功!!!");
}
else
{
MessageBox.Show("删除失败!!");
}
}
}
/// <summary>
/// 通过下拉框显示“在读”“退学”“全部”状态的学生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.Text=="全部")
{
Form2_Load(sender, e);
}
else
{
string sql = string.Format("select*from Students where zhuatai='{0}'", comboBox2.Text);
DataTable dt = DBHelper.GetDataTable(sql);
dataGridView1.DataSource = dt;
}
}
/// <summary>
/// 取消按钮 清空textBox中的数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
textBox2.Text = null;
textBox3.Text = null;
textBox4.Text = null;
textBox5.Text = null;
textBox6.Text = null;
}
}
}
添加界面 代码如下
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 WindowsFormsApp3
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
/// <summary>
/// 添加代码样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string sql = string.Format("insert into Students select '{0}','{1}','{2}','{3}','{4}','{5}'"
,textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text,textBox5.Text,textBox6.Text);
if (DBHelper.Zsg(sql))
{
MessageBox.Show("添加成功!!!");
this.Hide();
}
else
{
MessageBox.Show("添加失败!!!数据输入有误!!");
}
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
DBHlper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace WindowsFormsApp3
{
class DBHelper
{
//数据库链接字符串
public static string sqlconn = "Data Source=.;Initial Catalog=Student;Integrated Security=True";
public static SqlConnection conn = new SqlConnection(sqlconn);
//封装方法 断开连接
public static DataTable GetDataTable(string sql)
{
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
//增删改
public static bool Zsg(string sql)
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
int result = cmd.ExecuteNonQuery();
conn.Close();
return result > 0;
}
//聚合函数
public static object Juhe(string sql)
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
object result = cmd.ExecuteScalar();
conn.Close();
return result;
}
}
}
当然除了以上的代码外还有许多控件的属性需要调整比如:
通过键盘的Enter键来点击登录按钮:在Form1窗体属性中的AcceptButton属性
Esc键来点击取消按钮:在Form1窗体属性中的CancelButton属性
右键菜单:ContextMenuStrip属性等
说明:纯属分享自己最近学习时写的作业,不喜勿喷!谢谢!如果您有什么更好的建议可以在下方评论出来,大家可以一起学习!