我有这个程序,你可以制作一系列动作。这些操作存储在本地SQLCe
数据库中(一旦它工作,它将在真实的SQL Server
中),并且每个操作可以有多个参数。这段代码目前有效,但我想知道用循环或锯齿状数组缩小它的最佳方法,这是我无法弄清楚的。
以下是代码:
Imports System.Data.SqlServerCe
Imports System.Text
Imports Microsoft.Win32
Public Class Form2
Dim conn As SqlCeConnection = New SqlCeConnection("Data Source=Database.sdf")
Public Function Split(ByVal source As String) As Array
Dim results() As String
results = source.Split(",")
Return results
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim indexname As String = "Jagged Test"
Dim scriptrestriction As String = ""
Dim scriptid As String = ""
Dim scripttype As String = ""
Dim tempresult As String = ""
Dim Arg0 As New List(Of String)()
Dim Arg1 As New List(Of String)()
Dim Arg2 As New List(Of String)()
Dim Arg3 As New List(Of String)()
Dim Arg4 As New List(Of String)()
Dim Arg5 As New List(Of String)()
Dim Arg6 As New List(Of String)()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim sql As New SqlCeCommand("SELECT * FROM ScriptInfo WHERE Name = '" & indexname & "'", conn)
Dim sdr As SqlCeDataReader = sql.ExecuteReader()
While sdr.Read = True
If Not IsDBNull(sdr.Item("Restriction")) Then
scriptrestriction = sdr.Item("Restriction")
End If
If Not IsDBNull(sdr.Item("ID")) Then
scriptid = sdr.Item("ID")
End If
If Not IsDBNull(sdr.Item("ScriptType")) Then
scripttype = sdr.Item("ScriptType")
End If
tempresult = ""
If Not IsDBNull(sdr.Item("Arg0")) Then
tempresult = (sdr.Item("Arg0"))
If tempresult.Contains(",") Then
For Each s1 In (Split(tempresult))
Arg0.Add(s1)
Next
Else
Arg0.Add(tempresult)
End If
End If
tempresult = ""
If Not IsDBNull(sdr.Item("Arg1")) Then
tempresult = (sdr.Item("Arg1"))
If tempresult.Contains(",") Then
For Each s1 In (Split(tempresult))
Arg1.Add(s1)
Next
Else
Arg1.Add(tempresult)
End If
End If
tempresult = ""
If Not IsDBNull(sdr.Item("Arg2")) Then
tempresult = (sdr.Item("Arg2"))
If tempresult.Contains(",") Then
For Each s1 In (Split(tempresult))
Arg2.Add(s1)
Next
Else
Arg2.Add(tempresult)
End If
End If
tempresult = ""
If Not IsDBNull(sdr.Item("Arg3")) Then
tempresult = (sdr.Item("Arg3"))
If tempresult.Contains(",") Then
For Each s1 In (Split(tempresult))
Arg3.Add(s1)
Next
Else
Arg3.Add(tempresult)
End If
End If
tempresult = ""
If Not IsDBNull(sdr.Item("Arg4")) Then
tempresult = (sdr.Item("Arg4"))
If tempresult.Contains(",") Then
For Each s1 In (Split(tempresult))
Arg4.Add(s1)
Next
Else
Arg4.Add(tempresult)
End If
End If
tempresult = ""
If Not IsDBNull(sdr.Item("Arg5")) Then
tempresult = (sdr.Item("Arg5"))
If tempresult.Contains(",") Then
For Each s1 In (Split(tempresult))
Arg5.Add(s1)
Next
Else
Arg5.Add(tempresult)
End If
End If
tempresult = ""
If Not IsDBNull(sdr.Item("Arg6")) Then
tempresult = (sdr.Item("Arg6"))
If tempresult.Contains(",") Then
For Each s1 In (Split(tempresult))
Arg6.Add(s1)
Next
Else
Arg6.Add(tempresult)
End If
End If
End While
If Not conn.State = ConnectionState.Closed Then
conn.Close()
End If
End Sub
End Class
你能帮我把这个更优化吗?
答案 0 :(得分:0)
您可以做的一件事就是不要无休止地重复相同的代码:
tempresult = ""
If Not IsDBNull(sdr.Item("Arg0")) Then
tempresult = (sdr.Item("Arg0"))
If tempresult.Contains(",") Then
For Each s1 In (Split(tempresult))
Arg0.Add(s1)
Next
Else
Arg0.Add(tempresult)
End If
End If
将其变成这样的函数:
Function SplitResults(ByVal Arg As String) As List(Of String)
Dim Result As List(Of String) = New List(Of String)()
If Not IsDbNull(Arg) Then
If Arg.Contains(",") Then
Result.AddRange(Split(Arg, ","))
Else
Result.Add(Arg)
End If
End If
Return Result
End Function
你会这样称呼:
Arg1 = SplitResults(sdr.Item("Arg1"))
您还可以将单个List(Of String)
变量转换为List(Of List(Of String))
,这仍然会导致更容易循环或使用LINQ搜索的不规则列表。