asp.net(c#)如何连接和访问mysql

高手们,请教一下,asp.net(c#)如何连接和访问mysql
最新回答
沉鱼落雁

2024-04-22 01:34:32

mysql有个dll,引入项目里面就可以创建mysql连接对象。

MySql.Data.dll//Mysql驱动。去网上下载。
给你个MySQL的DBHelper类
MySqlDBHelper.cs
------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace MySQLDB
{
public class MySqlDBHelper
{
//引导数据库连接数据库调用Web.Config文件
private static MySqlConnection connection;
//创建连接
public static MySqlConnection Connection
{
get
{
MySqlConnection myConn = new MySqlConnection("server=localhost;user id=root;password=root;database=vn_site;CharSet=utf8;");
string connectionString = myConn.ConnectionString;
if (connection == null)
{
connection = new MySqlConnection(connectionString);
//打开连接
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
//(无参)返回执行的行数(删除修改更新)
public static int ExecuteCommand(string safeSql)
{
MySqlCommand cmd = new MySqlCommand(safeSql, Connection);
int result = cmd.ExecuteNonQuery();
return result;
}
//(有参)
public static int ExecuteCommand(string sql, params MySqlParameter[] values)
{
MySqlCommand cmd = new MySqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
//(无参)返回第一行第一列(删除修改更新)
public static int GetScalar(string safeSql)
{
MySqlCommand cmd = new MySqlCommand(safeSql, Connection);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
//(有参)
public static int GetScalar(string sql, params MySqlParameter[] values)
{
MySqlCommand cmd = new MySqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
//返回一个DataReader(查询)
public static MySqlDataReader GetReader(string safeSql)
{
MySqlCommand cmd = new MySqlCommand(safeSql, Connection);
MySqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static MySqlDataReader GetReader(string sql, params MySqlParameter[] values)
{
MySqlCommand cmd = new MySqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
MySqlDataReader reader = cmd.ExecuteReader();
return reader;
}

//返回一个DataTable
public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
MySqlCommand cmd = new MySqlCommand(safeSql, Connection);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
public static DataTable GetDataSet(string sql, params MySqlParameter[] values)
{
DataSet ds = new DataSet();
MySqlCommand cmd = new MySqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}
枕边徒留芳香

2024-04-22 11:13:51

这个百度文库很详细
http://wenku.baidu.com/view/85dc4c72f242336c1eb95ef0.html
无穷鸡腿

2024-04-22 04:18:10

动软代码生成器,那你们有个访问mysql的数据库方法类,可以参考看看。。
花开丶终会败

2024-04-22 13:38:19

简单点说,,,下载链接的dll文件放入asp.net的bin文件夹 然后其他就和链接mssql没什么区别了 只是把sqlconnection改为mysqlconnection等等