• VB.NET 调用金蝶登录,并解析PropsString


    本篇是上一篇的姊妹篇,使用VB.NET实现,这有点类似.NET 4.0之后,C#的Dynamic方式实现,具体看代码,调用方式与上一篇类似:

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    
    Namespace System
        Public Class KingdeeLogin
            Private _kindeeLoginObject As Object
            Private _sqlConnectionString As String
            Private _accountName As String
            Private _userName As String
            Private _userId As Integer
    
            Public Sub New()
                _kindeeLoginObject = CreateObject("K3Login.ClsLogin")
            End Sub
    
            Public Function CheckLogin() As Boolean
                Dim result As Boolean = _kindeeLoginObject.CheckLogin
                If result Then
                    InitialLoginDatas()
                End If
                Return result
            End Function
    
    
    #Region "解析金蝶的PropsString"
    
            Private Sub InitialLoginDatas()
                Dim kingdeePropsString As String = _kindeeLoginObject.PropsString
                _accountName = _kindeeLoginObject.AcctName
                ParsePropsStringToField(kingdeePropsString)
            End Sub
    
            Private Sub ParsePropsStringToField(ByVal propsString As String)
                Dim datas = ReadPropsStringToDictinary(propsString)
                _userName = GetValueFromDictionary("UserName", datas)
                _userId = CInt(GetValueFromDictionary("UserID", datas))
                _sqlConnectionString = ConvertSqlConnectionString(GetValueFromDictionary("ConnectString", datas))
                '在这里大家可以继续扩展哦
            End Sub
    
            Private Function ConvertSqlConnectionString(ByVal oleDbConnectionString As String) As String
                Dim oleBuilder As New OleDbConnectionStringBuilder(oleDbConnectionString)
                Dim sqlBuilder As New SqlConnectionStringBuilder
                sqlBuilder.UserID = oleBuilder("User ID").ToString()
                sqlBuilder.Password = oleBuilder("Password").ToString()
                sqlBuilder.DataSource = oleBuilder("Data Source").ToString()
                sqlBuilder.InitialCatalog = oleBuilder("Initial Catalog").ToString()
    
                Dim IntegratedSecurity As New Object
                If oleBuilder.TryGetValue("Integrated Security", IntegratedSecurity) Then
                    If IntegratedSecurity.ToString().ToUpper() = "SSPI" Then
                        sqlBuilder.IntegratedSecurity = True
                    End If
                Else
                    sqlBuilder.IntegratedSecurity = False
                End If
    
                Return sqlBuilder.ConnectionString
            End Function
    
            Private Function ReadPropsStringToDictinary(ByVal propsString As String) As Dictionary(Of String, String)
                Dim result As New Dictionary(Of String, String)
                Dim curIndex As Integer = 0
    
                While curIndex < propsString.Length
                    curIndex += ReadSection(result, propsString)
                End While
                Return result
            End Function
    
            Private Function ReadSection(ByVal sections As Dictionary(Of String, String), ByVal source As String) As Integer
                Dim lengthResult As Integer = 0
                Dim key As String = ""
                Dim value As String = ""
    
                lengthResult += ReadKey(source, key)
                lengthResult += ReadValue(source.Substring(lengthResult), value)
                sections.Add(key, value)
                Return lengthResult
    
            End Function
    
            Private Function ReadKey(ByVal source As String, ByRef key As String) As Integer
                key = ""
                Dim lengthResult As Integer = 0
                For i As Integer = 0 To source.Length
                    lengthResult = i + 1
                    If source(i) = "=" Then
                        Exit For
                    Else
                        key += source(i)
                    End If
                Next
    
                Return lengthResult
            End Function
    
            Private Function ReadValue(ByVal source As String, ByRef value As String)
                value = ""
                Dim lengthResult As Integer = 0
                Dim charStack As New Stack(Of Char)
    
                For i As Integer = 0 To source.Length
                    Dim c = source(i)
                    lengthResult += i + 1
    
                    If c = "{" Then
                        If charStack.Count > 0 Then value += c
                        charStack.Push(c)
                    ElseIf c = "}" Then
                        charStack.Pop()
                        If charStack.Count > 0 Then value += c
                    ElseIf c = ";" Then
                        If charStack.Count > 0 Then value += c Else Exit For
                    Else
                        value += c
                    End If
                Next
                Return lengthResult
            End Function
    
            Private Function GetValueFromDictionary(ByVal key As String, ByVal source As Dictionary(Of String, String)) As String
                Dim dicKey As String = ""
                For Each sKey As String In source.Keys
                    If key.Trim.ToUpper = sKey.Trim.ToUpper Then
                        dicKey = sKey
                        Exit For
                    End If
                Next
    
                If (String.IsNullOrEmpty(dicKey)) Then Return String.Empty
                Return source(dicKey)
            End Function
    
    #End Region
    
            Public ReadOnly Property SqlConnectionString() As String
                Get
                    Return _sqlConnectionString
                End Get
            End Property
    
            Public ReadOnly Property AccountName() As String
                Get
                    Return _accountName
                End Get
            End Property
    
            Public ReadOnly Property UserName() As String
                Get
                    Return _userName
                End Get
            End Property
    
            Public ReadOnly Property UserID() As Integer
                Get
                    Return _userId
                End Get
            End Property
    
        End Class
    End Namespace
    希望可以帮助到使用VB.NET为金蝶做二次开发的人。
    作者:Sun.M
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    剑指Offer——翻转单词顺序列
    剑指Offer——左旋转字符串
    剑指Offer——和为S的两个数字
    剑指Offer——和为S的连续正数序列
    剑指Offer——数组中只出现一次的数字
    log4cxx入门第一篇--一个小例子
    gsoap写一个c++ webservice
    Protocol Buffer技术详解(数据编码)
    Protocol Buffer技术详解(Java实例)
    Protocol Buffer技术详解(C++实例)
  • 原文地址:https://www.cnblogs.com/prinsun/p/2636801.html
Copyright © 2020-2023  润新知