『楼 主』:
[已结贴]分解数据库连接字符串?
已知某个目录下有数个文本文件,每个文件的内容是sqlserver数据库连接字符串,内容类似于这样:
Provider=SQLOLEDB;Server=localhost;Database=xx;UID=xxxx;PWD=xxxxxx 我们发现是5个用分号隔开的字符串,且有点文件还有一个“;Persist Security Info=True”这样的字符串,加起来就是6个。
问题的难点
1.在Provider,Server,Database,UID,PWD这几个分号(;)隔开的数据顺序是变动的,也即是Provider=SQLOLEDB可以在Server=localhost的后面等等。
2.有的文件用Data Source变量代替Server变量
有的文件用User ID变量替代UID变量
有的文件用password代替PWD。
有的文件用Initial Catalog替代database
多给点实例:
Provider=SQLOLEDB.1;Password=xx;Persist Security Info=True;User ID=xx;Initial Catalog=xx;Data Source=(local)
Provider=SQLOLEDB.1;Password=xxxx;Persist Security Info=True;User ID=xxxx;Initial Catalog=xxxxx;Data Source=. 求:如果通过批处理,将所需要的Server(Data source)、UID(user id)、PWD(password)提取到一个文件,格式为:
server1 uid1 pwd1
server2 uid2 pwd2
server3 uid3 pwd3 这个是我写的,当然肯定是错的:)
@echo off
for /f "usebackq" %%a in (`dir C:\Users\Administrator\Desktop\ConnectString4Web /s /b /a-d`) DO (
for /f "delims=; tokens=1,2,3,4,5,6" %%b in (%%a) DO (
for /f "delims== tokens=1,2,3,4,5,6,7,8,9,10,11,12" %%h in ("%%b=%%c=%%d=%%e=%%f=%%g") DO echo %%i %%k %%m %%o %%p %%s
)
) 自己搞定,可用代码:
@echo off
rem tunpishuang[http://tunps.com]
rem 连接字符串格式:
rem provider password security "user id" "initial catalog" "data source"
rem provider server database uid pwd
rem 211条记录
setlocal enabledelayedexpansion
set num=1
for /f "usebackq" %%a in (`dir C:\Users\Administrator\Desktop\ConnectString4Web /s /b /a-d`) DO (
for /f "delims=; tokens=1,2,3,4,5,6" %%b in (%%a) DO (
for /f "delims== tokens=1,2,3,4,5,6,7,8,9,10,11,12" %%h in ("%%b=%%c=%%d=%%e=%%f=%%g") DO (
set /a num=!num!+1
rem echo [%%h %%i] [%%j %%k] [%%l %%m] [%%n %%o] [%%p %%q] [%%r %%s] !num!
if "%%n"=="UID" echo www.xxx.com %%o %%q >>conn.txt
if "%%n"=="User ID" echo www.xxx.com %%o %%k >>conn.txt
rem if "%%j"=="Password" echo %%k
rem if "%%p"=="PWD" echo %%q
rem echo !num!
)
)
)
endlocal [ Last edited by tunpishuang on 2010-7-2 at 12:59 ]
|