0

我想为 LED 创建一个表。该表创建名称、中心波长和光谱等信息,其本身是格式强度超过波长的数据,为 2 xn 表数据。

我是初学者,目前不知道如何将其插入表格。我当然可以为每个 LED 单独创建一个表格,但是会有数百个这样的光谱数据。

4

2 回答 2

1

这种复杂的数据结构可能难以在数据库表中实现。我建议的一个选项是拥有一组代表数据的类。然后您可以将数据序列化和反序列化(读取和写入)到文件。

示例实现

Module Module1

    Sub Main()
        Dim leds = New List(Of LED)()
        Dim rnd = New Random()

        'create a bunch of LEDs
        For i = 1 To 10
            Dim led = New LED("LED " & (i + 1).ToString(), rnd.Next(0, i * 100))
            For x = 1 To 10
                led.Spectrum.Add(New SpectrumInfo(rnd.Next(1, 10), rnd.Next(1000, 10000)))
            Next

            leds.Add(led)
        Next


        ' write the led data to a file
        Using sw As New IO.StreamWriter("LED Data.ledx")
            Dim xs = New System.Xml.Serialization.XmlSerializer(leds.GetType())

            xs.Serialize(sw, leds)
        End Using


        'read the led data from a file
        Dim leds2 = New List(Of LED)()
        Using sr = New System.IO.StreamReader("LED Data.ledx")
            Dim xs = New System.Xml.Serialization.XmlSerializer(leds2.GetType())

            leds2 = DirectCast(xs.Deserialize(sr), List(Of LED))
        End Using


        'confirm the two are the same
        Console.WriteLine("LEDs and LEDS2 are " & If(leds.SequenceEqual(leds2), "the same", "different"))


        ' alternate saving using binary serializer 
        ' works in cases where XmlSerializer doesn't
        ' produces smaller files too


        'save the led data
        Using fs = New System.IO.FileStream("LED Data.ledb", IO.FileMode.Create)
            Dim bf = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            bf.Serialize(fs, leds)
        End Using

        'read the led data
        Dim leds3 = New List(Of LED)()
        Using fs = New System.IO.FileStream("LED Data.ledb", IO.FileMode.Open)
            Dim bf = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            leds3 = DirectCast(bf.Deserialize(fs), List(Of LED))
        End Using

        'confirm equality
        Console.WriteLine("LEDs and LEDS3 are " & If(leds.SequenceEqual(leds3), "the same", "different"))
        Console.WriteLine("LEDs2 and LEDS3 are " & If(leds2.SequenceEqual(leds3), "the same", "different"))


        Console.ReadLine()
    End Sub

End Module

<Serializable()> _
Public Class LED
    Dim _name As String
    Dim _cWL As Double
    Dim _spectrum As List(Of SpectrumInfo)


    Public Sub New()
        _name = String.Empty
        _cWL = 0
        _spectrum = New List(Of SpectrumInfo)()
    End Sub

    Public Sub New(name As String, cwl As Double, ParamArray spectrum() As SpectrumInfo)
        _name = name
        _cWL = cwl
        _spectrum = New List(Of SpectrumInfo)(spectrum)
    End Sub


    Public Property Name As String
        Get
            Return _name
        End Get
        Set(value As String)
            _name = value
        End Set
    End Property

    Public Property CenterWavelength As Double
        Get
            Return _cWL
        End Get
        Set(value As Double)
            _cWL = value
        End Set
    End Property

    Public ReadOnly Property Spectrum As List(Of SpectrumInfo)
        Get
            Return _spectrum
        End Get
    End Property


    Public Overrides Function Equals(obj As Object) As Boolean
        If Not (TypeOf obj Is LED) Then Return False

        Dim l2 = DirectCast(obj, LED)

        Return l2._name = _name AndAlso l2._cWL = _cWL AndAlso l2._spectrum.SequenceEqual(_spectrum)
    End Function

    Public Overrides Function ToString() As String
        Return String.Format("{0} [{1}]", _name, _cWL)
    End Function

    Public Overrides Function GetHashCode() As Integer
        Dim result As Integer

        For Each spec In _spectrum
            result = result Xor spec.GetHashCode()
        Next

        Return result Xor (_name.GetHashCode() + _cWL.GetHashCode())
    End Function
End Class


<Serializable()> _
Public Structure SpectrumInfo
    Dim _intensity As Double
    Dim _wavelength As Double

    Public Sub New(intensity As Double, wavelength As Double)
        _intensity = intensity
        _wavelength = wavelength
    End Sub


    Public ReadOnly Property Intensity As Double
        Get
            Return _intensity
        End Get
    End Property

    Public ReadOnly Property Wavelength As Double
        Get
            Return _wavelength
        End Get
    End Property


    Public Overrides Function Equals(obj As Object) As Boolean
        If TypeOf obj Is SpectrumInfo Then
            Dim si = DirectCast(obj, SpectrumInfo)
            Return si._wavelength = _wavelength AndAlso si._intensity = _intensity
        Else
            Return False
        End If
    End Function

    Public Overrides Function ToString() As String
        Return String.Format("Intensity: {0}, Wavelength: {1}", _intensity, _wavelength)
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return _intensity.GetHashCode() Xor _wavelength.GetHashCode()
    End Function
End Structure
于 2013-02-19T10:53:47.740 回答
0

You might look at http://r937.com/relational.html

I think you want:

LED Table

ID
LEDName
CenterWavelength

And then a table for spectra

ID
LedId
Intensisty
WaveLength
于 2013-02-19T19:16:15.100 回答