一.说明
这一篇简单的个人总结,方便自己的复制粘贴,软件是Visual Studio 2019,SQLyog
二.配置
一.在Web.config中的configuration标签下添加如下代码:
<connectionStrings><add name&#61;"Conn_example" connectionString&#61;"Database&#61;&#39;example&#39;;Data Source&#61;&#39;127.0.0.1&#39;;User Id&#61;&#39;root&#39;;Password&#61;&#39;xxxxx&#39;;charset&#61;&#39;utf8&#39;;pooling&#61;true"/>
connectionStrings>
二.创建一个名为MysqlData.cs的类,并写入如下代码:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;namespace MyTest03
{public class MysqlData{public static string DataBase &#61; ConfigurationManager.ConnectionStrings["Conn_example"].ToString();}
}
三.在官网下载MySql.Data.dll,并将其引用,大家请在网上查找
三.连接
创建一个asp.net web 窗口,输入如下代码:
<%&#64; Page Language&#61;"C#" AutoEventWireup&#61;"true" CodeBehind&#61;"WebForm2.aspx.cs" Inherits&#61;"MyTest03.WebForm2" %>DOCTYPE html><html xmlns&#61;"http://www.w3.org/1999/xhtml">
<head runat&#61;"server">
<meta http-equiv&#61;"Content-Type" content&#61;"text/html; charset&#61;utf-8"/><title>title>
head>
<body><form id&#61;"form1" runat&#61;"server"><div><asp:Button ID&#61;"Button1" runat&#61;"server" OnClick&#61;"Button1_Click" Text&#61;"增" Width&#61;"31px" /><br /><asp:Button ID&#61;"Button2" runat&#61;"server" OnClick&#61;"Button2_Click" Text&#61;"删" /><asp:TextBox ID&#61;"TextBox1" runat&#61;"server">asp:TextBox><br /><asp:Button ID&#61;"Button4" runat&#61;"server" OnClick&#61;"Button4_Click" Text&#61;"改" /><asp:TextBox ID&#61;"TextBox2" runat&#61;"server">asp:TextBox><br /><asp:Button ID&#61;"Button3" runat&#61;"server" OnClick&#61;"Button3_Click" Text&#61;"查" /><br />div>form>
body>
html>
其界面效果是这样的:
而对应的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 MyTest03
{public partial class WebForm2 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected void Button3_Click(object sender, EventArgs e){ try {string sql &#61; "select * from student";MySqlDataReader DR &#61; MySqlHelper.ExecuteReader(MysqlData.DataBase, sql);string sex &#61; "";while (DR.Read()){sex &#61; "";if (DR.GetInt32(2) &#61;&#61; 1){sex &#61; "男";}else{sex &#61; "女";}Response.Write("编号:" &#43; DR.GetString(0) &#43; ",姓名:" &#43; DR.GetString(1) &#43; ",性别:" &#43; sex &#43; "
");}DR.Close();} catch(Exception ex) {Response.Write(ex.Message);}}protected void Button1_Click(object sender, EventArgs e){string sql &#61; "insert into student value(4,&#39;小绿&#39;,0)";MySqlConnection conn &#61; new MySqlConnection(MysqlData.DataBase);conn.Open();MySqlCommand cmd &#61; new MySqlCommand();cmd.CommandText &#61; sql;cmd.Connection &#61; conn;int result &#61;cmd.ExecuteNonQuery();cmd.Dispose();conn.Close();if (result > 0){Response.Write("增加成功!");}else{Response.Write("增加失败!");}}protected void Button2_Click(object sender, EventArgs e){string te &#61; TextBox1.Text;string sql &#61; "delete from student where name&#61;&#39;" &#43; te &#43; "&#39;";MySqlConnection conn &#61; new MySqlConnection(MysqlData.DataBase);conn.Open();MySqlCommand cmd &#61; new MySqlCommand();cmd.CommandText &#61; sql;cmd.Connection &#61; conn;int result &#61; cmd.ExecuteNonQuery();cmd.Dispose();conn.Close();if (result > 0){Response.Write("删除成功!");}else{Response.Write("删除失败!");}}protected void Button4_Click(object sender, EventArgs e){string te &#61; TextBox2.Text;string sql &#61; "update student set id&#61;6 where name&#61;&#39;" &#43; te &#43; "&#39;";MySqlConnection conn &#61; new MySqlConnection(MysqlData.DataBase);conn.Open();MySqlCommand cmd &#61; new MySqlCommand();cmd.CommandText &#61; sql;cmd.Connection &#61; conn;int result &#61;cmd.ExecuteNonQuery();cmd.Dispose();conn.Close();if (result > 0){Response.Write("修改成功!");}else {Response.Write("修改失败!");}}}
}
四.结尾
为了数据库的安全,一般都是在取得返回之后,就将命令对象,连接对象关闭.
此为简陋的测试,接下来的学习中,如果遇到其他值得记录的代码,也会给小伙伴们分享.谢谢大家!