如何将文本框数组插入数据库?

时间:2015-03-24 08:35:14

标签: arrays database vb.net arraylist insert

我有一个带有String []数组行的视觉基本TextBox1(来自powershell脚本的输出),如此样本

3.50
12.59
21.34
31.20
3.80
12.72
21.60
33.43
3.21
12.08
21.30
33.02

我需要每4行拆分这些值并插入到数据库中

ResultA    ResultB ResultC ResultD
Test1   3.50    12.59   21.34   31.20

Test2 3.80 12.72 21.60 33.43

Test3 3.21 12.08 21.30 33.02

如果我在文本框16,20上有更多行,或者更多分割为4行并帮助我插入数据库表。 感谢

2 个答案:

答案 0 :(得分:0)

我不熟悉vb,但在C#中你可以这样做:

var yourArray = new string[40];
string resA = null, resB = null, resC = null, resD = null;

for(var i = 0; i<yourArray.Length; i+=4)
{
    resA = yourArray[i];
    if(i+1 < yourArray.Length)
        resB = yourArray[i+1];
    if(i+2 < yourArray.Length)
        resC = yourArray[i+2];
    if(i+3 < yourArray.Length)
        resD = yourArray[i+3];

    /// insert these values to db
}

答案 1 :(得分:0)

目前尚不清楚您是否知道如何在数据库中插入内容,或者您​​不知道如何使用VB.NET分割文本。我推测后者,否则问题太广泛,并没有包含足够的信息。

但是,我首先要实现一个自定义类来保存所有信息:

Public Class NumberResults
    Public Sub New(number As Int32, description As String, resA As Decimal, resB As Decimal, resC As Decimal, resD As Decimal)
        Me.Number = number
        Me.Description = description
        Me.ResultA = resA
        Me.ResultB = resB
        Me.ResultC = resC
        Me.ResultD = resD
    End Sub

    Public Property Number As Int32
    Public Property Description As String
    Public Property ResultA As Decimal
    Public Property ResultB As Decimal
    Public Property ResultC As Decimal
    Public Property ResultD As Decimal

    Public Overrides Function ToString() As String
        Return String.Format("{0} {1} {2} {3} {4}", Description, ResultA, ResultB, ResultC, ResultD)
    End Function
End Class

然后您可以使用LINQ选择解析为十进制的每一组四行。如果在LINQ查询中使用TryParse方法,则应使用返回Nullable(Of T)的辅助方法,这比使用局部变量更好:

你可以这些:

Module StringExtensions
    <Extension()>
    Public Function TryGetInt32(Str As String) As Nullable(Of Int32)
        If Str Is Nothing Then Return Nothing
        Dim num As Int32
        If Int32.TryParse(Str, num) Then Return num
        Return Nothing
    End Function

    <Extension()>
    Public Function TryGetIntDecimal(Str As String, Optional provider As IFormatProvider = Nothing) As Nullable(Of Decimal)
        If Str Is Nothing Then Return Nothing
        Dim num As Decimal
        If provider Is Nothing Then provider = Globalization.NumberFormatInfo.CurrentInfo
        If Decimal.TryParse(Str, Globalization.NumberStyles.Any, provider, num) Then Return num
        Return Nothing
    End Function
End Module

现在,您可以通过这种方式从文本框中获取所有可以解析为Decimal的行:

Dim numberLines = From line In TextBox1.Lines
                  Let numOrNull = line.TryGetIntDecimal(CultureInfo.InvariantCulture)
                  Where numOrNull.HasValue
                  Select numOrNull.Value

此LINQ查询被延迟执行,这意味着当前它是一个无操作。 您可以查看调试器中的值,它包含所有12行作为小数。现在我在这个查询中使用它,它使用\= integer division operator

进行分组
Dim results = numberLines.
    Select(Function(num, index) New With {.num = num, .index = index}).
    GroupBy(Function(x) x.index \ 4).
    Select(Function(grp, ix) New NumberResults(
               ix + 1, String.Format("Test{0}", ix + 1),
               grp.Select(Function(x) x.num).First(),
               grp.Select(Function(x) x.num).ElementAtOrDefault(1),
               grp.Select(Function(x) x.num).ElementAtOrDefault(2),
               grp.Select(Function(x) x.num).ElementAtOrDefault(3)))

查询仍未执行,因此您可以使用For Each将它们插入数据库中:

For Each res In results
    Console.WriteLine(res.ToString())
Next

输出(我使用逗号作为小数分隔符):

Test1 3,50 12,59 21,34 31,20
Test2 3,80 12,72 21,60 33,43
Test3 3,21 12,08 21,30 33,02