在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;