ASP.NET 连接MySql数据库
以下连接MySql数据库以VS2010为例,对于其他的编辑器也差不多
1. 我们需要在Mysql官网下载一个组件http://dev.mysql.com/downloads/connector/net/ 下载最新版的即可,并且安装connector,其实仅仅只是为了得到Mysql.Data.dll ( 0.0 )
2. 在你的VS2010 解决方案管理器里面添加 Mysql.Data.dll 引用(通常在c:\program files\MySQL\MySQL Connector Net(your version here)\Assemblies\v4.0 or v2.0)
3. 在你的web.config里面添加connection:
- <connectionStrings>
- <add name="MySQLConnString" connectionString="Server=your_MySql_Server_IP;Port=3306;Database=databaseName;Uid=username;Pwd=yourpassword;pooling=false;" providerName="MySql.Data.MySqlClient"/>
- </connectionStrings>
4. 在你的代码前面添加using MySQL.Data.MySqlClient;
下面是个简单的连接Mysql 并且使用的例子
- protected void Page_Load(object sender, EventArgs e)
- {
- DataSet ds_temp = GetObjects();
- ListBox1.DataSource = ds_temp;
- ListBox1.DataTextField = "yourColumn";
- ListBox1.DataValueField = "yourColumn";
- ListBox1.DataBind();
- }
- protected static DataSet GetObjects()
- {
- DataSet ds_temp = Connection_cls.GetDataSetQuery("SELECT yourColumn FROM yourtable", null);
- return ds_temp;
- }
- private static string getHostString()
- {
- string host = ConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;
- return host;
- }
- public static DataSet GetDataSetQuery(string sql, ArrayList paramList)
- {
- using (MySqlConnection conn = new MySqlConnection(getHostString()))
- {
- try
- {
- conn.Open();
- MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
- da.SelectCommand.Parameters.Clear();
- if (paramList != null)
- {
- for (int i = 0; i < paramList.Count; i++)
- {
- da.SelectCommand.Parameters.Add(paramList[i] as MySqlParameter);
- }
- }
- DataSet ds = new DataSet();
- da.Fill(ds);
- return ds;
- }
- finally
- {
- conn.Close();
- }
- }
- }
Mysql官方.net连接文档:
I'm using Visual Web Developer 2010 Express Edition (and Visual C# 2010 Express) with MySQL. This is what I did:
1 - Download and install MySQL connector(just to get MySql.Data.dll).
2 - VWD 2010 in the Solution Explorer add reference to MySql.Data.dll(usually in c:\program files\MySQL\MySQL Connector Net(your version here)\Assemblies\v4.0 or v2.0)
3 - In the web.config add your string connection:
<connectionStrings>
<add name="MySQLConnString" connectionString="Server=your_MySql_Server_IP;Port=3306;Database=databaseName;Uid=username;Pwd=yourpassword;pooling=false;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
4 - In the code behind type in: "using MySQL.Data.MySqlClient;"
Now you can use it as in this example:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds_temp = GetObjects();
ListBox1.DataSource = ds_temp;
ListBox1.DataTextField = "yourColumn";
ListBox1.DataValueField = "yourColumn";
ListBox1.DataBind();
}
protected static DataSet GetObjects()
{
DataSet ds_temp = Connection_cls.GetDataSetQuery("SELECT yourColumn FROM yourtable", null);
return ds_temp;
}
private static string getHostString()
{
string host = ConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;
return host;
}
public static DataSet GetDataSetQuery(string sql, ArrayList paramList)
{
using (MySqlConnection conn = new MySqlConnection(getHostString()))
{
try
{
conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
da.SelectCommand.Parameters.Clear();
if (paramList != null)
{
for (int i = 0; i < paramList.Count; i++)
{
da.SelectCommand.Parameters.Add(paramList[i] as MySqlParameter);
}
}
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
finally
{
conn.Close();
}
}
}
ASP.NET 连接MySql数据库的更多相关文章
- ASP.NET 连接MySQL数据库 详细步骤
ASP.NET默认的数据库是MS SQL Server,微软的数据库产品.事实上,如果不计成本因素的话,Windows Server + IIS + MS SQL Server + ASP.NET是网 ...
- asp.net连接mysql数据库
方法一:使用MySQL推出的MySQL Connector/Net组件, 该组件是MySQL为ADO.NET访问MySQL数据库设计的.NET专用访问组件.完成该组件后,需要在项目中引用这个组件,也可 ...
- C# asp.net 连接Mysql 数据库
首先添加 引用: using System.Data;using MySql.Data.MySqlClient; 代码: 这里只写了 后台代码 Mysql 连接和sql 连接代码几乎一样 只要把 My ...
- asp.net连接MySQL数据库错误-Out of sync with server
问题 网上说:http://wenda.haosou.com/q/1386389928069965 昨晚这个问题真的费了我不少时间(晚上9到凌晨2点),网上找解决方案,然后一个个尝试,没有成功.准备放 ...
- ASP 连接 MySQL 数据库两种方法
一般都是用myodbc来连接.首先,在系统中安装 Mysql 的ODBC数据库驱动.如安装稳定版本是3.51.下载地址是:http://dev.mysql.com/downloads/connecto ...
- .NET连接MySQL数据库的方法实现
突然对.NET连接MySQL数据库有点兴趣,于是乎网上到处找资料,学习MySQL的安装,MySQL的使用等等等等,终于搞定了! 最终效果就是显示数据库中数据表的数据: 首先,当然要有MySQL数据库啦 ...
- Asp.Net操作MySql数据库增删改查
Asp.Net操作MySql数据库增删改查,话不多说直接步入正题.git源码地址:https://git.oschina.net/gxiaopan/NetMySql.git 1.安装MySQL数据库 ...
- Vs连接Mysql数据库
Vs连接Mysql数据库步骤 1. 首先下载mysql数据库,安装,建库建表 https://www.yiibai.com/mysql/getting-started-with-mysql-store ...
- 【C#】【MySQL】C#连接MySQL数据库(一)代码
C#连接MySQL数据库 准备工作 1.环境安装 安装MySQL For Visual Studio<<点击进入官网下载 第一个要下载安装,第二个下载后将MySQL.data添加到Visu ...
随机推荐
- RPM和yum相关
写在前面: 在这里可以知道rpm和yum的基本用法,找到更新本地yum源.搭建yum源的方法以及yum很琐碎的东西,包括yum源的优先级.用yum来安装或卸载CentOS图形界面包以及保存yum下载的 ...
- unity调用C++ dll文件
首先建立Plugins文件夹,把dll文件放在里面 一一对应,我踩的坑是文件名加了后缀.dll,虽然不知道网上为什么都加了我这加了就报找不到dll文件错误,反正解决啦
- .net core web发布到CentOS汇总
直到今天我的博客终于可以见世人了,中间懒了很长一段时间,什么也没干,也没怎么学习,前段时间也是各种折腾,无心学习.本篇主要汇总下从一开始到现在遇到的问题汇总,作为学习笔记.我的博客就是我的学习笔记,因 ...
- android将drawable下的图片转换成bitmap
将drawable下的图片转换成bitmap 1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xx ...
- Python学习之路day4-函数高级特性、装饰器
一.预备知识 学习装饰器需理解以下预备知识: 函数即变量 函数本质上也是一种变量,函数名即变量名,函数体就变量对应的值:函数体可以作为值赋给其他变量(函数),也可以通过函数名来直接调用函数.调用符号即 ...
- Eclipse插件开发_异常_01_java.lang.RuntimeException: No application id has been found.
一.异常现象 在运行RCP程序时,出现 java.lang.RuntimeException: No application id has been found. at org.eclipse.equ ...
- 201621123014《Java程序设计》第六周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 答: 注1:关键词与内容不求多,但概念之间的联系 ...
- NHibernate常见错误汇总
NHibernateSample.Data.Test.QueryHQLFixture.WhereTest: NHibernate.Hql.Ast.ANTLR.QuerySyntaxException ...
- 在Mac上激活Adobe产品
1.在任意位置下载需要的Adobe软件(推荐官网正版) 网速不好或者不通推荐下载离线安装包: https://helpx.adobe.com/download-install/kb/creative- ...
- enumerate 枚举