已保存 SQL 服务器列表的 VB.Net My.Settings 自定义类型 [英] VB.Net My.Settings custom type for list of saved SQL servers

查看:26
本文介绍了已保存 SQL 服务器列表的 VB.Net My.Settings 自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 My.Settings 保存经过身份验证的服务器及其信息的列表,但我无法弄清楚如何制作我的自定义类型.

I am trying to use My.Settings to save a list of authenticated servers and their information, but I am having trouble figuring out how to make my custom type.

我现在有一个名为 SqlServer 的类,它具有以下三个字段:

I have a class right now called SqlServer that has these three fields:

 Public ServerName As String
 Public UserName As String
 Public Password As String

每次我使用 SQL 身份验证连接到 SQL 服务器时,我都想保存该服务器和登录信息.这意味着我需要一个自定义类型,它是 My.Settings 中 SqlServer 的集合.

Each time I connect to a SQL server using SQL Authentication, I want to save that server and the login information. This means that I need a custom type that is a collection of SqlServer's in My.Settings.

这是我到目前为止的代码:

This is the code I have so far:

Public Class SQLServerList
  Inherits List(Of SQLServer)
  Implements IComparable(Of SQLServer)

  Public Function CompareTo(ByVal SqlServerInfo As SQLServer) As Integer Implements IComparable(Of MyProjectName.SQLServer).CompareTo
    ...
  End Function
End Class

我通过继承 List 是否走在正确的轨道上?我需要什么样的属性/字段才能让这个东西按我想要的方式运行?谢谢.

Am I on the right track here by inheriting List? And what kinds of properties/fields will I need in order to get this thing to function as I want it to? Thank you.

推荐答案

给定一个集合类,很容易让它自己序列化内容.

Given a collection class, it is pretty easy to have it serialize the contents itself.

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

<Serializable>
Public Class Server
    Public Property Name As String
    Public Property UserName As String
    Public Property Password As String
End Class

一个简单的集合类:

<Serializable>
Public Class Servers
    Private myList As List(Of Server)

    Public Sub New()
        myList = New List(Of Server)
    End Sub

    Public Sub Add(svr As Server)
        myList.Add(svr)
    End Sub

    ' no reason it cant also create server objects for you
    Public Sub Add(sname As String, uname As String, pw As String)
        myList.Add(New Server With {.Name = sname, .UserName = uname, .Password = pw})
    End Sub

    'toDo Contains, Count, Item etc as needed

    Public Sub Save(mypath As String)
        Using fs As New FileStream(mypath, FileMode.OpenOrCreate)
            Dim bf As New BinaryFormatter
            bf.Serialize(fs, myList)
        End Using
    End Sub

    Public Function Load(mypath As String) As Int32
        'ToDo: check if file exists
        Using fs As New FileStream(mypath, FileMode.Open)
            Dim bf As New BinaryFormatter
            myList = CType(bf.Deserialize(fs), List(Of Server))
        End Using

        If myList IsNot Nothing Then
            Return myList.Count
        Else
            Return 0
        End If
    End Function
End Class

属性是 BinaryFormatter 所必需的.不同的序列化程序(json、ProtoBuf-NET)可能有自己的.

The <Serializable> attribute is required for the BinaryFormatter. Different serializers (json, ProtoBuf-NET) may have their own.

魔法"在 SaveLoad 方法中,它们序列化或反序列化对象的内部列表.只需几行代码即可加载/保存 1 或 1000 个项目.对于少量数据,它是替代数据库的绝佳选择.

The "magic" is in the Save and Load methods which serialize or deserialize the internal list of objects. Just a few lines of code to load/save 1 or 1000 items. Its a great alternative to a database for small amounts of data.

测试往返:

    Dim svrs As New Servers
    svrs.Add("SqlSS1", "ziggy", "foo")
    svrs.Add("SqlSS2", "zacky", "bar")
    svrs.Add("SqlSS3", "zoey", "baz")

    svrs.Save("C:\Temp\SSvrs.bin")

    ' now load to a new Servers collection
    ' to test the round trip
    Dim svrs2 As New Servers
    Dim sCount = svrs2.Load("C:\Temp\SSvrs.bin")

    For n = 0 To sCount  - 1
        Console.WriteLine("{0}  {1}   {2} ", svrs2(n).Name, svrs2(n).UserName, svrs2(n).Password)
    Next

输出:

SqlSS1 ziggy foo
SqlSS2 zacky 吧
SqlSS3 佐伊巴兹

SqlSS1 ziggy foo
SqlSS2 zacky bar
SqlSS3 zoey baz

我使用了 BinaryFormatter(没有真正的原因),但 ProtoBuf-NET 或 XMLSerializer 也可以使用.由于数据都是字符串,您可能需要对其进行加密(将文件流包装在加密流中).

I used the BinaryFormatter (for no real reason) but ProtoBuf-NET or XMLSerializer will also work. Since the data is all string, you may want to encrypt them (wrap the filestream in a cryptostream).

这篇关于已保存 SQL 服务器列表的 VB.Net My.Settings 自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆