mysql .net连接字符串,MySQL连接字符串C#

本文介绍了如何解决使用ODBC从.NET应用连接MySQL时遇到的ObdcException was unhandled by user code错误。建议使用MySQL Connector/NET替代ODBC,并提供了具体的配置步骤和示例代码。

I've this error:

"ObdcException was unhandled by user code"

1ChvS.png

I dont know why this...

This is the connection string:

how can i solve this problem?

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

The name of the data source was not found and there was no default driver specified

解决方案

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.

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.

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 */

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值