我如何设置MySQL与C#的工作? [英] How do I set up MySQL to work with C#?

查看:152
本文介绍了我如何设置MySQL与C#的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直负责创造一个传统的网站一个新的前端。

I have been tasked with creating a new frontend for a legacy website.

这是写在PHP(预OO),并使用MySQL数据库。托管提供了一个净包,但不提供MS SQL Server中。

It is written in php (pre-oo), and uses a MySQL database. The hosting provides a .Net package, but does not offer Ms Sql Server.

这是好的,因为数据库工作正常,但我真的想用Asp.net的页面。不过,我已经看到了从C#连接MySQL最需要的教程为MySQL安装专门的ODBC驱动程序。未控制的托管ENV,我怀疑我将能够做到这一点?)

This is fine, as the database is working fine, but I really want to use Asp.net for the pages. However, most tutorials I've seen on connecting to MySQL from C# require installing an ODBC Driver specifically for MySQL. Not controlling the hosting env, I doubt I'll be able to do just that :)

你有什么见解,分享关于这个问题

Have you got any insight to share on this issue?

推荐答案

MySQL本身具有的对于.NET连接器。你并不需要使用ODBC,

MySql does have connector for .Net. You do not need to use ODBC,

的MySQL Connector将让你与你的MySQL数据库进行交互,并全面管理ADO.Net提供商。你有二进制文件(DLL)或源代码,如果你的愿望。这很简单,一旦你已经导入的DLL,你只需要一个联接字符串(用户名,密码,位置),你会被设置!

MySql Connector will let you interact with your MySql database and is fully managed ADO.Net provider. You have the binary (dll) or the source code if you desire. It's pretty simple, once you have imported the dll you just need a connexion string (username,password,location) and you will be setup!

下面是一个代码示例(参考: bitdaddy.com ):

Here is a sample of code (ref: bitdaddy.com):

string MyConString = "SERVER=localhost;" +
	"DATABASE=mydatabase;" +
	"UID=testuser;" +
	"PASSWORD=testpassword;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select * from mycustomers";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
	string thisrow = "";
	for (int i= 0;i<Reader.FieldCount;i++)
			thisrow+=Reader.GetValue(i).ToString() + ",";
	listBox1.Items.Add(thisrow);
}
connection.Close();



我建议你不要把你的代码和持久性在同一个地方,把你的联接字符串在你App.Config中,但我认为这个节目,你如何做到这一点。

I suggest you to do not put your code and persistance in the same place and to place your connexion string in you App.Config, but I think this show you how to do it.

这篇关于我如何设置MySQL与C#的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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