SQL查询条件生成小工具
Posted yanghucheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL查询条件生成小工具相关的知识,希望对你有一定的参考价值。
最近运维数据,经常遇到需要在sql条件中个In(‘‘,‘‘....)个字符串的情况,于是在网上找了个小工具改造一下,先用着;
效果如图:
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using Newtonsoft.Json.Serialization; 8 using Newtonsoft.Json.Linq; 9 using Newtonsoft.Json.Converters; 10 using Newtonsoft.Json.Utilities; 11 using Newtonsoft.Json; 12 13 namespace ConsolePro 14 15 public class FileHelper 16 17 #region txt \\r\\n 给txt 文本 批量加‘,‘ 作为sql查询条件 18 public static void CreateSQLContidion() 19 20 var filePath = "D:\\\\rt.txt"; 21 int TotalCountInEveryFile = 10000; 22 List<string> gotStrings = GetStreamMethod(filePath); 23 //Console.WriteLine(string.Join("\\n", gotStrings.ToArray())); 24 if (gotStrings != null) 25 26 int fileCount = 1; 27 for (int i = 0; i < gotStrings.Count; i++) 28 29 StringBuilder sb = new StringBuilder(); 30 sb.AppendFormat("0\\r\\n", ("‘" + gotStrings[i].Trim() + "‘,")); 31 if (i + 1 < gotStrings.Count) 32 33 i++; 34 35 else 36 37 WriteStreamMethod(string.Format("d:\\\\Document0.txt", fileCount), sb.ToString()); 38 return; 39 40 while (i % TotalCountInEveryFile != 0) 41 42 sb.AppendFormat("0\\r\\n", ("‘" + gotStrings[i].Trim() + "‘,")); 43 if (i != gotStrings.Count - 1) 44 45 i++; 46 47 else 48 49 WriteStreamMethod(string.Format("d:\\\\Document0.txt", fileCount), sb.ToString()); 50 return; 51 52 53 //Console.WriteLine(sb.ToString()); 54 WriteStreamMethod(string.Format("d:\\\\Document0.txt", fileCount), sb.ToString()); 55 if (i % TotalCountInEveryFile == 0) 56 57 i--; 58 59 fileCount++; 60 61 62 63 #endregion 64 65 public static List<string> GetStreamMethod(string path) 66 67 List<string> list = new List<string>(); 68 StreamReader sr = new StreamReader(path); 69 String line; 70 while ((line = sr.ReadLine()) != null) 71 72 list.Add(line.ToString()); 73 74 return list; 75 76 77 public static string GetStrMethod(string path) 78 79 StringBuilder list = new StringBuilder(); 80 StreamReader sr = new StreamReader(path); 81 String line; 82 while ((line = sr.ReadLine()) != null) 83 84 list.Append(line.ToString()); 85 86 return list.ToString(); 87 88 89 public static void WriteStreamMethod(string path, string content) 90 91 FileStream fs = new FileStream(path, FileMode.Create); 92 StreamWriter sw = new StreamWriter(fs); 93 sw.Write(content); 94 sw.Flush(); 95 sw.Close(); 96 fs.Close(); 97 98 99 public static string GetCustomNo() 100 101 var filePath = "D:\\\\cus.txt"; 102 string gotStrings = GetStrMethod(filePath); 103 var jsonParse = gotStrings.ToString(); 104 //JObject JsonObj = JObject.Parse(jsonParse); 105 JArray list = JArray.Parse(jsonParse); 106 IList<JToken> delList = new List<JToken>(); 107 List<string> tempStr = new List<string>(); 108 StringBuilder s = new StringBuilder(); 109 foreach (var ss in list) //查找某个字段与值 110 111 if (((JObject)ss)["CustosNo"].ToString() != "aa") 112 //tempStr.Add(((JObject)ss)["CustosNo"].ToString()); 113 s.Append("‘"+((JObject)ss)["CustosNo"].ToString()+"‘,"); 114 //delList.Add(ss); 115 116 117 //var purchaseInfoes = JsonConvert.DeserializeObject<string>(JsonObj["CustosNo"].ToString()); 118 return s.ToString().TrimEnd(‘,‘); 119 120 121 122
以上是关于SQL查询条件生成小工具的主要内容,如果未能解决你的问题,请参考以下文章