在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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; } }
|
请发表评论