栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何最好地在SQL Server存储过程中找到硬编码的英语字符串?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何最好地在SQL Server存储过程中找到硬编码的英语字符串?

另一个想法:Microsoft通过Visual
Studio提供了可以解析SQL的程序集。我已经用过了,而且使用起来很简单。您也许可以使用它来解析存储过程的文本;它可以返回语句中各种标记的列表,包括标记的类型。因此,它应该能够帮助您区分您可能会感兴趣的文本字符串与注释中哪些内容可以忽略。

基本上,从.NET,您将打开与数据库的连接,并查询syscomments以获取存储过程的文本。您将遍历每个过程,并使用这些解析器对其进行解析。然后,您将使用Sql100scriptGenerator从解析的文本中获取标记,遍历标记并查找其类型为ASCII或Unipre字符串文字的标记。对于那些字符串,请检查它们的长度以查看其长度是否超过20,如果是20,则将这些字符串和proc标记为需要进一步检查。

我玩了一下,这是一个 非常 原始的示例来说明基本原理:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;using Microsoft.Data.Schema;using Microsoft.Data.Schema.scriptDom;using Microsoft.Data.Schema.scriptDom.Sql;namespace FindHardCodedStrings{    class Program    {        static void Main(string[] args)        { using (SqlConnection conn = new SqlConnection()) {     SqlConnectionStringBuilder bldr = new SqlConnectionStringBuilder();     bldr.DataSource = "localhost\sqlexpress";     bldr.InitialCatalog = "msdb";     bldr.IntegratedSecurity = true;     conn.ConnectionString = bldr.ConnectionString;     SqlCommand cmd = conn.CreateCommand();     cmd.CommandType = System.Data.CommandType.Text;     cmd.CommandText = "select [text] from syscomments";     SqlDataAdapter da = new SqlDataAdapter(cmd);     DataSet ds = new DataSet();     da.Fill(ds);     TSql100Parser parser = new TSql100Parser(false);     Sql100scriptGenerator gen = new Sql100scriptGenerator();     gen.Options.SqlVersion = SqlVersion.Sql100;     foreach (DataRow proc in ds.Tables[0].Rows)     {         string txt = proc[0].ToString();         using (System.IO.TextReader sr = new System.IO.StringReader(txt))         {  IList<ParseError> errs;  IscriptFragment frag = parser.Parse(sr, out errs);  if (null == frag)      continue;  IList<TSqlParserToken> tokens = gen.GenerateTokens((TSqlFragment)frag);  foreach (TSqlParserToken token in tokens)  {      if (token.TokenType == TSqlTokenType.UnipreStringLiteral || token.TokenType == TSqlTokenType.AsciiStringLiteral)      {          if (token.Text.Length >= 20)   Console.WriteLine("String found: " + token.Text);      }  }         }     } }        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号