2013-04-21 78 views
0

我在做什麼錯?我一直在這一段時間......我投降了。Visual Basic HowTo:從.mdf檢索數據

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Using sqlCon = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\gadgetDatabase.mdf;Integrated Security=True") 

     Dim Id As Integer = 2 
     'With the use of ID, it will get the appName or Application Display Name 
     sqlCon.Open() 
     Dim sqlText = "SELECT appName " & _ 
         "FROM appTable " & _ 
         "WHERE Id = @sqlID" 
     Dim sqlCmd = New SqlCommand(sqlText, sqlCon) 
     sqlCmd.Parameters.AddWithValue("@sqlID", Id) 
     'sqlCmd.ToString() 
     sqlCmd.ExecuteScalar() 'I had these in there before I copied the code over 
     sqlCon.Close() 
     Label3.Text = sqlText 'For testing or confirmation it went correctly... 

    End Using 

End Sub 
+0

它已被接受和upvoted。我很抱歉@MitchWheat – 2013-05-05 15:24:28

回答

1

你忘了打電話給

string apName = sqlCmd.ExecuteScaler(); 

並檢索結果。

如果預期結果集(多個結果),則調用reader = cmd.ExecuteReader()並遍歷結果。

+0

我在那裏,但是當我把代碼帶過來時,我把它當成了我們的意外......它仍然返回SELECT語句作爲標籤文本而不是appName – 2013-04-21 04:20:58

+0

明白了...你'重新做最好的!感謝米奇 – 2013-04-21 04:30:24