您的位置:首页 > 编程语言 > ASP

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: