Sqlconnection,sqlcommand,和List和Arraylist的区别

本文对比了SQL操作如Sqlconnection、SqlCommand与数据集合类如Array、ArrayList的特点与区别,并介绍了Server.Transfer与Response.Redirect两种页面跳转方式的工作原理。
 

Sqlconnection:数据库连接,配置连接字符串等;

 

SqlCommand:执行sql语句或存储过程,直接翻译成sql命令,

 

SqlDataReader:(sql数据配适器)

 

DataTable:表示内存中数据的一个表

 

List是一个接口,而ArrayList是一个类

 

Array类型的变量在声明的同时必须进行实例化(至少得初始化数组的大小),而ArrayList可以只是声明。

 

Array只能存储同构的对象而ArrayList可以存储异构的对象

 

Array是始终是连续存放的,而ArrayList的存放不一定连续

 

Array对象的初始化必须指定大小,且创建后的数组大小是固定的,而ArrayList的大小可以动态指定,其大小可以在初始化时指定,也可以不指定,也可以说该对象的控件可以人以增加。

 

Array不能够随意添加和删除其中的项,而ArrayList可以在任意位置处插入和删除项。

 

 

Server.Transfor()方法把执行流程从当前的ASPX文件转到同一服务器上的另一个ASPX页面。调用Server.Transfor时,当前的ASPX页面终于执行,执行流程流入另一个ASPX页面,但新的ASPX页面仍使用前一ASPX页面创建的应答流。

 

如果使用Server.Transfor方法实现页面之间的导航,浏览器中的URL不会改变,因为重定项完全在服务器端进行,浏览器根本不知道服务器已经执行了一次页面变换。

 

Response.Redirect则是客户端第一次请求返回状态代码302及新URL,客户端再次请求新URL,服务器再返回新页面,比Srever.Transfor多一次应答。

 

using System; using System.Collections; using System.Data; using System.Data.SqlClient; namespace RxjhServer.DbClss { public class DBA { public static void serlog(string txt) { string sqlJl = World.SqlJl; if (sqlJl.Length == 0) { return; } string text = sqlJl; char[] separator = new char[1] { '|' }; string[] array = text.Split(separator); string[] array2 = array; string[] array3 = array2; foreach (string text2 in array3) { if (txt.ToLower().IndexOf(text2.ToLower()) != -1) { Form1.WriteLine(99, txt); } } } public static void serlog(string txt, SqlParameter[] prams) { string sqlJl = World.SqlJl; if (sqlJl.Length == 0) { return; } string[] array = sqlJl.Split('|'); for (int i = 0; i < array.Length; i++) { if (txt.ToLower().IndexOf(array[i].ToLower()) != -1) { Form1.WriteLine(99, txt); } } for (int j = 0; j < array.Length; j++) { foreach (SqlParameter sqlParameter in prams) { if (sqlParameter.SqlValue.ToString().ToLower().IndexOf(array[j].ToLower()) != -1) { Form1.WriteLine(99, txt + " " + sqlParameter.SqlValue.ToString()); } } } } public static void Setlog(string txt, SqlParameter[] prams, Exception ex) { Form1.WriteLine(100, "-----------DBA数据层_错误-----------"); Form1.WriteLine(100, txt); if (prams != null) { foreach (SqlParameter sqlParameter in prams) { Form1.WriteLine(100, sqlParameter.SqlValue.ToString()); } } Form1.WriteLine(100, ex.Message); } public static string getstrConnection(string db) { try { if (db == null) { db = "GameServer"; } World.sql = (World.Db.TryGetValue(db, out var value) ? value.SqlConnect : null); DbClass value2; return World.Db.TryGetValue(db, out value2) ? value2.SqlConnect : null; } catch { return null; } } public static int ExeSqlCommand(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, prams, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, SqlParameter[] prams, string server) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, prams, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, string server) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, ref Exception exception, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch (Exception ex) { Exception ex2 = (exception = ex); return -1; } int result = sqlCommand2.ExecuteNonQuery(); sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static DataTable GetDBToDataTable(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand5 = (sqlDataAdapter.SelectCommand = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams)); SqlCommand sqlCommand2 = sqlCommand5; SqlCommand sqlCommand3 = sqlCommand2; using (sqlCommand3) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, prams, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand, SqlParameter[] prams, string server) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand5 = (sqlDataAdapter.SelectCommand = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams)); SqlCommand sqlCommand2 = sqlCommand5; SqlCommand sqlCommand3 = sqlCommand2; using (sqlCommand3) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, prams, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand5 = (sqlDataAdapter.SelectCommand = new SqlCommand(sqlCommand, sqlConnection)); SqlCommand sqlCommand2 = sqlCommand5; SqlCommand sqlCommand3 = sqlCommand2; using (sqlCommand3) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, null, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand, string server) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand5 = (sqlDataAdapter.SelectCommand = new SqlCommand(sqlCommand, sqlConnection)); SqlCommand sqlCommand2 = sqlCommand5; SqlCommand sqlCommand3 = sqlCommand2; using (sqlCommand3) { try { sqlConnection.Open(); } catch { return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataRowCollection GetDBValue(string sqlCommand, string db) { return GetDBToDataTable(sqlCommand).Rows; } public static ArrayList GetDBValue_1(string sqlCommand, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); if (!sqlDataReader.HasRows) { sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return null; } ArrayList arrayList = new ArrayList(); if (sqlDataReader.Read()) { for (int i = 0; i < sqlDataReader.FieldCount; i++) { arrayList.Add(sqlDataReader[i]); } } sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); sqlCommand2.Dispose(); return arrayList; } public static ArrayList GetDBValue_2(string sqlCommand, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); if (!sqlDataReader.HasRows) { sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return null; } ArrayList arrayList = new ArrayList(); while (sqlDataReader.Read()) { arrayList.Add(sqlDataReader[0]); } sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); sqlCommand2.Dispose(); return arrayList; } public static object GetDBValue_3(string sqlCommand) { serlog(sqlCommand); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static object GetDBValue_3(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static object GetDBValue_3(string sqlCommand, string db) { serlog(sqlCommand); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(db)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } } } 这是我的DBA类
07-19
【以下是World.cs类中的 数据列队处理的代码内容 】 public static void MonitorDatabaseQueue() { try { int currentCount = SqlPool.Count; DateTime now = DateTime.Now; if ((now - lastMonitorTime).TotalSeconds >= 5) { processingSpeed = (lastQueueCount - currentCount) / 5; lastQueueCount = currentCount; lastMonitorTime = now; Form1.WriteLine(1, $"数据库队列监控: 当前队列={currentCount}, 处理速度={processingSpeed}/秒"); } if (currentCount > 1000 && processingSpeed > 0) { int requiredThreads = Math.Min(Environment.ProcessorCount * 2, (currentCount / 500) + 1); } } catch (Exception ex) { Form1.WriteLine(1, "队列监控出错: " + ex.ToString()); } } public static void ProcessSqlQueue() { var connectionCache = new Dictionary<string, SqlConnection>(); try { const int maxRetry = 3; const int maxProcessPerCycle = 5000; const int batchSize = 50; int processedCount = 0; var batchList = new List<DbPoolClass>(batchSize); while (processedCount < maxProcessPerCycle) { while (batchList.Count < batchSize && processedCount < maxProcessPerCycle && SqlPool.TryDequeue(out DbPoolClass item)) { batchList.Add(item); processedCount++; } if (batchList.Count == 0) { break; } try { foreach (var group in batchList.GroupBy(x => x.Conn)) { SqlConnection conn = null; try { if (!connectionCache.TryGetValue(group.Key, out conn) || conn.State != ConnectionState.Open) { conn = new SqlConnection(group.Key); conn.Open(); connectionCache[group.Key] = conn; } foreach (var item in group) { int result = DbPoolClass.DbPoolClassRun(conn, item.Sql, item.Prams, item.Type); if (result == -1 && item.RetryCount < maxRetry) { item.RetryCount++; SqlPool.Enqueue(item); } } } catch (Exception ex) { Form1.WriteLine(1, $"分组执行出错: {ex.Message}"); SafeCloseConnection(conn, group.Key, connectionCache); foreach (var item in group) { if (item.RetryCount < maxRetry) { item.RetryCount++; SqlPool.Enqueue(item); } } } } } catch (Exception ex) { Form1.WriteLine(1, $"批量处理出错: {ex.Message}"); foreach (var item in batchList) { if (item.RetryCount < maxRetry) { item.RetryCount++; SqlPool.Enqueue(item); } } } batchList.Clear(); if (SqlPool.Count > 1000) { Thread.Sleep(Math.Min(50, SqlPool.Count / 200)); } } } finally { foreach (var conn in connectionCache.Values) { SafeCloseConnection(conn); } } } private static void SafeCloseConnection(SqlConnection conn, string key = null, Dictionary<string, SqlConnection> cache = null) { try { if (conn?.State == ConnectionState.Open) conn.Close(); if (key != null && cache != null) cache.Remove(key); } catch { } } 【以上是 World.cs类的数据库列队处理代码内容】 【以下是 DbPoolClass.cs类全部代码内容】 using System; using System.Collections.Concurrent; using System.Data; using System.Data.SqlClient; using System.Threading; using System.Threading.Tasks; namespace RxjhServer.DbClss { public class DbPoolClass { private string _Conn; private SqlParameter[] _Prams; private int _Type; private string _Sql; public int _RetryCount; public string Conn { get { return _Conn; } set { _Conn = value; } } public SqlParameter[] Prams { get { return _Prams; } set { _Prams = value; } } public int Type { get { return _Type; } set { _Type = value; } } public int RetryCount { get { return _RetryCount; } set { _RetryCount = value; } } public string Sql { get { return _Sql; } set { _Sql = value; } } public static int DbPoolClassRun(SqlConnection conn, string procName, SqlParameter[] prams, int Type) { try { if (conn == null || conn.State != ConnectionState.Open) { Form1.WriteLine(1, $"[SQL错误] 连接不可用:{procName}"); return -1; } //Form1.WriteLine(1, $"[SQL执行] 数据库:{conn.Database} 开始执行:{procName}"); int result; if (Type == 1) { result = SqlDBA.RunProcSql(conn, procName, prams); } else { result = SqlDBA.RunProc(conn, procName, prams); } //Form1.WriteLine(1, $"[SQL完成] 数据库:{conn.Database} 执行成功:{procName}"); return result; } catch (SqlException sqlEx) { if (sqlEx.Number == 1205 || sqlEx.Number == -2) { Form1.WriteLine(1, $"DbPoolClassRun 致命错误[{sqlEx.Number}]:{procName}, 错误:{sqlEx.Message}"); return 0; } switch (sqlEx.Number) { case 53: case 233: Form1.WriteLine(1, $"DbPoolClassRun 连接错误[{sqlEx.Number}]:{procName}, 错误:{sqlEx.Message}"); Thread.Sleep(5000); return -1; default: Form1.WriteLine(1, $"DbPoolClassRun SQL错误[{sqlEx.Number}]:{procName}, 错误:{sqlEx.Message}"); return -1; } } catch (Exception ex) { Form1.WriteLine(1, $"DbPoolClassRun 常规错误:{procName}, 错误:{ex.Message}\n堆栈:{ex.StackTrace}"); return -1; } } private static string GetConnectionPoolStats(string connectionString) { try { var builder = new SqlConnectionStringBuilder(connectionString); using (var conn = new SqlConnection(builder.ConnectionString)) { var pool = typeof(SqlConnection).GetMethod("GetConnectionPoolCount", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); if (pool != null) { var counts = (int[])pool.Invoke(null, new object[] { builder.ConnectionString }); return $"池中连接数: {counts[0]}, 活动连接: {counts[1]}, 空闲连接: {counts[2]}"; } return "无法获取连接池信息"; } } catch { return "连接池状态获取失败"; } } } } 【以上是 DbPoolClass.cs类全部代码内容】 【以下是 SqlDBA.cs 类中全部代码内容】 using System; using System.Data; using System.Data.SqlClient; namespace RxjhServer.DbClss { public class SqlDBA { private static readonly object _connectionLock = new object(); public static int RunProc(SqlConnection conn, string procName, SqlParameter[] prams) { try { if (conn.State != ConnectionState.Open) { conn.Open(); } using (var cmd = CreateCommand(conn, procName, prams)) { cmd.ExecuteNonQuery(); return (int)cmd.Parameters["ReturnValue"].Value; } } catch (SqlException sqlEx) { Form1.WriteLine(100, $"SqlDBA数据层_存储过程错误 {procName}: {sqlEx.Number} - {sqlEx.Message}"); return -1; } catch (Exception ex) { Form1.WriteLine(100, $"SqlDBA数据层_错误1 {procName}: {ex.Message}"); return -1; } } public static int RunProcSql(SqlConnection conn, string procName, SqlParameter[] prams) { try { if (conn.State != ConnectionState.Open) { conn.Open(); } using (var cmd = CreateCommandSql(conn, procName, prams)) { return cmd.ExecuteNonQuery(); } } catch (SqlException sqlEx) { Form1.WriteLine(100, $"SqlDBA数据层_SQL错误 {procName}: {sqlEx.Number} - {sqlEx.Message}"); return -1; } catch (Exception ex) { Form1.WriteLine(100, $"SqlDBA数据层_错误2 {procName}: {ex.Message}"); return -1; } } public static SqlCommand CreateCommand(SqlConnection conn, string procName, SqlParameter[] prams) { if (conn == null) { throw new ArgumentNullException(nameof(conn)); } if (string.IsNullOrWhiteSpace(procName)) { throw new ArgumentException("Procedure name cannot be empty.", nameof(procName)); } var command = new SqlCommand(procName, conn) { CommandType = CommandType.StoredProcedure, CommandTimeout = 180 }; AddParametersToCommand(command, prams); return command; } public static SqlCommand CreateCommandSql(SqlConnection conn, string sqlQuery, SqlParameter[] prams) { if (conn == null) { throw new ArgumentNullException(nameof(conn)); } if (string.IsNullOrWhiteSpace(sqlQuery)) { throw new ArgumentException("SQL query cannot be empty.", nameof(sqlQuery)); } var command = new SqlCommand(sqlQuery, conn) { CommandType = CommandType.Text, CommandTimeout = 180 }; AddParametersToCommand(command, prams); return command; } private static void AddParametersToCommand(SqlCommand command, SqlParameter[] prams) { if (prams != null && prams.Length > 0) { command.Parameters.AddRange(prams); } command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue }); } public static SqlParameter MakeInParam(string paramName, SqlDbType dbType, int size, object value) { return MakeParam(paramName, dbType, size, ParameterDirection.Input, value); } public static SqlParameter MakeParam(string paramName, SqlDbType dbType, int size, ParameterDirection direction, object value) { if (string.IsNullOrWhiteSpace(paramName)) { throw new ArgumentException("Parameter name cannot be empty.", nameof(paramName)); } var parameter = size > 0 ? new SqlParameter(paramName, dbType, size) : new SqlParameter(paramName, dbType); parameter.Direction = direction; if (direction != ParameterDirection.Output || value != null) { parameter.Value = value ?? DBNull.Value; } return parameter; } } } 【以上是 SqlDBA.cs 类中全部代码内容】 【以下是 DBA.cs 类中全部代码内容】 using System; using System.Collections; using System.Data; using System.Data.SqlClient; namespace RxjhServer.DbClss { public class DBA { public static void serlog(string txt) { string sqlJl = World.SqlJl; if (!(sqlJl != "")) { return; } string[] array = sqlJl.Split('|'); for (int i = 0; i < array.Length; i++) { if (txt.ToLower().IndexOf(array[i].ToLower()) != -1) { Form1.WriteLine(100, txt); } } } public static void serlog(string txt, SqlParameter[] prams) { string sqlJl = World.SqlJl; if (!(sqlJl != "")) { return; } string[] array = sqlJl.Split('|'); for (int i = 0; i < array.Length; i++) { if (txt.ToLower().IndexOf(array[i].ToLower()) != -1) { Form1.WriteLine(100, txt); } } for (int j = 0; j < array.Length; j++) { foreach (SqlParameter sqlParameter in prams) { if (sqlParameter.SqlValue.ToString().ToLower().IndexOf(array[j].ToLower()) != -1) { Form1.WriteLine(100, txt + " " + sqlParameter.SqlValue.ToString()); } } } } public static void Setlog(string txt, SqlParameter[] prams, Exception ex) { Form1.WriteLine(100, "-----------DBA数据层_错误-----------"); Form1.WriteLine(100, txt); if (prams != null) { foreach (SqlParameter sqlParameter in prams) { Form1.WriteLine(100, sqlParameter.SqlValue.ToString()); } } Form1.WriteLine(100, ex.Message); } public static string getstrConnection(string db) { try { if (db == null) { db = "GameServer"; } if (World.Db.TryGetValue(db, out var value)) { return value.SqlConnect; } return null; } catch { return null; } } public static int ExeSqlCommand(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, prams, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, SqlParameter[] prams, string server) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, prams, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, string server) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, ref Exception exception, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch (Exception ex) { Exception ex2 = (exception = ex); return -1; } int result = sqlCommand2.ExecuteNonQuery(); sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static DateTime GetDateTime(string sql, string dbName) { string connectionString = getstrConnection(dbName); if (string.IsNullOrEmpty(connectionString)) { return DateTime.MinValue; } using (SqlConnection conn = new SqlConnection(connectionString)) { try { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { object result = cmd.ExecuteScalar(); if (result != null && result != DBNull.Value) { return Convert.ToDateTime(result); } return DateTime.MinValue; } } catch { return DateTime.MinValue; } } } public static DataTable GetDBToDataTable(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand3 = (sqlDataAdapter.SelectCommand = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams)); SqlCommand sqlCommand4 = sqlCommand3; using (sqlCommand4) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误1" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, prams, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand, SqlParameter[] prams, string server) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand3 = (sqlDataAdapter.SelectCommand = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams)); SqlCommand sqlCommand4 = sqlCommand3; using (sqlCommand4) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误2" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, prams, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand3 = (sqlDataAdapter.SelectCommand = new SqlCommand(sqlCommand, sqlConnection)); SqlCommand sqlCommand4 = sqlCommand3; using (sqlCommand4) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误3" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, null, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand, string server) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand3 = (sqlDataAdapter.SelectCommand = new SqlCommand(sqlCommand, sqlConnection)); SqlCommand sqlCommand4 = sqlCommand3; using (sqlCommand4) { try { sqlConnection.Open(); } catch { return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataRowCollection smethod_0(string sqlCommand, string db) { return GetDBToDataTable(sqlCommand).Rows; } public static ArrayList GetDBValue_1(string sqlCommand, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); if (!sqlDataReader.HasRows) { sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return null; } ArrayList arrayList = new ArrayList(); if (sqlDataReader.Read()) { for (int i = 0; i < sqlDataReader.FieldCount; i++) { arrayList.Add(sqlDataReader[i]); } } sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return arrayList; } public static ArrayList GetDBValue_2(string sqlCommand, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); if (!sqlDataReader.HasRows) { sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return null; } ArrayList arrayList = new ArrayList(); while (sqlDataReader.Read()) { arrayList.Add(sqlDataReader[0]); } sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return arrayList; } public static object GetDBValue_3(string sqlCommand) { serlog(sqlCommand); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误4" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static object GetDBValue_3(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误5" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static object GetDBValue_3(string sqlCommand, string db) { serlog(sqlCommand); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(db)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误6" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } } } 【以上是 DBA.cs 类中全部代码内容】 数据库队列监控: 当前队列=1278, 处理速度=-255/秒 2025/6/24 16:48:54 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:48:54 数据库队列监控: 当前队列=1646, 处理速度=-73/秒 2025/6/24 16:49:02 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:49:02 数据库队列监控: 当前队列=1128, 处理速度=103/秒 2025/6/24 16:49:11 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:49:11 数据库队列监控: 当前队列=1501, 处理速度=-74/秒 2025/6/24 16:49:19 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:49:19 数据库队列监控: 当前队列=984, 处理速度=103/秒 2025/6/24 16:49:19 封包处理错误: 封包处理超时 2025/6/24 16:49:27 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:49:27 数据库队列监控: 当前队列=1372, 处理速度=-77/秒 2025/6/24 16:49:35 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:49:36 数据库队列监控: 当前队列=2027, 处理速度=-131/秒 2025/6/24 16:49:44 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:49:44 数据库队列监控: 当前队列=2163, 处理速度=-27/秒 2025/6/24 16:49:53 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:49:53 数据库队列监控: 当前队列=2558, 处理速度=-79/秒 2025/6/24 16:50:02 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:50:02 数据库队列监控: 当前队列=2949, 处理速度=-78/秒 2025/6/24 16:50:11 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:50:11 数据库队列监控: 当前队列=3341, 处理速度=-78/秒 2025/6/24 16:50:20 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:50:20 数据库队列监控: 当前队列=3730, 处理速度=-77/秒 2025/6/24 16:50:30 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:50:30 数据库队列监控: 当前队列=4113, 处理速度=-76/秒 2025/6/24 16:50:39 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:50:39 数据库队列监控: 当前队列=5404, 处理速度=-258/秒 2025/6/24 16:50:49 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:50:49 数据库队列监控: 当前队列=6695, 处理速度=-258/秒 2025/6/24 16:51:02 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:51:02 数据库队列监控: 当前队列=8892, 处理速度=-439/秒 2025/6/24 16:51:15 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:51:15 数据库队列监控: 当前队列=12004, 处理速度=-622/秒 2025/6/24 16:51:29 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:51:29 数据库队列监控: 当前队列=15627, 处理速度=-724/秒 2025/6/24 16:51:43 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:51:44 数据库队列监控: 当前队列=19847, 处理速度=-844/秒 2025/6/24 16:51:56 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:51:56 数据库队列监控: 当前队列=22235, 处理速度=-477/秒 2025/6/24 16:52:10 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:52:10 数据库队列监控: 当前队列=25479, 处理速度=-648/秒 2025/6/24 16:52:23 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:52:23 数据库队列监控: 当前队列=28448, 处理速度=-593/秒 2025/6/24 16:52:37 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:52:37 数据库队列监控: 当前队列=31561, 处理速度=-622/秒 2025/6/24 16:52:51 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:52:51 数据库队列监控: 当前队列=35604, 处理速度=-808/秒 2025/6/24 16:53:05 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:53:05 数据库队列监控: 当前队列=39570, 处理速度=-793/秒 2025/6/24 16:53:20 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:53:20 数据库队列监控: 当前队列=42944, 处理速度=-674/秒 2025/6/24 16:53:32 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:53:32 数据库队列监控: 当前队列=45219, 处理速度=-455/秒 2025/6/24 16:53:46 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:53:47 数据库队列监控: 当前队列=49989, 处理速度=-954/秒 2025/6/24 16:54:01 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:54:01 数据库队列监控: 当前队列=53102, 处理速度=-622/秒 2025/6/24 16:54:16 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:54:16 数据库队列监控: 当前队列=57119, 处理速度=-803/秒 2025/6/24 16:54:29 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:54:29 数据库队列监控: 当前队列=60229, 处理速度=-622/秒 2025/6/24 16:54:46 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:54:46 数据库队列监控: 当前队列=66041, 处理速度=-1162/秒 2025/6/24 16:55:00 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:55:00 数据库队列监控: 当前队列=69154, 处理速度=-622/秒 2025/6/24 16:55:14 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:55:14 数据库队列监控: 当前队列=72530, 处理速度=-675/秒 2025/6/24 16:55:27 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:55:27 数据库队列监控: 当前队列=75374, 处理速度=-568/秒 2025/6/24 16:55:41 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:55:42 数据库队列监控: 当前队列=79383, 处理速度=-801/秒 2025/6/24 16:55:59 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:55:59 数据库队列监控: 当前队列=85195, 处理速度=-1162/秒 2025/6/24 16:56:12 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:56:12 数据库队列监控: 当前队列=87402, 处理速度=-441/秒 2025/6/24 16:56:27 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:56:27 数据库队列监控: 当前队列=91412, 处理速度=-802/秒 2025/6/24 16:56:46 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:56:46 数据库队列监控: 当前队列=98127, 处理速度=-1343/秒 2025/6/24 16:57:02 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:57:02 数据库队列监控: 当前队列=103037, 处理速度=-982/秒 2025/6/24 16:57:19 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:57:19 数据库队列监控: 当前队列=107955, 处理速度=-983/秒 2025/6/24 16:57:34 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:57:34 数据库队列监控: 当前队列=111970, 处理速度=-803/秒 2025/6/24 16:57:52 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:57:53 数据库队列监控: 当前队列=118709, 处理速度=-1347/秒 2025/6/24 16:58:08 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:58:08 数据库队列监控: 当前队列=123743, 处理速度=-1006/秒 2025/6/24 16:58:32 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:58:32 数据库队列监控: 当前队列=132499, 处理速度=-1751/秒 2025/6/24 16:59:01 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:59:01 数据库队列监控: 当前队列=145462, 处理速度=-2592/秒 2025/6/24 16:59:08 封包处理错误: 封包处理超时 2025/6/24 16:59:22 网络队列: 连接数=302(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/24 16:59:22 数据库队列监控: 当前队列=153043, 处理速度=-1516/秒 2025/6/24 16:59:23 封包处理错误: 封包处理超时 2025/6/24 16:59:33 封包处理错误: 封包处理超时 2025/6/24 16:59:34 封包处理错误: 封包处理超时 上诉数据列队处理的代码垃圾的很,登录300个假人测试了40分钟数据列队直接飙升到55万,CPU使用直接飙升到100%,无法形容的垃圾代码 然后你给我优化了一下 新建了一个类 : using RxjhServer; using RxjhServer.DbClss; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace RxjhServer { public static class DatabaseQueueProcessor { private static DateTime lastMonitorTime = DateTime.MinValue; private static int lastQueueCount = 0; private static double processingSpeed = 0; private static int activeThreads = 0; private static readonly object threadLock = new object(); private static CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); public static void StartProcessing() { // 启动监控线程 Task.Run(() => MonitorDatabaseQueue()); // 初始启动4个处理线程 for (int i = 0; i < 4; i++) { StartNewProcessingThread(); } } public static void StopProcessing() { cancellationTokenSource.Cancel(); } private static void StartNewProcessingThread() { lock (threadLock) { if (activeThreads >= Environment.ProcessorCount * 4) return; activeThreads++; } Task.Run(() => { try { ProcessSqlQueue(cancellationTokenSource.Token); } finally { lock (threadLock) { activeThreads--; } } }); } public static void MonitorDatabaseQueue() { while (!cancellationTokenSource.IsCancellationRequested) { try { int currentCount = World.SqlPool.Count; DateTime now = DateTime.Now; // 每5秒计算一次处理速度 if ((now - lastMonitorTime).TotalSeconds >= 5) { int processedItems = lastQueueCount - currentCount; // 如果队列增长了,计算增长速率 if (processedItems < 0) { processingSpeed = -processedItems / 5; // 队列增长速率 } else { processingSpeed = processedItems / 5; // 处理速率 } lastQueueCount = currentCount; lastMonitorTime = now; Form1.WriteLine(1, $"数据库队列监控: 当前队列={currentCount}, 处理速度={(processedItems < 0 ? "-" : "")}{processingSpeed}/秒, 活动线程={activeThreads}"); // 动态调整线程数量 AdjustProcessingThreads(currentCount); } Thread.Sleep(1000); // 每秒检查一次 } catch (Exception ex) { Form1.WriteLine(1, "队列监控出错: " + ex.ToString()); Thread.Sleep(5000); } } } private static void AdjustProcessingThreads(int currentQueueSize) { if (currentQueueSize < 500) return; int desiredThreads; if (processingSpeed <= 0 || currentQueueSize / processingSpeed > 10) { // 队列在增长或处理速度不足 desiredThreads = Math.Min( Environment.ProcessorCount * 4, Math.Max(4, (int)(currentQueueSize / 1000) + 1) ); } else { // 队列在处理中,保持稳定 desiredThreads = Math.Min( Environment.ProcessorCount * 2, Math.Max(2, (int)(currentQueueSize / 2000) + 1) ); } lock (threadLock) { while (activeThreads < desiredThreads) { StartNewProcessingThread(); } } } public static void ProcessSqlQueue(CancellationToken cancellationToken) { var connectionCache = new Dictionary<string, SqlConnection>(); try { const int maxRetry = 3; const int maxProcessPerCycle = 1000; // 减少每周期处理量以避免长时间占用 const int batchSize = 50; while (!cancellationToken.IsCancellationRequested) { int processedCount = 0; var batchList = new List<DbPoolClass>(batchSize); // 批量获取任务项 while (batchList.Count < batchSize && processedCount < maxProcessPerCycle && World.SqlPool.TryDequeue(out DbPoolClass item)) { batchList.Add(item); processedCount++; } if (batchList.Count == 0) { Thread.Sleep(50); // 队列为空时短暂休眠 continue; } // 按连接字符串分组处理 foreach (var group in batchList.GroupBy(x => x.Conn)) { SqlConnection conn = null; try { // 获取或创建连接 if (!connectionCache.TryGetValue(group.Key, out conn) || conn.State != ConnectionState.Open) { SafeCloseConnection(conn, group.Key, connectionCache); conn = new SqlConnection(group.Key); conn.Open(); connectionCache[group.Key] = conn; } // 处理组内所有SQL foreach (var item in group) { if (cancellationToken.IsCancellationRequested) return; int result = DbPoolClass.DbPoolClassRun(conn, item.Sql, item.Prams, item.Type); if (result == -1 && item.RetryCount < maxRetry) { item.RetryCount++; World.SqlPool.Enqueue(item); // 重试 } } } catch (SqlException sqlEx) { Form1.WriteLine(1, $"数据库连接错误[{sqlEx.Number}]: {sqlEx.Message}"); SafeCloseConnection(conn, group.Key, connectionCache); // 重试组内所有项目 foreach (var item in group) { if (item.RetryCount < maxRetry) { item.RetryCount++; World.SqlPool.Enqueue(item); } } } catch (Exception ex) { Form1.WriteLine(1, $"分组执行出错: {ex.Message}"); SafeCloseConnection(conn, group.Key, connectionCache); foreach (var item in group) { if (item.RetryCount < maxRetry) { item.RetryCount++; World.SqlPool.Enqueue(item); } } } } batchList.Clear(); // 动态休眠控制 if (World.SqlPool.Count > 5000) { int sleepTime = Math.Min(100, World.SqlPool.Count / 1000); Thread.Sleep(sleepTime); } } } finally { // 清理所有连接 foreach (var conn in connectionCache.Values) { SafeCloseConnection(conn); } } } private static void SafeCloseConnection(SqlConnection conn, string key = null, Dictionary<string, SqlConnection> cache = null) { try { if (conn != null) { if (conn.State == ConnectionState.Open) conn.Close(); conn.Dispose(); } if (key != null && cache != null && cache.ContainsKey(key)) cache.Remove(key); } catch (Exception ex) { Form1.WriteLine(1, $"关闭连接时出错: {ex.Message}"); } } } } 2025/6/25 2:02:00 数据库队列监控: 当前队列=0, 处理速度=8/秒, 活动线程=4 2025/6/25 2:02:01 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:05 数据库队列监控: 当前队列=193, 处理速度=-38/秒, 活动线程=4 2025/6/25 2:02:06 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:10 数据库队列监控: 当前队列=0, 处理速度=38/秒, 活动线程=4 2025/6/25 2:02:12 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:15 数据库队列监控: 当前队列=678, 处理速度=-135/秒, 活动线程=4 2025/6/25 2:02:17 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:20 数据库队列监控: 当前队列=1151, 处理速度=-94/秒, 活动线程=4 2025/6/25 2:02:22 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:25 数据库队列监控: 当前队列=357, 处理速度=158/秒, 活动线程=4 2025/6/25 2:02:27 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:30 数据库队列监控: 当前队列=937, 处理速度=-116/秒, 活动线程=4 2025/6/25 2:02:32 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:36 数据库队列监控: 当前队列=0, 处理速度=187/秒, 活动线程=4 2025/6/25 2:02:37 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:41 数据库队列监控: 当前队列=328, 处理速度=-65/秒, 活动线程=4 2025/6/25 2:02:42 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:46 数据库队列监控: 当前队列=111, 处理速度=43/秒, 活动线程=4 2025/6/25 2:02:47 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:51 数据库队列监控: 当前队列=58, 处理速度=10/秒, 活动线程=4 2025/6/25 2:02:52 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:02:56 数据库队列监控: 当前队列=40, 处理速度=3/秒, 活动线程=4 2025/6/25 2:02:57 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:03:01 数据库队列监控: 当前队列=0, 处理速度=8/秒, 活动线程=4 2025/6/25 2:03:03 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 2025/6/25 2:03:06 数据库队列监控: 当前队列=0, 处理速度=0/秒, 活动线程=4 2025/6/25 2:03:08 网络队列: 连接数=419(活跃=0) 总队列=0KB 最大队列=0字节 过载连接=0 但是新建这个类 我登录了400个假人做测试 列队明显居高不下 而且落差很大 感觉处理很吃力的样子 最明显的是CPU 一会30% 一会96% 大多数都在85%以上使用率 这明显代码有问题啊
06-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值