asp.net访问MySQL数据库

数据库链接字符串:

<span style="font-size:14px;"><configuration>
  <connectionStrings> 
    <add name="MySqlConnectionString" connectionString="server=127.0.0.1:4050; Initial Catalog=Products;User Id=root;Password=root;" />
  </connectionStrings>
</configuration></span>


数据访问类:

<span style="font-size:18px;">using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    /// <summary>
    /// 访问数据库底层
    /// </summary>
    public sealed class SQLHelper
    {
        private readonly string MySqlConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString;

        private static SQLHelper helper;

        private SQLHelper() { }

        public static SQLHelper getInstance() 
        {
            if (helper != null) 
            {
                helper = new SQLHelper();
            }
            return helper;
        }

        /// <summary>
        /// 返回影响的行数
        /// </summary>
        /// <param name="sql">sql 语句</param>
        /// <param name="parameter">参数化</param> 
        public int ExecuteNonq(string sql, SqlParameter[] parameter)
        {
            using (MySqlConnection conn = new MySqlConnection(MySqlConnStr)) 
            {
                conn.Open();
                using (MySqlCommand cmd = conn.CreateCommand()) 
                {
                    try
                    {
                        cmd.CommandText = sql;
                        if (parameter != null)
                        {
                            cmd.Parameters.AddRange(parameter);
                        }
                        return cmd.ExecuteNonQuery();
                    }
                    finally 
                    {
                        cmd.Dispose();
                        conn.Close();
                    } 
                }
            }
        }

        /// <summary>
        /// 返回首行首列
        /// </summary>
        /// <param name="sql">sql 语句</param>
        /// <param name="parameter">参数化</param> 
        public object ExecuteScalar(string sql, SqlParameter[] parameter)
        {
            using (MySqlConnection conn = new MySqlConnection(MySqlConnStr))
            {
                conn.Open();
                using (MySqlCommand cmd = conn.CreateCommand())
                {
                    try
                    {
                        cmd.CommandText = sql;
                        if (parameter != null)
                        {
                            cmd.Parameters.AddRange(parameter);
                        }
                        return cmd.ExecuteScalar();
                    }
                    finally 
                    {
                        cmd.Dispose();
                        conn.Close();
                    }
                }
            }
        }

        /// <summary>
        /// 返回一个DataTable
        /// </summary>
        /// <param name="sql">sql 语句</param>
        /// <param name="parameter">参数化</param> 
        public DataTable ExecuteDataTable(string sql, SqlParameter[] parameter)
        {
            using (MySqlConnection conn = new MySqlConnection(MySqlConnStr))
            {
                conn.Open();
                using (MySqlCommand cmd = conn.CreateCommand())
                {
                    MySqlDataAdapter adapter = null;
                    DataSet ds = null;
                    try
                    {
                        cmd.CommandText = sql;
                        if (parameter != null)
                        {
                            cmd.Parameters.AddRange(parameter);
                        }
                        adapter = new MySqlDataAdapter(cmd);
                        ds = new DataSet();
                        adapter.Fill(ds);
                        return ds.Tables[0];
                    }
                    finally 
                    {
                        adapter.Dispose();
                        ds.Dispose();
                        cmd.Dispose();
                        conn.Close();
                    }
                }
            }
        }
        
        /// <summary>
        /// 返回一个结果集
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameter">参数化</param> 
        public DataSet ExecuteDataSet(string sql, SqlParameter[] parameter)
        {
            using (MySqlConnection conn = new MySqlConnection(MySqlConnStr))
            {
                conn.Open();
                using (MySqlCommand cmd = conn.CreateCommand())
                {
                    MySqlDataAdapter adapter = null;
                    DataSet ds = null;
                    try
                    {
                        cmd.CommandText = sql;
                        if (parameter != null)
                        {
                            cmd.Parameters.AddRange(parameter);
                        }
                        adapter = new MySqlDataAdapter(cmd);
                        ds = new DataSet();
                        adapter.Fill(ds);
                        return ds;
                    }
                    finally 
                    {
                        adapter.Dispose();
                        ds.Dispose();
                        cmd.Dispose();
                        conn.Close();
                    }
                }
            }
        }
    }
}</span>


你可能感兴趣的:(设计模式,mysql,asp.net)