2011-06-24 45 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