• MySqlHelper


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    
    namespace MySqlHelper
    {
        public class SQLHelper
        {
            private static readonly string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
            /// <summary>
            /// 连接数据库
            /// </summary>
            /// <returns></returns>
            public static SqlConnection CreateConnection()
            {
                SqlConnection conn = new SqlConnection(connstr);
                conn.Open();
                return conn;
            }
            /// <summary>
            /// 返回受影响行数
            /// </summary>
            /// <param name="conn"></param>
            /// <param name="sql"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public static int ExecuteNonQuery(SqlConnection conn, string sql, params SqlParameter[] parameters)
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
            /// <summary>
            /// 按现有连接,返回受影响行数
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
            {
                using (SqlConnection conn = CreateConnection())
                {
                    return ExecuteNonQuery(conn,sql,parameters);
                }
            }
            /// <summary>
            /// 返回第一行第一条数据
            /// </summary>
            /// <param name="conn"></param>
            /// <param name="sql"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public static object ExecuteScalar(SqlConnection conn, string sql, params SqlParameter[] parameters)
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
            /// <summary>
            /// 按现有连接,返回第一行第一条数据
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
            {
                using (SqlConnection conn = CreateConnection())
                {
                    return ExecuteScalar(conn,sql,parameters);
                }
            }
            /// <summary>
            /// 返回多条数据
            /// </summary>
            /// <param name="conn"></param>
            /// <param name="sql"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public static DataTable ExecuteReader(SqlConnection conn, string sql, params SqlParameter[] parameters)
            {
                DataTable table = new DataTable();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        table.Load(reader);
                    }
                }
                return table;
            }
            /// <summary>
            /// 按现有连接,返回多条数据
            /// </summary>
            /// <param name="sql"></param>
            /// <param name="parameters"></param>
            /// <returns></returns>
            public static DataTable ExecuteReader(string sql, params SqlParameter[] parameters)
            {
                using (SqlConnection conn = CreateConnection())
                {
                    return ExecuteReader(conn,sql,parameters);
                }
            }
    
        }
    }

    Web.config配置文件:

    <configuration>
        <system.web>
          <compilation debug="true" targetFramework="4.5" />
          <httpRuntime targetFramework="4.5" />
        </system.web>
      <connectionStrings>
        <add name="connstr" connectionString="server=PC-20140729GKIU;user id=sa;password=update123;database=sy"/>
      </connectionStrings>
    </configuration>
  • 相关阅读:
    QT自定义信号和槽
    C++中深入理解dynamic_cast
    C++中rapidxml用法
    VS2015 创建C++动态库及使用
    C++ 已知两个时间(年月日)求日期差
    Electron 打开开发者工具 devtools
    NSIS安装或卸载时检查程序是否正在运行
    sqlite3 读写锁
    CEF 远程调试
    linux缩减分区空间,用以安装win系统
  • 原文地址:https://www.cnblogs.com/genesis/p/4671441.html
Copyright © 2020-2023  润新知