• C#windowsform 连接数据库


    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;
                }
            }
        }
    }
  • 相关阅读:
    删除List集合中的元素你碰到过这样的陷阱吗?
    从spring框架中的事件驱动模型出发,优化实际应用开发代码
    SpringBoot启动原理及相关流程
    基于SpringBoot实现定时任务的设置(常用:定时清理数据库)
    C#开发中常用的加密解密方法
    http://go.microsoft.com/fwlink/?linkid问题
    移动端开发必须知道的小技巧
    工作中遇到的细节问题总结(二)
    redis分布式锁和消息队列
    join和wait
  • 原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/15551054.html
Copyright © 2020-2023  润新知