1. 程式人生 > 其它 >C#windowsform 連線資料庫

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;
            }
        }
    }
}