试图了解ASP.NET DAL和TableAdapter,我希望有人可以在使用SQL查询参数时帮助我获取列数据。
如果我有一个带有返回一行或多行的查询的TableAdapter,我可以从数据表中获取行和列数据没问题。对于返回一行的简单查询,我可以使用:
SQL: SELECT firstName, lastName from Names WHERE ID=1
Dim MyAdapter1 As New ProjectTableAdapters.NamesTableAdapter1
Dim f As String = MyAdapter1.GetNames.Rows(0)("firstName")
Dim l As String = MyAdapter1.GetNames.Rows(0)("lastName")
但是如果使用带有参数查询的TableAdapter,我会不断抛出异常。我确信这很简单,如果有人可以指出我正确的方向(如果可能的话,vb)。非常感谢。
SQL: SELECT firstName, lastName from Names WHERE ID=@ID
Dim ID As Integer = 1
Dim MyAdapter2 As New ProjectTableAdapters.NamesTableAdapter2
Dim f As String = MyAdapter2.GetNames(Rows(0)("firstName"),ID) [throws exection]
编辑: 我想我现在有效 - 我的语法不好......
Dim f As String = MyAdapter2.GetNames(ID).Rows(0)("firstName")
答案 0 :(得分:0)
Public Function MyFunction() As DataSet
Dim conString As String = "Connection String"
Dim conn As New SqlConnection(conString)
Dim da As New SqlDataAdapter()
Dim cmd As New SqlCommand With {
.Connection = conn,
.CommandText = "SELECT firstName, lastName from Names WHERE ID=@ID",
.CommandType = CommandType.Text}
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1
da.SelectCommand = cmd
Dim ds As DataSet = New DataSet()
conn.Open()
da.Fill(ds)
conn.Close()
Return ds
End Function