1,Download Connector/Net 5.2(odbc) and install
2, add reference "MySQL.Data.dll"
3, add connection in web.config file
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<connectionStrings>
<add name="connStr" connectionString="server=hostName(ip); user id=user_name; password=pass_word; database=dbName;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
4, get the connection string
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
dbConnectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
dbProviderName = ConfigurationManager.ConnectionStrings["ConnStr"].ProviderName;
5, fetch data from database using "using System.Data.Common;"
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Configuration;
/// <summary>
/// Summary description for GenericDataAccess
/// </summary>
public static class GenericDataAccess
{
public static DataTable ExecuteSelectCommand(DbCommand command)
{
DataTable table = null;
try
{
command.Connection.Open();
DbDataReader reader = command.ExecuteReader();
table = new DataTable();
table.Load(reader);
reader.Close();
}
catch (Exception ex)
{
Utilities.LogError(ex);
throw ex;
}
finally {
command.Connection.Close();
}
return table;
}
public static DbCommand CreateCommand()
{
string dataProviderName = BalloonShopConfiguration.DbProviderName;
string connectionString = BalloonShopConfiguration.DbConnectionString;
DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
DbConnection conn = factory.CreateConnection();
conn.ConnectionString = connectionString;
DbCommand comm = conn.CreateCommand();
comm.CommandType = CommandType.StoredProcedure;
return comm;
}
}