非常简单,直接给出代码:
数据库操作类
这个无需多言就是简单的包含了数据操作的内容,允许你在这一个类中写完关于本地数据库或者云数据库操作的逻辑,与登录逻辑分开哦。
注意,如果你的软件要给别人运行使用,你的数据库信息也可以存在授权文件中,以避免出现信息的泄露。如果你使用软件直接操作数据库可能存在危险,请注意连接数据库时对sql语句的检查。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace chuankouzhushou1
{internal class dataoperator{private static string connString = @"Data Source=DREAMER;Database=db_Mydb;User ID=sa;Pwd=yourpasswd;";public static SqlConnection connection = new SqlConnection(connString);public int ExecuteSQL(string sql){SqlCommand cmd = new SqlCommand(sql, connection);if (connection.State == ConnectionState.Closed)connection.Open();int num = Convert.ToInt32(cmd.ExecuteScalar()); //执行查询connection.Close();return num;}public int ExecuteSQLResult(string sql){SqlCommand cmd = new SqlCommand(sql, connection);if (connection.State == ConnectionState.Closed)connection.Open();int result = cmd.ExecuteNonQuery();connection.Close();return result;}public DataSet GetDataSet(string sql){SqlDataAdapter sqlda = new SqlDataAdapter(sql, connection); //指定要执行的SQL语句DataSet ds = new DataSet();sqlda.Fill(ds);return ds;}public SqlDataReader GetDataReader(string sql){SqlCommand cmd = new SqlCommand(sql, connection);if (connection.State == ConnectionState.Closed)connection.Open();SqlDataReader dataReader = cmd.ExecuteReader();//添加参数CommandBehavior.CloseConnection就可以自动释放连接return dataReader;}}
}
输入的监测
private bool ValidateInput(){if (tx_ID.Text.Trim() == "") //登录账号{MessageBox.Show("请输入登录账号", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);tx_ID.Focus(); //使登录账号文本框获得鼠标焦点return false;}else if (tx_ID.Text.Length>30){MessageBox.Show("请输入正确的登录账号", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);tx_ID.Focus(); //使登录账号文本框获得鼠标焦点return false;}else if (tx_ID.Text.Length > 5 && tx_PWD.Text.Trim() == "") //密码{MessageBox.Show("请输入密码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);tx_PWD.Focus(); //使密码文本框获得鼠标焦点return false;}return true;}
登录按钮(回车实现登录)
private void loginbtn(object sender, EventArgs e){if(ValidateInput()){int num= 0;string sql = "select count(*) from usertb where userid=" + int.Parse(tx_ID.Text.Trim()) + " and userpwd = '" + tx_PWD.Text.Trim() + "'";try{num = dbop.ExecuteSQL(sql);}catch {//MessageBox.Show(e.ToString());} if (num == 1)//验证通过{publicinfo.userid = int.Parse(tx_ID.Text.Trim());// 执行新的SQL查询来获取userid,username和userlvsql = "select userid, username, userlv from usertb where userid=" + publicinfo.userid;SqlDataReader reader = dbop.GetDataReader(sql);if (reader.Read()){publicinfo.userid = reader.GetInt32(0);publicinfo.username = reader.GetString(1);publicinfo.userlv = reader.GetInt32(2);}reader.Close();mainfrm frm_Main = new mainfrm();frm_Main.Show();this.Visible = false;}else{MessageBox.Show("登录失败!", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}}}private void tx_ID_KeyPress(object sender, KeyPressEventArgs e){if (char.IsDigit(e.KeyChar) || (e.KeyChar == '\r') || (e.KeyChar == '\b'))e.Handled = false;elsee.Handled = true;}