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 ...
随机推荐
- WIN7 X64 PASSUAC 源码
// Passuac.cpp : Defines the entry point for the console application. // #include "stdafx.h&quo ...
- 安装,配置webpack.
1.下载node.js 2.在需要用到webpack的项目下打开命令窗口运行npm init生成package.js 3.安装webpack,使用npm install webpack --save- ...
- MATLAB一个数组中另一个数组的值
c = setdiff(a,b) 删掉素组a中数组b的元素 如:
- java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
使用方法测试时出现以下错误 java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.v ...
- count(*) 和count(1) 有区别吗
create table test1 (a varchar2(2),b varchar2(2)); insert into test1 values ('b','c'); insert into te ...
- Windows7+VS2008 下编译Subversion 1.8.3
一.需要的软件包 1.python-2.7.5.msi http://www.python.org/ 2.ActivePerl-5.8.8.822-MSWin32-x86-280952.msi h ...
- 利用Pelican搭建个人博客
博客基于win7系统,python2.7和pelican. 1.安装工具 安装virtualenv pip install virtualenv 下载make,或者make移动至任一目录,并将路径写入 ...
- python--关于if __name__==__main__的解释
在解释之前首先我们看下__name__这个变量存的是什么: #!/usr/bin/env python # -*- coding:utf-8 -*- print(__name__) 结果: __mai ...
- 30 python 并发编程之多线程
一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 官网链接:https://docs.python ...
- Spring通过注解方式实现定时任务
XML配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...