123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Data;
- using System.Data.SqlClient;
- using Microsoft.Extensions.Configuration;
- using System.IO;
- namespace YDSP.Core
- {
- /// <summary>
- /// 帮助类
- /// </summary>
- public class YDSPHepler
- {
- private static readonly object obj = new object();
- private static YDSPHepler instance = null;
- public IConfigurationRoot Config { get; }
- private YDSPHepler()
- {
- var builder = new ConfigurationBuilder()
- .SetBasePath(Directory.GetCurrentDirectory())
- .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
- Config = builder.Build();
- }
- public static YDSPHepler GetInstance()
- {
- if (instance == null)
- {
- lock (obj)
- {
- if (instance == null)
- {
- instance = new YDSPHepler();
- }
- }
- }
- return instance;
- }
- public static string GetConfig(string name)
- {
- return GetInstance().Config.GetSection(name).Value;
- }
- private static string CONNECTION_STRING = YDSPHepler.GetConfig("ConnectionStrings:DefaultConnection");
- private static string _conStr = CONNECTION_STRING;
- private static SqlConnection _ion;
- public static SqlConnection _Ion
- {
- get
- {
- if (_ion == null)
- _ion = new SqlConnection(_conStr);
- if (_ion.State != ConnectionState.Open)
- _ion.Open();
- return _ion;
- }
- }
- /// <summary>
- /// 执行数据库增删改
- /// </summary>
- /// <param name="sql">执行的语句</param>
- /// <returns></returns>
- public static int ExecuteNonQuery(string sql)
- {
- SqlCommand and = _Ion.CreateCommand();
- and.CommandText = sql;
- try
- {
- return and.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return 0;
- }
- finally
- {
- and.Connection.Close();
- }
- }
- /// <summary>
- /// 负责查询数据库数据
- /// </summary>
- /// <param name="sql"></param>
- /// <returns></returns>
- public static DataTable GetData(string sql)
- {
- SqlDataAdapter adapter = new SqlDataAdapter(sql, _conStr);
- DataSet set = new DataSet();
- adapter.Fill(set);
- return set.Tables[0];
- }
- public static DataSet GetSet(string sql)
- {
- SqlDataAdapter adapter = new SqlDataAdapter(sql, _conStr);
- DataSet set = new DataSet();
- adapter.Fill(set);
- return set;
- }
- }
- }
|