asp.net通过web.config连接mysql数据库
2008-09-18 16:37
435 查看
1,Download Connector/Net 5.2(odbc) and install
2, add reference "MySQL.Data.dll"
3, add connection in web.config file
Code
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;
}
}
2, add reference "MySQL.Data.dll"
3, add connection in web.config file
Code
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;
}
}
相关文章推荐
- ASP.NET通过配置Web.config文件提高手动绑定数据库效率的一个简单方法,不用每次手动连接数据库地址,或者数据库地址变更时只改一处即可。
- ASP.NET 2.0 web.config数据库连接设置与读取
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- ASP.NET连接ACCESS、SqlServer以及Oracle数据库的web.config内路径最优写法
- asp.net数据库连接web.config配置
- Asp.net中Web.config连接字符串及配置数据库sqlserver
- Asp.Net配置Web.config连接SqlServer数据库
- asp.net 下web.config中配置数据库连接
- Asp.Net连接ACCESS数据库相对路径问题,web.config中如何使用相对路径及在类文件中调用这个相对路径
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- 【ASP.NET】--web.config中的连接字符串
- ASP.NET WEB.CONFIG 配置数据库连接
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- ASP.NET数据库连接字符串写在config.web配置文件中
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- Asp.Net配置Web.config连接数据库SqlServer
- (译)利用ASP.NET加密和解密Web.config中连接字符串
- 利用ASP.NET加密和解密Web.config中连接字符串
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- asp.net 获取web.config中的连接字符串