ADO.NET连接MYSQL

在这里插入图片描述
先拓展mysql的插件

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace adoNet
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            MySqlConnection mysqlCon = null;
            try
            {
                String con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
                mysqlCon = new MySqlConnection(con);
                mysqlCon.Open();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }finally
            {
                mysqlCon.Close();
            }
         
    }
    }
}

在配置文件中插入数据库信息connectionStrings
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
    
	<connectionStrings>
		<add name="conn" connectionString="server=1xx.xx.xx.xx;port=13xx6;Database=my_test;UID=root;PWD=xxxxx" providerName="MySql.Data.MySqlClient"/>
	</connectionStrings>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用ADO.NET连接MySQL数据库的步骤: 1. 下载MySQL Connector/NET,链接为:http://dev.mysql.com/downloads/connector/net/5.0.html,选择.NET & Mono平台,下载免安装的.zip文件。 2. 解压下载的文件,将其中的MySql.Data.dll文件添加到你的项目中。 3. 在代码中引用MySql.Data命名空间。 4. 创建一个MySqlConnection对象,设置连接字符串,连接字符串的格式为:"server=服务器地址;user id=用户名;password=密码;database=数据库名"。 5. 调用MySqlConnection对象的Open()方法打开数据库连接。 6. 执行SQL语句或存储过程,可以使用MySqlCommand对象的ExecuteNonQuery()方法执行非查询语句,使用ExecuteReader()方法执行查询语句并返回一个MySqlDataReader对象,使用ExecuteScalar()方法执行查询语句并返回结果集中的第一行第一列的值。 7. 关闭数据库连接,调用MySqlConnection对象的Close()方法关闭连接。 下面是一个连接MySQL数据库并执行查询语句的示例代码: ```csharp using System; using MySql.Data.MySqlClient; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string connStr = "server=localhost;user id=root;password=123456;database=test"; MySqlConnection conn = new MySqlConnection(connStr); try { conn.Open(); string sql = "SELECT * FROM user"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["id"] + "\t" + reader["name"] + "\t" + reader["age"]); } reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { conn.Close(); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值