帮助从存储过程填充网格视图

发布于 2024-10-09 22:48:42 字数 1182 浏览 2 评论 0原文

我正在尝试执行存储过程并将信息放入数据网格中(不使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳絮泡泡 2024-10-16 22:48:42

您不需要将 DataSet 绑定到 GridView - SqlDataReader 就足够了。也就是说,您可以使用以下内容:

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()

    GridView1.DataSource = reader
    GridView1.DataBind()

    conn.Close()
End Sub

但是,如果您想要/需要使用 DataTable,那也没什么大不了的。以下代码片段将读取器加载到数据表中:

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()

Dim myTable As DataTable = New DataTable()
myTable.Load(reader)

GridView1.DataSource = dt
GridView1.DataBind()

conn.Close()

快乐编程!

You don't need to bind a DataSet to a GridView - a SqlDataReader will suffice. That is, you could use the following:

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()

    GridView1.DataSource = reader
    GridView1.DataBind()

    conn.Close()
End Sub

However, if you want/need to use a DataTable, that's no big deal, either. The following snippet loads a reader into a DataTable:

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()

Dim myTable As DataTable = New DataTable()
myTable.Load(reader)

GridView1.DataSource = dt
GridView1.DataBind()

conn.Close()

Happy Programming!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文