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 ...
随机推荐
- spring mvc 官方下载
1.进入https://spring.io 2.选择projects选项卡 3.点击spring frawewoek 4.选择右中方的Reference 5.选择2.3章节 6.点击 Distribu ...
- Java编程思想 Random(47)
Random类包含两个构造方法,下面依次进行介绍:1. public Random()该构造方法使用一个和当前系统时间对应的相对时间有关的数字作为种子数,然后使用这个种子数构造Random对象.2. ...
- Navicat 导入Excel与增加主键
1.当你需要导入某Excel文件时,你必须把这个lxsl文件用Excel先打开(与其他软件的导入有点不太,其他会报错已占用之类的) 2.设置主键 当你打开你导入的Excel文件时,会显示无主键,需要你 ...
- Spring Boot入门——tomcat配置
1.通过配置文件配置 server.port = 8080 2.通过程序配置 import org.springframework.boot.SpringApplication; import org ...
- BZOJ 2752 [HAOI2012]高速公路(road):线段树【维护区间内子串和】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2752 题意: 有一个初始全为0的,长度为n的序列a. 有两种操作: (1)C l r v: ...
- hibernate学习(2)
1 实体类编写规则 2 hibernate主键生成策略 3实体类操作 (1)crud操作 (2)实体对象状态 4 hibernate的一级缓存 5 hibernate事务操作 (1)事务代码规则写法 ...
- 使用hibernate读取hibernate.cfg.xml文件时碰到这个错误org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/HibernateTest/src/hibernate.cfg.xml]
我的问题在于把hibernate.cfg.xml文件放置在某个包中了,hibernate.cfg.xml文件需要放置在src目录下.
- WebAPI序列化后的时间中含有“T”的解决方法
问题: { "name": "abc", "time": "2015-02-10T15:18:21.7046433+08:00&q ...
- 5.6 WebDriver API实例讲解(31-40)
31.判断页面元素是否存在 public static void testElementExist(){ driver.get("http://www.sogou.com"); t ...
- ROC曲线是通过样本点分类概率画出的 例如某一个sample预测为1概率为0.6 预测为0概率0.4这样画出来,此外如果曲线不是特别平滑的话,那么很可能存在过拟合的情况
ROC和AUC介绍以及如何计算AUC from:http://alexkong.net/2013/06/introduction-to-auc-and-roc/ ROC(Receiver Operat ...