ASP.NET 连接MySql数据库
2016-02-27 16:10
513 查看
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:
[csharp] view plain copy
<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 并且使用的例子
[csharp] view plain copy
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();
}
}
}
以下连接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:
[csharp] view plain copy
<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 并且使用的例子
[csharp] view plain copy
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.NET5》Web API Controller Class学习
- 【ASP.Net】HtmlTextWriter学习笔记
- ASP.NET路由模型解析
- 无法将类型“ASP.login_aspx”转换为“System.Web.UI.WebControls.Login”
- cmd删除Temporary ASP.NET Files方法
- ASP.NET 页面之间传递值的几种方式
- 【Web API系列教程】2.3 — ASP.NET Web API 2中的属性路由
- 【Web API系列教程】2.2 — ASP.NET Web API中的路由和动作选择机制
- 【Web API系列教程】2.1 — ASP.NET Web API中的路由机制
- NU1002 The dependency Microsoft.AspNet.Mvc 5.2.3 in project dotnetstarter does not support framework
- CS0234 The type or namespace name 'Mvc' does not exist in the namespace 'Microsoft.AspNet' (are you
- 尝试在mac上用dotnet cli运行asp.net core示例程序
- 关于aspx 页面生成html 源码顶部空行不得不说的事儿
- asp.net IIS7 503错误
- 实现Asp.Net Mvc4多级Views目录
- aspv6_index
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- ASP.NET MVC RouteExistingFiles
- asp.net程序中如何使用皮肤更换的小功能
- ASP.NET内置对象的总结