【问题标题】:how can i insert textbox array to database?如何将文本框数组插入数据库?
【发布时间】:2015-03-24 08:35:14
【问题描述】:

我有一个带有 String[] 数组行(从 powershell 脚本输出)的 Visual Basic TextBox1,就像这个示例一样

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 行并插入到数据库中

结果A 结果B 结果C 结果D
测试1 3.50 12.59 21.34 31.20

测试2 3.80 12.72 21.60 33.43

测试3 3.21 12.08 21.30 33.02

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

【问题讨论】:

    标签: arrays database vb.net arraylist insert


    【解决方案1】:

    我不熟悉 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
    }
    

    【讨论】:

      【解决方案2】:

      不清楚您是否不知道如何将某些内容插入到您的数据库中,或者您不知道如何使用 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
      

      【讨论】:

        猜你喜欢
        • 2011-05-05
        • 1970-01-01
        • 2013-03-27
        • 2012-01-20
        • 1970-01-01
        • 2023-03-17
        • 2012-03-02
        • 2021-10-29
        • 2015-03-10
        相关资源
        最近更新 更多