SQL 从存储过程填充 DataGridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1927822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate DataGridView from a Stored Procedure
提问by Belliez
Using SQL Server 2008 I created a Stored Procedure called MyStoreProc and it runs fine from the Management Tools.
我使用 SQL Server 2008 创建了一个名为 MyStoreProc 的存储过程,它在管理工具中运行良好。
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.
在 VB.Net 2008 中,我创建了一个新的数据集和一个新的 TableAdaptor。在这个表适配器中,我创建了一个名为 FillByGrid 的新查询并选择了存储过程。预览数据并正确预览。
On a form I created DataGridView and selected the Table Adapter from the dataset.
在一个表单上,我创建了 DataGridView 并从数据集中选择了表适配器。
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:
我运行了该应用程序,但没有显示任何数据。Visual Studio 自动创建了下面的代码,我将其更改为选择我刚刚创建的查询:
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")
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.
仍然没有显示数据。然而,通过我可以看到连接是打开的,并且“da.Fill(ds,“tbl1”)”需要一点时间,因为它正在运行存储过程并且ds表具有正确的行数和列数. 它只是没有显示在数据网格上。
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.
在数据集中创建另一个表适配器并使用标准 select * from table 命令从数据库表中返回数据,显示在 datagridview 中。
Does anyone have any suggestions?
有没有人有什么建议?
Thank you
谢谢
采纳答案by Mark Cooper
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/-
关于如何做到这一点,似乎有大量可用资源。最好的来源是:http: //www.asp.net/learn/data-access/-
Failing that a good old fashioned search yields lots too.
如果失败,一个好的老式搜索也会产生很多。
回答by Belliez
I managed to get this going now. Below is what I did. Thank you everyone for your help. It pushed me in the right direction.
我现在设法做到了。下面是我所做的。感谢大家的帮助。它把我推向了正确的方向。
Dim strCon As String = myConnectionString
Dim strSQL As String = "dbo.MyStoreProc"
Dim dataAdapter As New SqlClient.SqlDataAdapter(strSQL, strCon)
Dim table As New DataTable
dataAdapter.Fill(table)
DataGridView1.DataSource = table
回答by Nick
try DataGridView2.DataBind(); at the end
试试 DataGridView2.DataBind(); 在末尾