简体   繁体   English

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

[英]how can i insert textbox array to database?

I have a visual basic TextBox1 with String[] Array lines (output from powershell script)like this sample 我有一个带有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

I need split these values each 4 line and insert into database like 我需要将这些值每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

also if I have more lines on textbox 16,20 or more split to 4 lines and help me to insert into database table. 另外,如果我在文本框上有更多行16,20或更多,请分成4行,并帮助我插入数据库表中。 thanks 谢谢

I'm not familiar with vb but in C# you can do this: 我不熟悉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
}

It's not clear whether you don't know how to insert something into your database or you don't know how to split the text with VB.NET. 目前尚不清楚您是否不知道如何在数据库中插入某些内容,还是不知道如何使用VB.NET拆分文本。 I presume the latter, otherwise the question is too broad and doesn't contain enough informations. 我认为是后者,否则问题就太广泛了,没有足够的信息。

However, i would first implement a custom class to hold all your informations: 但是,我首先实现一个自定义类来保存您的所有信息:

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

Then you can use LINQ to select every group of four lines parsed to decimal. 然后,您可以使用LINQ来选择解析为十进制的每组四行。 If you use a TryParse method in a LINQ query you should use a helper method that returns a Nullable(Of T) which is better than using a local variable: 如果在LINQ查询中使用TryParse方法,则应使用一个辅助方法,该方法返回一个Nullable(Of T) ,它比使用局部变量更好:

You could these: 您可以:

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

Now you get all lines from the textbox that can be parsed to Decimal in this way: 现在,您可以通过这种方式从文本框中获取所有行,这些行可以解析为Decimal

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

This LINQ query is deferred executed which means that currently it is a no-op. 延迟执行此LINQ查询,这意味着当前它是无操作的。 You could have a look at the values in the debugger, it contains all 12 lines as decimals. 您可以看一下调试器中的值,它包含所有12行作为小数。 Now i use it in this query which does the grouping by using the \\= 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)))

The queries are still not executed, therefore you could for example use a For Each to insert them into database: 这些查询仍未执行,因此您可以使用例如For Each将其插入数据库中:

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

which outputs (i use comma as decimal separator): 输出(我用逗号作为小数点分隔符):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM