C#連接MySQL數(shù)據(jù)庫
準(zhǔn)備工作
1.環(huán)境安裝
安裝MySQL For Visual Studio<<點(diǎn)擊進(jìn)入官網(wǎng)下載
第一個要下載安裝,第二個下載后將MySQL.data添加到Visual Studio的項(xiàng)目引用當(dāng)中。
2.準(zhǔn)備好數(shù)據(jù)庫
2.1 創(chuàng)建數(shù)據(jù)庫
2.2 創(chuàng)建創(chuàng)建數(shù)據(jù)表
2.3 為數(shù)據(jù)表添加數(shù)據(jù)
3.數(shù)據(jù)庫檢查
3.1 檢查數(shù)據(jù)表是否有主鍵
數(shù)據(jù)表中必須至少有一個主鍵
3.2 檢查數(shù)據(jù)列編碼方法與排序方法
字符集一般使用utf8mb4
3.3 檢查MySQL服務(wù)是否正在運(yùn)行
創(chuàng)建C# Web項(xiàng)目
1 添加引用
在 項(xiàng)目 -> 引用 中,添加引用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)
{
//接受前端數(shù)據(jù)并進(jìn)行簡單處理
string usrName = tb1.Text.Trim();
string usrPwd = tb2.Text.Trim();
//驗(yàn)證數(shù)據(jù)是否合理
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
//{
//設(shè)計(jì)連接字符串(連接數(shù)據(jù)庫)
string conn =
"Data Source = 127.0.0.1;" +
"User ID=root;" +
"Password=qq2686485465;" +
"DataBase=omtpc;" +
"port=3306";
//定義連接對象(構(gòu)造函數(shù)的參數(shù)為數(shù)據(jù)庫連接字符串)
MySqlConnection con = new MySqlConnection(conn);
//打開數(shù)據(jù)庫連接
con.Open();
//執(zhí)行數(shù)據(jù)庫的訪問操作
string strSqlCommand = "Select*from officer21 where usrID='" + usrName + "'";
MySqlCommand cmd = new MySqlCommand(strSqlCommand, con);
MySqlDataReader dr = cmd.ExecuteReader();//查找多行 : ExecuteReader()方法 | 執(zhí)行結(jié)果放入dr中
//dr.Read();//讀出dr內(nèi)容
if (dr.Read())
{
string queryPassword = dr["password"].ToString();
if (usrPwd == queryPassword)
{
lbl_Message.Text = "驗(yàn)證成功";
Response.Redirect("welcome.aspx");
}
else
{
lbl_Message.Text = "驗(yàn)證失敗";
}
}
else {
lbl_Message.Text = "用戶名錯誤";
}
//結(jié)束
dr.Close();
con.Close();
//}
//catch (MySqlException ex)
//{
// Console.WriteLine(ex.Message);//有錯則報(bào)出錯誤
//}
//finally
//{
//}
}
}
}
繼續(xù)閱讀
連接數(shù)據(jù)庫流程介紹、代碼的解釋,請閱讀下面這篇博文
C#連接MySQL數(shù)據(jù)庫(二)解析
閱讀密碼:9920
本文摘自 :https://www.cnblogs.com/