在asp.net中连接MySql、使用MySql.Data.DLL、完全ADO.NET访问模式、操作方便如同SQL Server
MySql.Data.dll下载地址:http://dev.mysql.com/downloads/
http://free5.ys168.com/?dotnetly
using MySql.Data.MySqlClient;
public partial class Msg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string conn = "Data Source=127.0.0.1;User ID=root;Password=123;DataBase=NewDB;Charset=gb2312;";
MySqlConnection con = new MySqlConnection(conn);
con.Open();
MySqlCommand cmd = new MySqlCommand("select * from book",con);
MySqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
dr.Close();
con.Close();
}
}
需要说明的是:MySql中的参数化不同于SqlServer
Sqlserver的参数化写法:
myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
myCommand.Parameters["@Address"].Value = address.Value;
MySql的参数化写法:
cmd.Parameters.Add(new MySqlParameter("?p_bname", MySqlDbType.VarChar, 50));
cmd.Parameters["?p_bname"].Value = book.Text;
MySql.Data.dll下载地址:http://dev.mysql.com/downloads/
http://free5.ys168.com/?dotnetly
using MySql.Data.MySqlClient;
public partial class Msg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string conn = "Data Source=127.0.0.1;User ID=root;Password=123;DataBase=NewDB;Charset=gb2312;";
MySqlConnection con = new MySqlConnection(conn);
con.Open();
MySqlCommand cmd = new MySqlCommand("select * from book",con);
MySqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
dr.Close();
con.Close();
}
}
需要说明的是:MySql中的参数化不同于SqlServer
Sqlserver的参数化写法:
myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
myCommand.Parameters["@Address"].Value = address.Value;
MySql的参数化写法:
cmd.Parameters.Add(new MySqlParameter("?p_bname", MySqlDbType.VarChar, 50));
cmd.Parameters["?p_bname"].Value = book.Text;
本文转自chy710博客园博客,原文链接:http://www.cnblogs.com/chy710/archive/2006/09/05/495000.html,如需转载请自行联系原作者