MySQL连接字符串C# [英] MySQL Connection String C#

查看:435
本文介绍了MySQL连接字符串C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误:

"ObdcException未被用户代码处理"

我不知道为什么...

I dont know why this...

这是连接字符串:

<add name="MiniBoxConnection" connectionString="DRIVER={MySQL ODBC 5.1 Driver};Database=DATABASENAME;Server=SERVERNAME;UID=USER;PWD=PASS;"/>

我该如何解决这个问题?

how can i solve this problem?

我正在使用localhost进行开发,但是数据库处于联机状态

i'm developing in the localhost, but database is online

未找到数据源的名称,并且未指定默认驱动程序

推荐答案

您正尝试使用ODBC从.net代码连接到MySQL数据库.您的错误消息告诉您尚未创建适当命名的ODBC数据源对象(DSN).如果需要,可以使用ODBC数据源管理员控制台进行操作.

You are attempting to connect to your MySQL database from your .net code using ODBC. Your error message is telling you that you haven't created an appropriately named ODBC data source object (DSN). You can do that with the ODBC Data Source Adminstrator control panel if you need to.

如果您是我,我将使用连接器/NET而不是ODBC.它的性能更好,而且正确配置也不会给您带来很大的痛苦.

If I were you I would use Connector/NET instead of ODBC. It performs better and it isn't quite such a pain in the neck to configure correctly.

您可以在此处下载安装工具包. http://dev.mysql.com/downloads/connector/net/

You can download the install kit for it here. http://dev.mysql.com/downloads/connector/net/

您需要为此更改代码.但是,这是值得的!严重地!您的代码最终将看起来像这样.

You'll need to change your code for this. But, it's worth it! Seriously! Your code will end up looking like this.

using System;
//etc etc
using MySql.Data.MySqlClient;
//etc etc

namespace myapp
{
    class Myclass
    {
        static void Mymethod(string[] args)
        {
            string connStr = "server=server;user=user;database=db;password=*****;";
            MySqlConnection conn = new MySqlConnection(connStr);
            conn.Open();

            string sql = "SELECT this FROM that";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            using (MySqlDataReader rdr = cmd.ExecuteReader()) {
                while (rdr.Read()) {
                    /* iterate once per row */
                }
            }
        }
    }
}

这篇关于MySQL连接字符串C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆