[英]populate a sql query into datagridview on vb.net
I wanna populate sql query on datagridview on vb.net I tried this code that I found on inte.net but I get an error我想在 vb.net 上的 datagridview 上填充 sql 查询 我尝试了在 inte.net 上找到的这段代码,但出现错误
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
DataGridView2.Visible = False
Dim Dataconnection As SqlConnection
Dataconnection.ConnectionString = "server=DESKTOP-514KV5J\SQLEXPRESS;database=A;trusted_connection=True"
Dim cmd As New SqlCommand
cmd.Connection = Dataconnection
cmd.CommandText = "select SA,Product,sum(quantity) quantity from(select SA,Product,quantity from tbpurchase union all select SA,Product,-quantity from tbsold)dt group by SA,Product"
Dim rdr As SqlDataAdapter = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(rdr)
rdr.close()
DataGridView2.DataSource = dt
End Sub
I just want to view the query on datagridview2 whitout any storage on any table.我只想查看 datagridview2 上的查询,而无需任何表上的任何存储。 if you have a solution how can I do it.
如果您有解决方案,我该怎么做。 because i have 2 tables.
因为我有2张桌子。 1 for purchasing.
1 用于采购。 1 for selling.
1 用于销售。 and the query that i did work on ms sql well and caculates the stock still available thanks in advance
以及我在 sql 女士上所做的查询,并计算出仍然可用的库存提前致谢
There are two obvious issues there and, for all we know, there could be more.那里有两个明显的问题,据我们所知,可能还有更多。 Firstly, you never actually create a connection object. All you do is declare a variable and then try to set the
ConnectionString
of an object that you never created.首先,您从未实际创建连接 object。您所做的只是声明一个变量,然后尝试设置您从未创建的 object 的
ConnectionString
。 That's absolute programming fundamentals and nothing specific to do with data access, so that's not something we should have to explain, especially since you are creating objects later in the code so obviously know how.这是绝对的编程基础,与数据访问无关,所以这不是我们应该解释的东西,特别是因为您稍后会在代码中创建对象,所以显然知道如何操作。
Secondly, you aren't even trying to open the connection that you didn't create.其次,您甚至不会尝试打开您未创建的连接。 If you call
Fill
on a data adapter then it will open and close the connection automatically but, if you use a data reader, you have to do it yourself.如果您在数据适配器上调用
Fill
,那么它会自动打开和关闭连接,但如果您使用数据读取器,则必须自己完成。
With those two issues in mind and cleaning up the code somewhat, we arrive at this:考虑到这两个问题并稍微清理代码,我们得出以下结论:
Using connection As New SqlConnection("server=DESKTOP-514KV5J\SQLEXPRESS;database=A;trusted_connection=True"),
command As New SqlCommand("SELECT SA, Product, SUM(Quantity) Quantity FROM (SELECT SA, Product, Quantity FROM tbpurchase UNION ALL SELECT SA, Product, -Quantity FROM tbsold) dt GROUP BY SA, Product", connection)
connection.Open()
Dim table As New DataTable
Using reader = command.ExecuteReader()
table.Load(reader)
End Using
DataGridView2.DataSource = table
End Using
For objects that support it, you should always create them with a Using
block if you are only going to use them then and there, because they will be implicitly disposed at the End Using
line.对于支持它的对象,如果您只打算当场使用它们,则应始终
Using
块创建它们,因为它们将隐式放置在End Using
行。 It is also preferable to pass arguments to constructors rather than call parameterless constructors and then set properties immediately after.最好将 arguments 传递给构造函数,而不是调用无参数构造函数,然后立即设置属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.