每个人都有自己的知识体系。
Toggle navigation
Home
随笔
C#/.Net
树莓派 / Raspberry
皓月汉化组
Beego
Golang
OxideMod
apache
haproxy
windows
Java
Objective-C
日语/罗马音歌词/日语常识
MongoDB
python
电学
公告
Minecraft服务器-公告
NanoPi
C4D (CINEMA 4D)
生活
推流/m3u8/rtmp/rtsp
Unity3d
ffmpeg
数据结构
区块链
tarui
UnityForPSVita
About Me
Archives
Tags
一个简易的PostgreSql 连接池
2024-06-12 17:45:13
35
0
0
akiragatsu
using Npgsql; using System.Data; public static class SQLRUN { static string connectionString => $"Host={Config.DictCfg["dbServer"]};Port={Config.DictCfg["dbPort"]};Database={Config.DictCfg["dbName"]};Username={Config.DictCfg["dbUser"]};Password={Config.DictCfg["dbPwd"]};"; static Queue<NpgsqlConnection> QueueCon = new Queue<NpgsqlConnection>(); static int PoolLimit = 10; static int GetPoolCount() { return QueueCon.Count; } static NpgsqlConnection GetSqlConnect() { lock (QueueCon) { NpgsqlConnection con; if (QueueCon.Count > 0) { con = QueueCon.Dequeue(); if (con.State < ConnectionState.Open) { con.Dispose(); NpgsqlConnection newcon = new NpgsqlConnection(connectionString); newcon.Open(); con = newcon; } } else { NpgsqlConnection newcon = new NpgsqlConnection(connectionString); newcon.Open(); con = newcon; } return con; } } static void PushConnect(NpgsqlConnection con) { lock (QueueCon) { if (QueueCon.Count < PoolLimit && con.State >= ConnectionState.Open) { QueueCon.Enqueue(con); } else { if (con.State >= ConnectionState.Open) { con.Close(); } con.Dispose(); } } } public static bool QuerySQL(string strSql, out DataTable dt) { dt = null; if (string.IsNullOrEmpty(strSql)) return false; NpgsqlConnection con = GetSqlConnect(); bool bneedPush = true; try { NpgsqlDataAdapter sda = new NpgsqlDataAdapter(strSql, con); DataSet ds = new DataSet(); sda.Fill(ds); PushConnect(con); bneedPush = false; dt = ds.Tables[0]; return true; } catch { if (bneedPush) { PushConnect(con); } return false; } } public static bool ExcuteSQL(string strSql) { NpgsqlConnection con = GetSqlConnect(); bool bneedPush = true; if (string.IsNullOrEmpty(strSql)) return false; string strSqlinsert = strSql; int setnumbert = 0; try { using (NpgsqlCommand SqlCommand = new NpgsqlCommand(strSqlinsert, con)) { setnumbert = SqlCommand.ExecuteNonQuery(); } PushConnect(con); bneedPush = false; return true; } catch (Exception ex) { if (bneedPush) { PushConnect(con); } return false; } } public static DateTime ConvertTimestamptzToDatetime(object obj) { return TimeZoneInfo.ConvertTimeFromUtc((DateTime)obj, TimeZoneInfo.Local); } }
Pre:
每个人都是生活的导演
Next:
皓月云 PS2-怪物猎人联机教程 - 傻瓜版
0
likes
35
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.