vb asp.net字符串操作

时间:2011-06-24 01:29:09

标签: asp.net sql vb.net

更新后我将在完成后发布新代码

2 个答案:

答案 0 :(得分:1)

如果通过执行

检查sp是否包含两个元素
If sp.Length = 2 Then
...
End If

并在If区块内,您可以

Dim track1 as String = sp(0)
Dim track2 as String = sp(1)

答案 1 :(得分:1)

C#更符合我的喜好,但这是一个粗略的方法,它将拆分你的字符串,测试正确数量的元素,然后创建数据库连接(假定sql server但任何ole db应该工作)并调用你的存储过程

Public Sub ProcessSwipe(ByVal value As String)
    Dim splitValues() As String
    Dim separator As Char = ";"
    Dim connectionString As String = String.Empty
    Dim storedProcName As String = String.Empty

    ' Edit these to sane values
    connectionString = "Provider=SQLOLEDB;BData Source=localhost;Initial Catalog=msdb;Integrated Security=SSPI;"
    storedProcName = "dbo.SaveSwipe"

    If String.IsNullOrEmpty(value) Then
        MsgBox("No value provided from swipe")
        Return
    End If

    Try
        splitValues = value.Split(separator)
        If splitValues.Length <> 2 Then
            MsgBox(String.Format("Splitting of swipe data did not result in 2 values. Swiped value is {0}", value))
            Return
        End If

        ' At this point, we should have an array with 2 elements in it
        ' Open a connection
        Using connection As New OleDb.OleDbConnection(connectionString)
            connection.Open()

            Using Command As New OleDb.OleDbCommand
                Command.CommandText = storedProcName
                Command.CommandType = CommandType.StoredProcedure
                Command.Connection = connection

                ' Assumes your proc takes 2 parameters, named swipe1 and swipe2
                ' Update to sane values
                Command.Parameters.AddWithValue("@swipe1", splitValues(0))
                Command.Parameters.AddWithValue("@swipe2", splitValues(1))

                ' Make the actual call to your stored procedure
                Command.ExecuteNonQuery()
            End Using
        End Using

    Catch ex As Exception
        MsgBox(String.Format("Attempt to split {0} failed. {1}", value, ex))
    End Try
End Sub

Sub Main()
    Dim shorty As String
    Dim works As String
    Dim longun As String
    Dim nuthin As String

    shorty = "%128565?"
    works = "%128565?;229584115?"
    longun = "%128565?;229584115?%128565?;229584115?"
    nuthin = Nothing

    ProcessSwipe(shorty)
    ProcessSwipe(works)
    ProcessSwipe(longun)
    ProcessSwipe(nuthin)
End Sub