}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LenovoDataCrawlService.DBAccessor
{
public enum DbProviderType
{
SqlServer,
MySql,
SQLite,
Oracle,
ODBC,
OleDb,
Firebird,
PostgreSql,
DB2,
Informix,
SqlServerCe
}
}
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
namespace LenovoDataCrawlService.DBAccessor
{
public class ProviderFactory
{
private static ConcurrentDictionary<DbProviderType, string> providerInvariantNames = new ConcurrentDictionary<DbProviderType, string>();
private static ConcurrentDictionary<DbProviderType, DbProviderFactory> providerFactoies = new ConcurrentDictionary<DbProviderType, DbProviderFactory>();
static ProviderFactory()
{
providerInvariantNames.TryAdd(DbProviderType.SqlServer, "System.Data.SqlClient");
providerInvariantNames.TryAdd(DbProviderType.OleDb, "System.Data.OleDb");
providerInvariantNames.TryAdd(DbProviderType.ODBC, "System.Data.ODBC");
providerInvariantNames.TryAdd(DbProviderType.Oracle, "Oracle.DataAccess.Client");
providerInvariantNames.TryAdd(DbProviderType.MySql, "MySql.Data.MySqlClient");
providerInvariantNames.TryAdd(DbProviderType.SQLite, "System.Data.SQLite");
providerInvariantNames.TryAdd(DbProviderType.Firebird, "FirebirdSql.Data.Firebird");
providerInvariantNames.TryAdd(DbProviderType.PostgreSql, "Npgsql");
providerInvariantNames.TryAdd(DbProviderType.DB2, "IBM.Data.DB2.iSeries");
providerInvariantNames.TryAdd(DbProviderType.Informix, "IBM.Data.Informix");
providerInvariantNames.TryAdd(DbProviderType.SqlServerCe, "System.Data.SqlServerCe");
}
public static string GetProviderInvariantName(DbProviderType providerType)
{
return providerInvariantNames[providerType];
}
public static DbProviderFactory GetDbProviderFactory(DbProviderType providerType)
{
if (!providerFactoies.ContainsKey(providerType))
{
providerFactoies.TryAdd(providerType, ImportDbProviderFactory(providerType));
}
return providerFactoies[providerType];
}
private static DbProviderFactory ImportDbProviderFactory(DbProviderType providerType)
{
string providerName = providerInvariantNames[providerType];
DbProviderFactory factory = null;
try
{
factory = DbProviderFactories.GetFactory(providerName);
}
catch (ArgumentException e)
{
factory = null;
}
return factory;
}
}
}
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Text;
namespace LenovoDataCrawlService
{
public class DbDataReaderReflection
{
public static List<T> Convert<T>(DbDataReader dataReader) where T : new()
{
List<T> result = new List<T>();
if (dataReader != null)
{
using (dataReader)
{
while (dataReader.Read())
{
T data = new T();
Type type = data.GetType();
PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var item in propertyInfos)
{
var obj = new object();
try
{
obj = dataReader[item.Name];
}
catch (Exception)
{
continue;
}
if (obj == DBNull.Value || obj == null)
{
continue;
}
if (item.CanWrite)
{
item.SetValue(data, obj, null);
}
}
result.Add(data);
}
}
}
return result;
}
}
}
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<clear/>
<add name="MysqlConnectionString" connectionString="Server=*;Port=*;Database=*;Uid=root;Pwd=*;MinimumPoolSize=10;maximumpoolsize=100;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>