好久没写东西了,也没什么好写的,只好拿一个小函数来充数:(
type
TConnInfo = record
ServerName: String;
UserName: String;
PassWord: String;
end;
function GetServerNameUserNameAndPassWord(ConnString: String):TConnInfo;
//549@10:38 2005-9-7
const
StrServerName = 'Data Source=';
StrUserName = 'User Id=';
StrPassword = 'Password=';
var
InQuotes: Boolean;//是否处于""中
PriorIsQuoteBegin: Boolean;//上一个字符是两个连续"的第一个
CurPos: Integer; //当前位置
TmpList: TStrings;//临时列表
StrLen: Integer; //连接字符串长度
begin
InQuotes := False;
PriorIsQuoteBegin := False;
StrLen := Length(ConnString);
TmpList := TStringList.Create;
try
TmpList.Add('');
for CurPos := 1 to StrLen do begin
if ConnString[CurPos] = '"' then begin
if not Inquotes then begin
InQuotes := True; //第一个"
PriorIsQuoteBegin := False;
end
else begin
//不是第一个",那么如果后面有内容,这个"后面必然带一个"或者;
if CurPos = StrLen then Break;
if PriorIsQuoteBegin then begin
TmpList[TmpList.Count - 1] :=
TmpList[TmpList.Count - 1] + ConnString[CurPos];
PriorIsQuoteBegin := False;
end
else begin
if ConnString[CurPos + 1] = '"' then begin
PriorIsQuoteBegin := True;
end;
end;
end;
end
else begin
if ConnString[CurPos] = ';' then begin
if (not PriorIsQuoteBegin) and (ConnString[CurPos - 1] = '"')then
InQuotes := False;
if InQuotes then
TmpList[TmpList.Count - 1] :=
TmpList[TmpList.Count - 1] + ConnString[CurPos]
else begin
TmpList.Add('');
end;
end
else
TmpList[TmpList.Count - 1] :=
TmpList[TmpList.Count - 1] + ConnString[CurPos];
PriorIsQuoteBegin := False;
end;
end;
finally
Result.ServerName := GetStrByLeftHeader(StrServerName, TmpList, False);
Result.ServerName :=
RightStr(Result.ServerName,
Length(Result.ServerName) - Length(StrServerName));
Result.UserName := GetStrByLeftHeader(StrUserName, TmpList, False);
Result.UserName :=
RightStr(Result.UserName,
Length(Result.UserName) - Length(StrUserName));
Result.PassWord := GetStrByLeftHeader(StrPassWord, TmpList, False);
Result.PassWord :=
RightStr(Result.PassWord,
Length(Result.PassWord) - Length(StrPassWord));
// ShowMessage(Result.ServerName+CRLF+Result.UserName+CRLF+Result.PassWord);
TmpList.Free;
end;
end;
function GetStrByLeftHeader(Str: String; List: TStrings;
CaseSensitive: Boolean = True): String;
var
i: Integer;
begin
Result := '';
for i := 0 to List.Count - 1 do begin
if CaseSensitive then begin
if LeftStr(List[i], Length(Str)) = Str then begin
Result := List[i];
Break;
end;
end
else begin
if UpperCase(LeftStr(List[i], Length(Str))) = UpperCase(Str) then begin
Result := List[i];
Break;
end;
end;
end;
end;