帮助从存储过程填充网格视图
我正在尝试执行存储过程并将信息放入数据网格中(不使用 .net 向导),我想手动执行此操作。我相信使用 ado.net。我正在使用 vb.net 和 asp.net
Public cmd As New SqlCommand()
Public saoda As New SqlDataAdapter(cmd)
Public conn As New SqlConnection(" ")
Dim saods As New DataSet
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("@yeartoget", DropDownList1.SelectedValue)
cmd.CommandText = "casof"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
conn.Open()
reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.
GridView1.DataSource = saods
saoda.FillSchema(saods, SchemaType.Mapped)
GridView1.DataBind()
conn.Close()
End Sub
End Class
vvvvv
Dim pctofsales As New DataColumn
pctofsales = New DataColumn("PCTofsales", GetType(Decimal))
pctofsales.Expression = "IIF([YEsales] = 0, 0, [ASOFSales] / [YEsales])"
saods1.Tables("salesasoftable").Columns.Add(pctofsales)
I am trying to execute a stored procedure and place the information in a datagrid(without using the .net wizard) i want to do it manually. Using ado.net i believe. I am using vb.net and asp.net
Public cmd As New SqlCommand()
Public saoda As New SqlDataAdapter(cmd)
Public conn As New SqlConnection(" ")
Dim saods As New DataSet
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("@yeartoget", DropDownList1.SelectedValue)
cmd.CommandText = "casof"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
conn.Open()
reader = cmd.ExecuteReader()
' Data is accessible through the DataReader object here.
GridView1.DataSource = saods
saoda.FillSchema(saods, SchemaType.Mapped)
GridView1.DataBind()
conn.Close()
End Sub
End Class
vvvvv
Dim pctofsales As New DataColumn
pctofsales = New DataColumn("PCTofsales", GetType(Decimal))
pctofsales.Expression = "IIF([YEsales] = 0, 0, [ASOFSales] / [YEsales])"
saods1.Tables("salesasoftable").Columns.Add(pctofsales)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要将 DataSet 绑定到 GridView - SqlDataReader 就足够了。也就是说,您可以使用以下内容:
但是,如果您想要/需要使用 DataTable,那也没什么大不了的。以下代码片段将读取器加载到数据表中:
快乐编程!
You don't need to bind a DataSet to a GridView - a SqlDataReader will suffice. That is, you could use the following:
However, if you want/need to use a DataTable, that's no big deal, either. The following snippet loads a reader into a DataTable:
Happy Programming!