得到ConnectionString中的特定信息

好久没写东西了,也没什么好写的,只好拿一个小函数来充数:(

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;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值