从存储过程填充 DataGridView [英] Populate DataGridView from a Stored Procedure

查看:15
本文介绍了从存储过程填充 DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SQL Server 2008 创建了一个名为 MyStoreProc 的存储过程,它在管理工具中运行良好.

Using SQL Server 2008 I created a Stored Procedure called MyStoreProc and it runs fine from the Management Tools.

在 VB.Net 2008 中,我创建了一个新数据集和一个新 TableAdaptor.在这个表适配器中,我创建了一个名为 FillByGrid 的新查询并选择了存储过程.预览数据并正确预览.

In VB.Net 2008 I created a new dataset and a new TableAdaptor. In this table adapter I created a new Query called FillByGrid and selected the Stored Procedure. Previewed data and it previewed correctly.

在一个表单上,我创建了 DataGridView 并从数据集中选择了 Table Adapter.

On a form I created DataGridView and selected the Table Adapter from the dataset.

我运行了应用程序,但没有显示任何数据.Visual Studio 自动创建了下面的代码,我将其更改为选择我刚刚创建的查询:

I ran the app and no data is shown. Visual Studio autocreated the code below and I changed it to select the the Query I just created:

Me.MyTableAdapter.FillByGrid(Me.MyDataset.MyTableAdaptor)

网格上没有显示数据,所以我尝试了手动方法:

No data is shown on the grid so I tried the manual approach:

' Create the dataset
Dim da As New SqlDataAdapter, ds As New DataSet
Dim conn As New SqlConnection

conn.ConnectionString = opsData.DBConn.ConnectionString
da.SelectCommand = New SqlCommand
da.SelectCommand.Connection = conn
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.CommandText = "dbo.MyStoreProc"
da.Fill(ds, "tbl1")
DataGridView2.DataSource = ds.Tables("tbl1")

仍然没有显示数据.但是单步执行我可以看到连接已打开,并且da.Fill(ds, "tbl1")" 需要一点时间,因为它正在运行存储过程,并且 ds 表具有正确的行数和列数.它只是没有显示在数据网格上.

Still no data shown. However stepping through the I can see that the connection is open, and "da.Fill(ds, "tbl1")" takes a little bit of time as it is running the Stored Procedure and ds table has the correct number of rows and columns. Its just not being shown on the datagrid.

在数据集中创建另一个表适配器,并使用标准的 select * from table 命令从数据库表中返回数据,显示在 datagridview 中.

Creating another table adapter in the dataset and returning data from a database table using a standard select * from table command display in the datagridview fine.

有人有什么建议吗?

谢谢

推荐答案

似乎有大量可用资源来说明如何做到这一点.最好的来源是:http://www.asp.net/learn/data-access/ -

There seem to be a wealth of resources available on how to do this. The best source would be: http://www.asp.net/learn/data-access/ -

没有一个好的老式 搜索结果也很多.

这篇关于从存储过程填充 DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆