源代码下载

​ ​点击打开链接下载源代码(超链接)​​

首先上MySQL网站下驱动(以前学Java的时候,要用connecter来做):MySQL :: MySQL Connectors

NET连接MySQL数据库_sql

安装下载的安装包,此处略去一万字

创建一个项目(我用的是Visual Studio 2017),创建成功后添加引用

找到引用的位置,下图是我的位置,如果你的位置正确,会含有下面标记的两个dll文件

NET连接MySQL数据库_NET连接MySQL_02

添加引用

项目右键----------------》添加---------------------------》引用

找到上图标记的的两个文件,添加到项目中

NET连接MySQL数据库_NET连接MySQL_03

NET连接MySQL数据库_NET连接MySQL数据库_04

把上上上图标记的两个DLL文件添加进来

查看引用是否添加的方法(项目右键--》属性页)

NET连接MySQL数据库_NET_05

NET连接MySQL数据库_sql_06

修改Web.config文件(添加一个配置信息)

需要修改的是(id = 数据库用户名)(password = 数据库密码)(database = 数据库名称),注意一下name属性要和后面的代码中一致

需要添加的代码

<connectionStrings>
<add name="MySqlStr" connectionString="server = localhost; user id = root; password = root; database = text"/>
</connectionStrings>

代码的位置  configuration  节点的子节点

<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.6.1">
<assemblies>
<add assembly="MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
<add assembly="MySql.Web, Version=6.9.9.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>


<connectionStrings>
<add name="MySqlStr" connectionString="server = localhost; user id = root; password = root; database = text"/>
</connectionStrings>


</configuration>

我的数据库信息

NET连接MySQL数据库_NET连接MySQL数据库_07

查询代码

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;//新引入的命名空间
using System.Configuration;//新引入的命名空间

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

}

protected void Button1_Click(object sender, EventArgs e)
{
//获得Web.config中的配置信息
string sqlCconnStr = ConfigurationManager.ConnectionStrings["MySqlStr"].ConnectionString;
MySqlConnection sqlCon = new MySqlConnection(sqlCconnStr);//连接数据库

//打开连接
sqlCon.Open();

//SQL语句
string sql = "select username from user ";

//获得MySsqlCommand
MySqlCommand cmd = new MySqlCommand(sql, sqlCon);

//执行SQL并且返回查询结果
MySqlDataReader dataReader = cmd.ExecuteReader();

//处理返回结果
while (dataReader.Read())
{

//string obj = dataReader.GetString(0);
string obj = dataReader.GetString("username");
//Console.WriteLine(obj);
Label1.Text += obj;
}

//关闭连接
cmd = null;
sqlCon.Close();
sqlCon = null;







}
}