Sql Visual Basic HowTo:从.mdf检索数据
我做错了什么?我已经做了一段时间了。。。我投降Sql Visual Basic HowTo:从.mdf检索数据,sql,vb.net,visual-studio-2012,sql-server-2012,Sql,Vb.net,Visual Studio 2012,Sql Server 2012,我做错了什么?我已经做了一段时间了。。。我投降 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=Tr
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
你忘了打电话了
string apName = sqlCmd.ExecuteScaler();
并检索结果
如果需要一个结果集(多个结果),那么调用
reader=cmd.ExecuteReader()
并循环遍历结果。我把它放在那里,但当我把代码带过来时,我意外地把它拿走了。。。它仍然返回SELECT语句作为标签文本,而不是APPNAME。它已被接受并向上投票。我向米奇麦道歉