ASP.NET 连接MySql 数据库

转载自:http://www.cnblogs.com/chy710/archive/2006/09/05/495000.aspx



1.数据库MySql for .Net 驱动下载:

http://dev.mysql.com/downloads/connector/


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;

namespace MyWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string conn = "Data Source=127.0.0.1;User ID=root;Password=QUSTDJX;DataBase=ElectClass;Charset=gb2312;";
            MySqlConnection con = new MySqlConnection(conn);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("select * from Student", con);
            MySqlDataReader dr = cmd.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
            dr.Close();
            con.Close();
        }

        
    }
}


using MySql.Data 需要手动加入工程中的“引用”中去。

需要说明的是: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;


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码实现asp.net连接MySQL数据库: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using MySql.Data.MySqlClient; public class MySQLDBHelper { private static string connectionString = "server=localhost;user id=root;password=123456;database=test;Charset=utf8;"; public static DataTable ExecuteDataTable(string commandText, CommandType commandType, params MySqlParameter[] parameters) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand command = new MySqlCommand(commandText, connection)) { command.CommandType = commandType; if (parameters != null) { command.Parameters.AddRange(parameters); } MySqlDataAdapter adapter = new MySqlDataAdapter(command); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); return dataTable; } } } public static int ExecuteNonQuery(string commandText, CommandType commandType, params MySqlParameter[] parameters) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand command = new MySqlCommand(commandText, connection)) { command.CommandType = commandType; if (parameters != null) { command.Parameters.AddRange(parameters); } connection.Open(); int result = command.ExecuteNonQuery(); return result; } } } } ``` 其中,MySQLDBHelper是一个帮助类,提供了两个静态方法,一个用于执行SELECT语句并返回DataTable,另一个用于执行INSERT/UPDATE/DELETE等操作并返回受影响的行数。需要将connectionString变量替换为自己的MySQL连接字符串。使用示例: ``` MySqlParameter[] parameters = new MySqlParameter[] { new MySqlParameter("@name", "张三"), new MySqlParameter("@age", 20) }; string sql = "INSERT INTO student (name, age) VALUES (@name, @age)"; int result = MySQLDBHelper.ExecuteNonQuery(sql, CommandType.Text, parameters); if (result > 0) { Response.Write("添加成功!"); } else { Response.Write("添加失败!"); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值