我正在尝试从数据库中获取数据(我已经完成并且知道它有效) 但是我想将数据推送到变量中。
我看不出有什么问题,目前“cmd.select”周围有一个错误,所以如果有人能指出我正确的方向,那就太好了!
Dim cn As SqlConnection = New SqlConnection()
Dim cmd As SqlCommand = New SqlCommand()
Dim sqladp As New SqlDataAdapter()
Dim ds As New DataSet()
cmd.Parameters.Clear()
cn.ConnectionString = ConfigurationManager.ConnectionStrings("PemcoConnectionString").ConnectionString
cmd.Connection = cn
GridView2.Visible = True
cmd.Connection = cn
cmd.CommandText = "spUserResultsDetails"
cmd.CommandType = CommandType.StoredProcedure
Session.Item("ID") = (sender.SelectedValue.ToString)
cmd.Parameters.AddWithValue("@ID", Session.Item("ID"))
For Each datarow As Data.DataRowView In cmd.Select(DataSourceSelectArguments.Empty)
sEmailAddress = datarow("UserEmail")
Next
sqladp.SelectCommand = cmd
sqladp.Fill(ds)
答案 0 :(得分:1)
有许多事情需要改进,这应该有效:
Using cn = New SqlConnection(ConfigurationManager.ConnectionStrings("PemcoConnectionString").ConnectionString))
Using da = New SqlDataAdapter("spUserResultsDetails", cn)
da.SelectCommand.CommandType=CommandType.StoredProcedure
da.SelectCommand.Parameters.AddWithValue("@ID", CInt(Session.Item("ID")))
Dim table = New Data.DataTable()
da.Fill(table)
GridView2.DataSource = table
GridView2.DataBind()
End Using
End Using
摘要:
SqldataAdapter
,请使用DataTable
,您无需使用填充打开/关闭它,使用它的SelectCommand
属性来获取SqlCommand
的引用{1}} DataSource
的{{1}}和DataBind it GridView
,则应将传递的对象强制转换为正确的类型,否则无法推断出正确的类型