2011-06-24 48 views

回答

1

時,將發佈新的代碼如果您檢查,如果SP有兩個元素或沒有做

If sp.Length = 2 Then 
... 
End If 

If塊內,你可以做

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

Bala我向你的帖子中添加了一個問題,謝謝你對我最後一個問題的幫助。你知道一本好書,我買了兩本書,但他們似乎經歷了最基本的例子,而不是現實世界的東西。還是一本從頭到尾開始一個巨大項目的書?再次感謝 – Tim

+0

你好巴拉我發現了一本書,所以沒關係,它被稱爲正則表達式食譜今天買了它會發布更新的代碼,當我讀了這本書,謝謝你的幫助 – Tim

+0

@Jawaid對不起,我不知道任何好書。我通常會用我的方式來搜索我的東西,但我很高興你找到了一本書。 –

1

C#更符合我的喜好,但這是一個粗略的方法,將拆分您的字符串,測試正確數量的元素,然後創建數據庫連接(假定SQL Server BU任何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 
+0

感謝您的代碼,但是我需要檢索一個或另一個跟蹤代碼,只檢查是否存在2個元素,然後它會做一些事情。我使用vb中的中間函數來獲取我需要的數字,並將它們存儲在2個單獨的變量中,並根據所存在的變量進行調用 – Tim