惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
博客园 - 叶小钗
AI
AI
T
Tor Project blog
Forbes - Security
Forbes - Security
W
WeLiveSecurity
博客园_首页
爱范儿
爱范儿
J
Java Code Geeks
B
Blog
G
GRAHAM CLULEY
aimingoo的专栏
aimingoo的专栏
Cloudbric
Cloudbric
C
CXSECURITY Database RSS Feed - CXSecurity.com
TaoSecurity Blog
TaoSecurity Blog
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
有赞技术团队
有赞技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
H
Help Net Security
博客园 - 三生石上(FineUI控件)
C
Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 司徒正美
The Last Watchdog
The Last Watchdog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
S
Secure Thoughts
Spread Privacy
Spread Privacy
F
Fortinet All Blogs
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Security Latest
Security Latest
Webroot Blog
Webroot Blog
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog

博客园 - 钟少

关于 MonoDevelop on Linux 单步调试问题的解决 MonoDevelop 4.2.2/Mono 3.4.0 in CentOS 6.5 安装笔记 在ASP.NET MVC 4 on Mono中使用OracleClient in CentOS 6.x的问题记录 在CentOS 6.4 x86_32中使用Rhythmbox听MP3 MonoDevelop 4.0.9 on CentOS 6.3 安装笔记 MemoryMappedFile 在 Mono in Linux 的开发笔记 Mono on CentOS 6.3 安装笔记 在OpenSUSE中听歌 ASP.NET MVC 3.0 源码阅读手记(1) Mono on Linux 开发与实践札记(1) 在Windows Mobile中检测应用程序是否运行在模拟器中 我的WPF学习札记(1) 我的插件框架·前传 恢复Ico图标文件在资源管理器中的显示 深入剖析 ASP.NET 1.x 中 Forms 身份验证(1) 北大青鸟(深圳中青)培训中心招聘: .NET 讲师 .NET 2.0 中的自定义配置处理 一个三层架构的WinForms程序的完整范例(.NET 1.1/Northwind) IIS6.0服务器架站无法访问解决方案总结(转贴)
数据库连接字符串解析的正则表达式
钟少 · 2010-11-11 · via 博客园 - 钟少

最近在写一个Windows Mobile的小程序,其中需要访问数据库,数据库连接字符串大致如下:

Data Source=Zongsoft.MAS.sdf;Password=xxxxxx;Persist Security Info=True

其中的Data Source部分指定了数据库文件的名称,但是当使用DbConnection.Open()方法进行连接时提示找不到数据库文件,经查实发现其必须指定为数据库文件的完整路径。——这是个麻烦事,因为手机程序部署的物理位置并不能确定,故此无法在配置文件中指定数据库文件的完整路径

首先,我想到用DbConnectionStringBuilder来动态生成连接字符串,但是,在.NET CompactFramework 3.5中并不支持该类。退而其次发现可以使用DbConnection.ChangeDatabase()方法来修改数据库路径,但是,经调试发现该方法只能在DbConnection.StateOpen状态下操作,因为架构设计原因我拒绝这种野蛮方式,最后确定使用正则表达式进行动态替换,翻了下资料写出如下Regx:

(?<=Data\s+Source\s*=\s*['"]*\s*)\w+[^;'"]+(?=\b)

注:如果在C#中,请将上述表达式中的双引号(")写双份,以进行字符转义。

好吧,下面给出该方法的完整代码(C#):

   1: /// <summary>
   2: /// 修复连接字符串,将数据库文件相对路径的转换为绝对路径。
   3: /// </summary>
   4: /// <param name="connectionString">连接字符串的值。</param>
   5: /// <returns>返回被修整过的连接字符串,如果无需修整则返回参数原值。</returns>
   6: private static string FixConnectionString(string connectionString)
   7: {
   8:     const string DATASOURCE_PATTERN = @"(?<=Data\s+Source\s*=\s*['""]*\s*)\w+[^;'""]+(?=\b)";
   9:  
  10:     if(string.IsNullOrEmpty(connectionString))
  11:         return connectionString;
  12:  
  13:     Match match = Regex.Match(connectionString, DATASOURCE_PATTERN, RegexOptions.IgnoreCase | RegexOptions.Singleline);
  14:     if(!match.Success)
  15:         return connectionString;
  16:  
  17:     if(Path.IsPathRooted(match.Value))
  18:         return connectionString;
  19:  
  20:     string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
  21:     string dataSource = Path.Combine(directoryName, match.Value);
  22:  
  23:     return Regex.Replace(connectionString, DATASOURCE_PATTERN, dataSource, RegexOptions.IgnoreCase | RegexOptions.Singleline);
  24: }