【问题标题】:Visual Basic HowTo: Retrieve Data from .mdfVisual Basic 操作方法:从 .mdf 检索数据
【发布时间】:2013-04-21 03:57:39
【问题描述】:

我做错了什么?我已经有一段时间了……我投降了。

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

【问题讨论】:

  • 它已被接受和投票。我很抱歉@MitchWheat

标签: sql vb.net visual-studio-2012 sql-server-2012


【解决方案1】:

你忘了打电话

string apName = sqlCmd.ExecuteScaler();

并检索结果。

如果需要一个结果集(多个结果),则调用 reader = cmd.ExecuteReader() 并循环遍历结果。

【讨论】:

  • 我有它,但是当我把代码拿过来时,我不小心把它拿走了......它仍然返回 SELECT 语句作为标签文本而不是 appName
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多