【C#】【MySQL】C#連接MySQL數據庫(一)代碼


C#連接MySQL數據庫

准備工作

1.環境安裝

安裝MySQL For Visual Studio<<點擊進入官網下載

第一個要下載安裝,第二個下載后將MySQL.data添加到Visual Studio的項目引用當中。

2.准備好數據庫

2.1 創建數據庫

2.2 創建創建數據表

2.3 為數據表添加數據

3.數據庫檢查

3.1 檢查數據表是否有主鍵

數據表中必須至少有一個主鍵

3.2 檢查數據列編碼方法與排序方法

字符集一般使用utf8mb4

3.3 檢查MySQL服務是否正在運行

創建C# Web項目

1 添加引用

在 項目 -> 引用 中,添加引用MySQL.Data

2 Web窗體-Login-前端代碼如下

WebForm_Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_Login.aspx.cs" Inherits="WebApplication_OmtpcMgrSystem.sign.WebForm_Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lbl1" runat="server" Text="用戶名"></asp:Label>
            <asp:TextBox ID="tb1" runat="server"></asp:TextBox>
        </div>
        <asp:Label ID="lbl2" runat="server" Text="密碼"></asp:Label>
        <asp:TextBox ID="tb2" runat="server"></asp:TextBox>
        <br />
      <asp:Label ID="lbl_Message" runat="server" Text=""></asp:Label>
        <br />
        <asp:Button ID="btl_Login" runat="server" Text="登錄" OnClick="btl_Login_Click" />
        <br />
        <asp:HyperLink ID="hre_forget" runat="server">忘記密碼</asp:HyperLink>
        <asp:HyperLink ID="hre_reg" runat="server">注冊</asp:HyperLink>
    </form>
</body>
</html>

3 Web窗體-Login-后端代碼如下

WebForm_Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;


namespace WebApplication_OmtpcMgrSystem.sign
{
    public partial class WebForm_Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btl_Login_Click(object sender, EventArgs e)
        {
            //接受前端數據並進行簡單處理
            string usrName = tb1.Text.Trim();
            string usrPwd = tb2.Text.Trim();
            //驗證數據是否合理
            if (usrName.Length == 0 || usrName.Length > 100)
            {
                lbl_Message.Text = "UserName is wrong!";
            };
            if (usrPwd.Length < 6 || usrPwd.Length > 100)
            {
                lbl_Message.Text = "UserPassword is wrong!";
            }
            //try
            //{
                //設計連接字符串(連接數據庫)
                string conn =
                    "Data Source = 127.0.0.1;" +
                    "User ID=root;" +
                    "Password=qq2686485465;" +
                    "DataBase=omtpc;" +
                    "port=3306";
                //定義連接對象(構造函數的參數為數據庫連接字符串)
                MySqlConnection con = new MySqlConnection(conn);
                //打開數據庫連接
                con.Open();
                //執行數據庫的訪問操作
                string strSqlCommand = "Select*from officer21 where usrID='" + usrName + "'";
            MySqlCommand cmd = new MySqlCommand(strSqlCommand, con);
                MySqlDataReader dr = cmd.ExecuteReader();//查找多行 : ExecuteReader()方法 | 執行結果放入dr中
                //dr.Read();//讀出dr內容

            if (dr.Read())
            {
                string queryPassword = dr["password"].ToString();
                if (usrPwd == queryPassword)
                {
                    lbl_Message.Text = "驗證成功";
                    Response.Redirect("welcome.aspx");
                }
                else
                {
                    lbl_Message.Text = "驗證失敗";
                }
            }
            else {
                lbl_Message.Text = "用戶名錯誤";
            }
                //結束
                dr.Close();
                con.Close();
                


            //}
            //catch (MySqlException ex)
            //{
            //    Console.WriteLine(ex.Message);//有錯則報出錯誤
            //}

            //finally
            //{

            //}



        }


        }
}

繼續閱讀

連接數據庫流程介紹、代碼的解釋,請閱讀下面這篇博文
C#連接MySQL數據庫(二)解析
閱讀密碼:9920


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM