using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MySql.Data.MySqlClient;
namespace Db
{
public class SqlHelper
{
private MySqlCommand cmd;//执行一条sql语句。
private MySqlConnection con;
private MySqlDataReader reader = null;
MySqlDataAdapter msda;//Represents a set of data commands and a database connection that are used to fill a data set and update a MySQL database.
//连接字符串
private static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//查询与ataGridView控件配套
public DataTable ExecuteQuery_DataTable(string sqlStr)
{
//查
DataTable dt = new DataTable();//用于ataGridView控件
con = new MySqlConnection(connStr);
con.Open();//打开链接,可以省略,建议写上
cmd = new MySqlCommand(sqlStr, con);
try
{
cmd.CommandType = CommandType.Text;
msda = new MySqlDataAdapter(cmd);
msda.Fill(dt);
}
catch (Exception ex) {}
return dt;
}
//查询使用reader,一次读一条,类似于c语言读文件。
public void ExecuteQuery(string sqlStr)
{
//查
con = new MySqlConnection(connStr);
con.Open();//打开链接,可以省略,建议写上
cmd = new MySqlCommand(sqlStr, con);
reader = cmd.ExecuteReader();
//while (reader.Read())
//{
// string str="ID=" + reader[0].ToString() + " ,TITLE=" + reader[1].ToString() + " ,KEYWORD=" +
// reader[2].ToString() + " ,CONTENT=" + reader[3].ToString() + ".";
//}
}
public int ExecuteUpdate(string sqlStr)
{
//增删改
MySqlCommand cmd;
MySqlConnection con;
con = new MySqlConnection(connStr);
con.Open();
cmd = new MySqlCommand(sqlStr,con);
cmd.CommandType = CommandType.Text;
int iud = 0;
iud = cmd.ExecuteNonQuery();
con.Close();
return iud;
}
public void allClose()
{
con.Close();
}
public MySqlDataReader getReader()
{
return reader;
}
}
}
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 Db;
using MySql.Data.MySqlClient;
namespace WindowsFormsAppCS
{
public partial class Form1 : Form
{
private Form2 anotherForm;
private SqlHelper sqlHelper = new SqlHelper();
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//数据清空
userid_input.Clear();
userpassword_input.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
//接收信息
string userName = userid_input.Text.Trim();
string userPass = userpassword_input.Text.Trim();
//判断是否为空
if(string.IsNullOrEmpty(userName))
{
MessageBox.Show("用户名不能为空");
userid_input.Focus();
return;
}
if (string.IsNullOrEmpty(userPass))
{
MessageBox.Show("密码不能为空");
userid_input.Focus();
return;
}
//登录检查
string sql = "select * from users where userid='" + userName + "' and userpass='" + userPass + "'";
sqlHelper.ExecuteQuery(sql);
if(sqlHelper.getReader().Read())
{
sqlHelper.allClose();
anotherForm = new Form2();
this.Hide();
anotherForm.ShowDialog();
Application.ExitThread();
}
else
{
MessageBox.Show("用户名密码错误");
sqlHelper.allClose();
return;
}
}
}
}