using DevExpress.XtraEditors; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geodatabase; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace KGIS.Plugin.LayerProperty.Model { internal class SQLSearch { private static IFeatureLayer m_pCurrentLayer; private static IFeatureClass m_FeatureClass; public static IFeatureLayer CurrentLayer { get { return SQLSearch.m_pCurrentLayer; } set { SQLSearch.m_pCurrentLayer = value; SQLSearch.FeatureClass = SQLSearch.m_pCurrentLayer.FeatureClass; } } public static IFeatureClass FeatureClass { get { return SQLSearch.m_FeatureClass; } set { SQLSearch.m_FeatureClass = value; } } public static string ReadFile(string ReadFileName) { if (File.Exists(ReadFileName)) { return SQLSearch.ReadStringFromFile(ReadFileName); } return ""; } public static string ReadStringFromFile(string filePath) { string result; try { FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream, Encoding.Unicode); string text = streamReader.ReadToEnd(); streamReader.Close(); fileStream.Close(); result = text; } catch (Exception ex) { //RdbUtil.AddException(ex); result = ""; } return result; } public static string ReadStringFromFile(string filePath, Encoding encode) { string result; try { FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream, encode); string text = streamReader.ReadToEnd(); streamReader.Close(); fileStream.Close(); result = text; } catch (Exception ex) { //RdbUtil.AddException(ex); result = ""; } return result; } public static bool SaveString2File(string filePath, ref string content) { bool result; try { FileStream fileStream = new FileStream(filePath, FileMode.Create); StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.Unicode); streamWriter.Write(content); streamWriter.Close(); fileStream.Close(); result = true; } catch (Exception ex) { //RdbUtil.AddException(ex); result = false; } return result; } public static void SaveFile(string SaveFileName, ref string SqlInfo) { if (SaveFileName.Equals("")) { return; } if (File.Exists(SaveFileName)) { File.Delete(SaveFileName); } SQLSearch.SaveString2File(SaveFileName, ref SqlInfo); } public static void ShowUniqueValues(string sqlFieldname, ListBoxControl lstOnlyValue, int SampleRate) { SQLSearch.ShowUniqueValuesPrivate(sqlFieldname, lstOnlyValue, SampleRate); } private static void ShowUniqueValuesPrivate(string sqlField, ListBoxControl lstOnlyValue, int SampleRate) { IEnumerator enumerator = null; try { ICursor cursor = SQLSearch.FeatureClass.Search(null, false) as ICursor; enumerator = ((IDataStatistics)new DataStatisticsClass { Cursor = cursor, Field = sqlField, SampleRate = SampleRate }).UniqueValues; enumerator.Reset(); string text = ""; while (enumerator.MoveNext()) { if (enumerator.Current != null && enumerator.Current != DBNull.Value) { text = enumerator.Current.ToString(); } if (!text.Equals("")) { lstOnlyValue.Items.Add(text); text = ""; } } } catch (Exception ex) { //RdbUtil.AddException(ex); } if (enumerator != null) { enumerator = null; } } public static void ShowUniqueValues(IField field, ListBoxControl lstOnlyValue, int SampleRate) { SQLSearch.ShowUniqueValuesPrivate(field, lstOnlyValue, SampleRate); } private static void ShowUniqueValuesPrivate(IField field, ListBoxControl lstOnlyValue, int SampleRate) { IEnumerator enumerator = null; try { ICursor cursor = SQLSearch.FeatureClass.Search(null, false) as ICursor; enumerator = ((IDataStatistics)new DataStatisticsClass { Cursor = cursor, Field = field.Name, SampleRate = SampleRate }).UniqueValues; enumerator.Reset(); string text = ""; while (enumerator.MoveNext()) { if (enumerator.Current != null && enumerator.Current != DBNull.Value) { text = enumerator.Current.ToString(); } if (!text.Equals("")) { switch (field.Type) { case esriFieldType.esriFieldTypeString: text = "'" + text + "'"; break; case esriFieldType.esriFieldTypeDate: text = "#" + text + "#"; break; } lstOnlyValue.Items.Add(text); text = ""; } else { text = "NULL"; } } } catch (Exception ex) { //RdbUtil.AddException(ex); } if (enumerator != null) { enumerator = null; } } private static void GetString(ref string value) { value = "'" + value + "'"; } public static void ShowUniqueValues(string lstFieldItemName, ComboBoxEdit lstOnlyValue, int SampleRate) { SQLSearch.ShowUniqueValuesPrivate(lstFieldItemName, lstOnlyValue, SampleRate); } private static void ShowUniqueValuesPrivate(string lstFieldItemName, ComboBoxEdit lstOnlyValue, int SampleRate) { IEnumerator enumerator = null; try { ICursor cursor = SQLSearch.FeatureClass.Search(null, false) as ICursor; enumerator = ((IDataStatistics)new DataStatisticsClass { Cursor = cursor, Field = lstFieldItemName, SampleRate = SampleRate }).UniqueValues; enumerator.Reset(); string text = ""; while (enumerator.MoveNext()) { if (enumerator.Current != null && enumerator.Current != DBNull.Value) { text = enumerator.Current.ToString(); } if (!text.Equals("")) { lstOnlyValue.Properties.Items.Add(text); } text = ""; } } catch (Exception ex) { //RdbUtil.AddException(ex); } if (enumerator != null) { enumerator = null; } } public static bool VerifySql(IFeatureClass feaLyr, string sql) { try { bool result; if (feaLyr.Search(new QueryFilterClass { WhereClause = sql }, false) == null) { result = false; return result; } result = true; return result; } catch (Exception ex) { //RdbUtil.AddException(ex); } return false; } } }