diff --git a/Core/EnumDbType.cs b/Core/EnumDbType.cs new file mode 100644 index 0000000000000000000000000000000000000000..90497641f788f57fe52f0de238ce37e51662269b --- /dev/null +++ b/Core/EnumDbType.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CSU.Ligq.Core +{ + /// + /// 数据类型 + /// + public enum DbType + { + /// + /// 未知数据库 + /// + UNKNOW = 0, + POSTGIS = 1, + SQLSERVER = 2 + } +} diff --git a/Core/SketchStyleConfig.cs b/Core/SketchStyleConfig.cs new file mode 100644 index 0000000000000000000000000000000000000000..91951ef059e763cb4adbad404a14f95ac151d4bf --- /dev/null +++ b/Core/SketchStyleConfig.cs @@ -0,0 +1,109 @@ +using ESRI.ArcGIS.Display; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CSU.Ligq.Core +{ + /// + /// 草图风格配置 + /// + public class SketchStyleConfig + { + + IRgbColor lineColor = new RgbColor(); + int lineWidth =1; + IRgbColor fillColor = new RgbColor(); + esriSimpleLineStyle lineStyle; + + /// + /// 线颜色 + /// + public IRgbColor LineColor { get => lineColor; set => lineColor = value; } + /// + /// 线宽 + /// + public int LineWidth { get => lineWidth; set => lineWidth = value; } + /// + /// 填充颜色 + /// + public IRgbColor FillColor { get => fillColor; set => fillColor = value; } + /// + /// 线型 + /// + public esriSimpleLineStyle LineStyle { get => lineStyle; set => lineStyle = value; } + + static SketchStyleConfig instance; + + /// + /// 获取/创建实例 + /// + /// + public static SketchStyleConfig GetInstance() + { + if (instance == null) + { + instance = new SketchStyleConfig(); + instance.ReadConfig(); + } + return instance; + } + + /// + /// 读取数据库配置 + /// + public void ReadConfig() + { + try + { + string str = ConfigurationManager.AppSettings["sketch-line-color"].Trim(); + string[] arr = str.Split(','); + if (arr.Length == 3) + { + this.lineColor.Red = int.Parse(arr[0]); + this.lineColor.Green = int.Parse(arr[1]); + this.lineColor.Blue = int.Parse(arr[2]); + } + + str = ConfigurationManager.AppSettings["sketch-fill-color"].Trim(); + arr = str.Split(','); + if (arr.Length == 3) + { + this.fillColor.Red = int.Parse(arr[0]); + this.fillColor.Green = int.Parse(arr[1]); + this.fillColor.Blue = int.Parse(arr[2]); + } + + str = ConfigurationManager.AppSettings["sketch-line-width"].Trim(); + int.TryParse(str, out this.lineWidth); + + str = ConfigurationManager.AppSettings["sketch-line-style"].Trim(); + int i = 0; + int.TryParse(str, out i); + this.lineStyle = (esriSimpleLineStyle)i; + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// 保存配置 + /// + public void SaveConfig() + { + Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); + config.AppSettings.Settings["sketch-line-color"].Value = $"{this.lineColor.Red},{this.lineColor.Green},{this.lineColor.Blue}"; + config.AppSettings.Settings["sketch-line-width"].Value = this.lineWidth.ToString(); + config.AppSettings.Settings["sketch-line-style"].Value = ((int)this.lineStyle).ToString(); + config.AppSettings.Settings["sketch-fill-color"].Value = $"{this.fillColor.Red},{this.fillColor.Green},{this.fillColor.Blue}"; ; + + config.Save(ConfigurationSaveMode.Modified); + } + } +} + diff --git a/DAL/IDBHelper.cs b/DAL/IDBHelper.cs new file mode 100644 index 0000000000000000000000000000000000000000..648b22ddd9366dda0b951b1aa61de4424f024007 --- /dev/null +++ b/DAL/IDBHelper.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CSU.Ligq.Dal +{ + /// + /// 接口:数据库助手接口 + /// 作者:李光强 + /// 时间:2023.11.11 + /// + /// + public interface IDBHelper + { + Core.DbType GetDbType(); + /// + /// 创建数据库助手实例 + /// + /// + IDBHelper CreateInstance(); + + /// + /// 获取数据库的版本,用于测试数据库连接是否成功 + /// + /// + string GetVersion(); + + /// + /// 获取记录数 + /// + /// 查询条件从句 + /// 数据库名称,可以指定模式名称 + int GetCount(string tblName, string where); + + /// + /// 执行查询SQL + /// + /// 查询参数列表 + /// sql语句 + /// DataTable + DataTable ExecuteQuery( + string sql + , params DbParameter[] cmdParms); + + /// + /// 执行查询 + /// + /// 查询参数列表 + /// 查询命令 + /// 查询方式 + /// DataTable + DataTable ExecuteQuery( + CommandType cmdType + , string cmdText + , DbParameter[] cmdParms = null); + + /// + /// 执行SQL语句 语句并返回受影响的行数。 + /// + /// 参数列表 + /// SQL语句 + /// 命令类型 + int ExecuteNonQuery(string cmdText, CommandType cmdType = CommandType.Text, DbParameter[] cmdParms = null); + + /// + /// 执行查询,返回DataReader + /// + DbDataReader ExecuteReader( + CommandType cmdType + , string cmdText + , params DbParameter[] cmdParms); + + /// + /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。 + /// + /// 参数列表 + /// SQL + /// 命令类型 + object ExecuteScalar(CommandType cmdType, string cmdText, + params DbParameter[] cmdParms); + } +} diff --git a/DAL/SqlServerHelper.cs b/DAL/SqlServerHelper.cs new file mode 100644 index 0000000000000000000000000000000000000000..3acc010ba24b81ec33f960d86e2f23c699a3116f --- /dev/null +++ b/DAL/SqlServerHelper.cs @@ -0,0 +1,338 @@ +using System; +using System.Data; +using System.Data.OleDb; +using System.Data.SqlClient; +using System.Windows.Forms; +using System.IO; +using System.Drawing; +using System.Data.Common; +using CSU.Ligq.Core; +using System.Text; + +namespace CSU.Ligq.Dal +{ + /// + /// SQLSERVERݿ + /// ߣǿ + /// ʱ䣺2009.11.18 + /// 汾V1.0 + /// ޸ģ2023.11.12 + /// 汾V2.0 + /// + /// + public class SQLServerHelper : IDBHelper + { + #region ˽б + + /// + /// ַ + /// + /// ʾ: "Host=localhost;Username=postgres;Password=s$cret;Database=testdb"; + string connectionString; + + /// + /// ݿ + /// + protected SqlConnection connection; + + #endregion + + /// + /// ʵ + /// + /// + public IDBHelper CreateInstance() + { + return new SQLServerHelper(); + } + + /// + /// 캯 + /// + /// + public SQLServerHelper ( ) + { + this.connectionString = string.Format("Data Source={0},{4};persist security info=true;user id={1};password={2};Initial Catalog={3};" + , DbConfig.GetInstance().Host + , DbConfig.GetInstance().User + , DbConfig.GetInstance().Password + , DbConfig.GetInstance().Database + , DbConfig.GetInstance().Port + ); + } + /// + /// 캯 + /// + /// + public SQLServerHelper(string connStr) + { + this.connectionString = connStr; + } + /// + /// 캯 + /// + /// + public SQLServerHelper(string host, string user, string password, string dataBase, int port) + { + this.connectionString = string.Format("Data Source={0},{4};persist security info=true;user id={1};password={2};Initial Catalog={3};" + , host + , user + , password + , dataBase + , port + ); + } + + /// + /// + /// + /// + public Core.DbType GetDbType() + { + return Core.DbType.SQLSERVER; + } + + /// + /// ȡݿİ汾ڲݿǷɹ + /// + /// + public string GetVersion() + { + try + { + string sql = "select @@VERSION"; + DataTable dt = this.ExecuteQuery(sql); + if (dt != null) + { + DataRow row = dt.Rows[0]; + return row[0].ToString(); + } + else + throw new Exception("ʧ."); + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// ȡ¼ + /// + /// ѯӾ + /// ݿƣָģʽ + public int GetCount(string tblName, string condition) + { + try + { + StringBuilder sql = new StringBuilder($"select count(*) from {tblName}"); + if (!string.IsNullOrEmpty(condition)) + sql.Append($" where {condition}"); + + object count = ExecuteScalar(CommandType.Text, sql.ToString(), null); + return int.Parse(count.ToString()); + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// ִвѯSQL + /// + /// ѯб + /// sql + /// DataTable + public DataTable ExecuteQuery( + string sql + , params DbParameter[] cmdParms) + { + try + { + using (SqlConnection conn = new SqlConnection(connectionString)) + { + conn.Open(); + this.connection = conn; + using (SqlCommand cmd = new SqlCommand()) + { + PrepareCommand(cmd, CommandType.Text, sql, cmdParms); + using (SqlDataAdapter da = new SqlDataAdapter(cmd)) + { + DataSet ds = new DataSet(); + da.Fill(ds, "ds"); + cmd.Parameters.Clear(); + if (ds.Tables.Count > 0) + return ds.Tables[0]; + else + return null; + } + } + } + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// ִвѯ + /// + /// ѯб + /// ѯ + /// ѯʽ + /// DataTable + public DataTable ExecuteQuery( + CommandType cmdType + , string cmdText + , DbParameter[] cmdParms = null) + { + try + { + using (SqlConnection conn = new SqlConnection(connectionString)) + { + conn.Open(); + this.connection = conn; + using (SqlCommand cmd = new SqlCommand()) + { + PrepareCommand(cmd, cmdType, cmdText, cmdParms); + using (SqlDataAdapter da = new SqlDataAdapter(cmd)) + { + DataSet ds = new DataSet(); + da.Fill(ds, "ds"); + cmd.Parameters.Clear(); + if (ds.Tables.Count > 0) + return ds.Tables[0]; + else + return null; + } + } + } + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// ִSQL 䲢Ӱ + /// + /// б + /// SQL + /// + public int ExecuteNonQuery(string cmdText, CommandType cmdType = CommandType.Text, DbParameter[] cmdParms = null) + { + try + { + using (SqlConnection conn = new SqlConnection(connectionString)) + { + conn.Open(); + this.connection = conn; + using (SqlCommand cmd = new SqlCommand()) + { + PrepareCommand(cmd, cmdType, cmdText, cmdParms); + int val = cmd.ExecuteNonQuery(); + cmd.Parameters.Clear(); + return val; + } + } + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// ִвѯDataReader + /// + public DbDataReader ExecuteReader( + CommandType cmdType + , string cmdText + , params DbParameter[] cmdParms) + { + try + { + using (SqlConnection conn = new SqlConnection(connectionString)) + { + conn.Open(); + this.connection = conn; + using (SqlCommand cmd = new SqlCommand()) + { + PrepareCommand(cmd, cmdType, cmdText, cmdParms); + SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); + cmd.Parameters.Clear(); + return rdr; + } + } + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// ִвѯزѯصĽеһеĵһСлС + /// + /// б + /// SQL + /// + public object ExecuteScalar(CommandType cmdType, string cmdText, + params DbParameter[] cmdParms) + { + try + { + using (SqlConnection conn = new SqlConnection(connectionString)) + { + conn.Open(); + this.connection = conn; + using (SqlCommand cmd = new SqlCommand()) + { + PrepareCommand(cmd, cmdType, cmdText, cmdParms); + object val = cmd.ExecuteScalar(); + cmd.Parameters.Clear(); + return val; + } + } + } + catch (Exception ex) + { + throw ex; + } + } + + /// + /// Ҫִе + /// + /// ĸʽð+ + private void PrepareCommand( + DbCommand cmd + , CommandType cmdType + , string cmdText + , DbParameter[] cmdParms) + { + try + { + cmd.Connection = this.connection; + cmd.CommandText = cmdText;//.Replace(":", "@").Replace("?", "@"); + cmd.CommandType = cmdType; + + if (cmdParms != null) + { + foreach (SqlParameter parm in cmdParms) + { + parm.ParameterName = parm.ParameterName;//.Replace(":", "@").Replace("?", "@"); + cmd.Parameters.Add(parm); + } + } + } + catch (Exception ex) + { + throw ex; + } + } + } +}