2015-12-23 53 views
0

我有一些完美的代碼,但我不知道如何從字符串中獲取值?如何從SQL String中獲取值? vb.net

Dim SelectQuery As String = "SELECT OrderID FROM Orders_Header" 
     Dim WhereQuery As String = " WHERE 1=1" 

    Dim SO As String = BOX_SearchSO.Text 
    Dim RowID As Integer = Nothing 

    If SO = Nothing Then 
     LBL_Status.ForeColor = System.Drawing.Color.Red 
     LBL_Status.Text = "Please insert SO !" 
     Exit Sub 
    End If 

    If SO <> Nothing Then 
     WhereQuery = WhereQuery & " AND DVMOrderNumber LIKE '" & "%" + SO + "%" & "'" 
    End If 

    Dim myQuery As String = SelectQuery & WhereQuery 

MyQuery是完整的SQL查詢,我如何從字符串中獲取值?

+0

只是表明你在'myQuery'獲得最終的字符串變量 –

+0

有點offtopic,但您的查詢concatinating高容易受到SQL注入 –

+0

但我需要Query ...的值不是字符串 –

回答

0

建立數據庫連接並從數據庫中獲取數據。要了解更多,請點擊here

0
Dim con As New SqlConnection 
    Dim cmd As New SqlCommand 

    con = FunctionConnection() 
    cmd.Connection = con 
    cmd.CommandText = myQuery 
    cmd.CommandType = CommandType.Text 

    con.Open() 
    RowID = cmd.ExecuteScalar 
    con.Close() 

這是解決