zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }
  • 相关阅读:
    【BZOJ1854】游戏[SCOI2009](神奇贪心+并查集)
    P4177 [CEOI2008]order(网络流)最大权闭合子图
    P2472 [SCOI2007]蜥蜴(网络流)
    P2877 [USACO07JAN]牛校Cow School(01分数规划+决策单调性分治)
    bzoj3437 小P的牧场(斜率优化dp)
    P3628 [APIO2010]特别行动队(斜率优化dp)
    P3648 [APIO2014]序列分割(斜率优化dp)
    P4027 [NOI2007]货币兑换(斜率优化dp+cdq分治)
    P2365 任务安排 / [FJOI2019]batch(斜率优化dp)
    P3195 [HNOI2008]玩具装箱TOY(斜率优化dp)
  • 原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/15551054.html
Copyright © 2011-2022 走看看