1、首先上mysql网站下驱动:http://www.mysql.com/products/connector/
2、安装下载的安装包
3、我们在visual studio里创建一个web application,然后引入x:/xxxx/mysql/mysql connector net 6.3.6/assemblies下的v2.0或v4.0里的dll文件。
在mysqlhelper里面加入:
using mysql.data.mysqlclient;
在web.config里面加入mysql数据库的连接字符串:
4、封装好的mysqlhelper类
using system;using system.collections.generic;using system.data;using system.collections;using mysql.data.mysqlclient;using system.configuration;namespace dbutility{/// /// the sqlhelper class is intended to encapsulate high performance,/// scalable best practices for common uses of sqlclient./// public abstract class mysqlhelper{ // read the connection strings from the configuration file public static readonly string connectionstringlocaltransaction = configurationmanager.connectionstrings[oraconnstring1].connectionstring; public static readonly string connectionstringinventorydistributedtransaction = configurationmanager.connectionstrings[oraconnstring2].connectionstring; public static readonly string connectionstringorderdistributedtransaction = configurationmanager.connectionstrings[oraconnstring3].connectionstring; public static readonly string connectionstringprofile = configurationmanager.connectionstrings[oraprofileconnstring].connectionstring; public static readonly string connectionstringmembership = configurationmanager.connectionstrings[oramembershipconnstring].connectionstring; //create a hashtable for the parameter cached private static hashtable parmcache = hashtable.synchronized(new hashtable()); /// /// execute a database query which does not include a select /// /// connection string to database /// command type either stored procedure or sql /// acutall sql command /// parameters to bind to the command /// public static int executenonquery(string connectionstring, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { // create a new mysql command mysqlcommand cmd = new mysqlcommand(); //create a connection using (mysqlconnection connection = new mysqlconnection(connectionstring)) { //prepare the command preparecommand(cmd, connection, null, cmdtype, cmdtext, commandparameters); //execute the command int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } } /// /// execute an mysqlcommand (that returns no resultset) against an existing database transaction /// using the provided parameters. /// /// /// e.g.: ///int result = executenonquery(trans, commandtype.storedprocedure, publishorders, new mysqlparameter(:prodid, 24)); /// /// an existing database transaction /// the commandtype (stored procedure, text, etc.) /// the stored procedure name or pl/sql command /// an array of mysqlparamters used to execute the command /// an int representing the number of rows affected by the command public static int executenonquery(mysqltransaction trans, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); preparecommand(cmd, trans.connection, trans, cmdtype, cmdtext, commandparameters); int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } /// /// execute an mysqlcommand (that returns no resultset) against an existing database connection /// using the provided parameters. /// /// /// e.g.: ///int result = executenonquery(connstring, commandtype.storedprocedure, publishorders, new mysqlparameter(:prodid, 24)); /// /// an existing database connection /// the commandtype (stored procedure, text, etc.) /// the stored procedure name or pl/sql command /// an array of mysqlparamters used to execute the command /// an int representing the number of rows affected by the command public static int executenonquery(mysqlconnection connection, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); preparecommand(cmd, connection, null, cmdtype, cmdtext, commandparameters); int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } /// /// execute a select query that will return a result set /// /// connection string //// the commandtype (stored procedure, text, etc.) /// the stored procedure name or pl/sql command /// an array of mysqlparamters used to execute the command /// public static mysqldatareader executereader(string connectionstring, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { //create the command and connection mysqlcommand cmd = new mysqlcommand(); mysqlconnection conn = new mysqlconnection(connectionstring); try { //prepare the command to execute preparecommand(cmd, conn, null, cmdtype, cmdtext, commandparameters); //execute the query, stating that the connection should close when the resulting datareader has been read mysqldatareader rdr = cmd.executereader(commandbehavior.closeconnection); cmd.parameters.clear(); return rdr; } catch { //if an error occurs close the connection as the reader will not be used and we expect it to close the connection conn.close(); throw; } } /// /// execute an mysqlcommand that returns the first column of the first record against the database specified in the connection string /// using the provided parameters. /// /// /// e.g.: ///object obj = executescalar(connstring, commandtype.storedprocedure, publishorders, new mysqlparameter(:prodid, 24)); /// /// a valid connection string for a sqlconnection /// the commandtype (stored procedure, text, etc.) /// the stored procedure name or pl/sql command /// an array of mysqlparamters used to execute the command /// an object that should be converted to the expected type using convert.to{type} public static object executescalar(string connectionstring, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); using (mysqlconnection conn = new mysqlconnection(connectionstring)) { preparecommand(cmd, conn, null, cmdtype, cmdtext, commandparameters); object val = cmd.executescalar(); cmd.parameters.clear(); return val; } } /// /// execute a mysqlcommand (that returns a 1x1 resultset) against the specified sqltransaction /// using the provided parameters. /// /// a valid sqltransaction /// the commandtype (stored procedure, text, etc.) /// the stored procedure name or pl/sql command /// an array of mysqlparamters used to execute the command /// an object containing the value in the 1x1 resultset generated by the command public static object executescalar(mysqltransaction transaction, commandtype commandtype, string commandtext, params mysqlparameter[] commandparameters) { if (transaction == null) throw new argumentnullexception(transaction); if (transaction != null && transaction.connection == null) throw new argumentexception(the transaction was rollbacked or commited, please provide an open transaction., transaction); // create a command and prepare it for execution mysqlcommand cmd = new mysqlcommand(); preparecommand(cmd, transaction.connection, transaction, commandtype, commandtext, commandparameters); // execute the command & return the results object retval = cmd.executescalar(); // detach the sqlparameters from the command object, so they can be used again cmd.parameters.clear(); return retval; } /// /// execute an mysqlcommand that returns the first column of the first record against an existing database connection /// using the provided parameters. /// /// /// e.g.: ///object obj = executescalar(conn, commandtype.storedprocedure, publishorders, new mysqlparameter(:prodid, 24)); /// /// an existing database connection /// the commandtype (stored procedure, text, etc.) /// the stored procedure name or pl/sql command /// an array of mysqlparamters used to execute the command /// an object that should be converted to the expected type using convert.to{type} public static object executescalar(mysqlconnection connectionstring, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); preparecommand(cmd, connectionstring, null, cmdtype, cmdtext, commandparameters); object val = cmd.executescalar(); cmd.parameters.clear(); return val; } /// /// add a set of parameters to the cached /// /// key value to look up the parameters /// actual parameters to cached public static void cacheparameters(string cachekey, params mysqlparameter[] commandparameters) { parmcache[cachekey] = commandparameters; } /// /// fetch parameters from the cache /// /// key to look up the parameters /// public static mysqlparameter[] getcachedparameters(string cachekey) { mysqlparameter[] cachedparms = (mysqlparameter[])parmcache[cachekey]; if (cachedparms == null) return null; // if the parameters are in the cache mysqlparameter[] clonedparms = new mysqlparameter[cachedparms.length]; // return a copy of the parameters for (int i = 0, j = cachedparms.length; i /// internal function to prepare a command for execution by the database /// /// existing command object /// database connection object /// optional transaction object /// command type, e.g. stored procedure /// command test /// parameters for the command private static void preparecommand(mysqlcommand cmd, mysqlconnection conn, mysqltransaction trans, commandtype cmdtype, string cmdtext, mysqlparameter[] commandparameters) { //open the connection if required if (conn.state != connectionstate.open) conn.open(); //set up the command cmd.connection = conn; cmd.commandtext = cmdtext; cmd.commandtype = cmdtype; //bind it to the transaction if it exists if (trans != null) cmd.transaction = trans; // bind the parameters passed in if (commandparameters != null) { foreach (mysqlparameter parm in commandparameters) cmd.parameters.add(parm); } } /// /// converter to use boolean data type with mysql /// /// value to convert /// public static string mysqlbit(bool value) { if (value) return y; else return n; } /// /// converter to use boolean data type with mysql /// /// value to convert /// public static bool mysqlbool(string value) { if (value.equals(y)) return true; else return false; }}}