1,Download Connector/Net 5.2(odbc) and install

2,  add reference "MySQL.Data.dll"

3,  add connection in web.config file

 


        <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

].ConnectionString;
dbProviderName = ConfigurationManager.ConnectionStrings["ConnStr"].ProviderName;

 

5, fetch data from database using "using System.Data.Common;"

 

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

 

 

 

相关文章: