如何将公共变量传递给sql查询 [英] How to pass a public variable to a sql query

查看:68
本文介绍了如何将公共变量传递给sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我在应用程序的form1中声明的公共变量。我试图在form2中调用该变量,然后在sql查询中传递该变量。


如果我申报


公共类Form1

公共payPeriodStartDate,payPeriodEndDate As Date


如何在form2中声明该变量以及如何将其传递给我的sql查询

I have a public variable that I declared in form1 of an application. I am trying to call that variable in form2 and then pass that variable in a sql query.

If I declare

Public Class Form1
Public payPeriodStartDate, payPeriodEndDate As Date

How then to I declare that variable in form2 and how to I pass it to my sql query

展开 | 选择 | 换行 | 行号

推荐答案

您使用的是tableadapter吗?这使它变得容易多了。从tableadapter可以添加查询,在创建查询时使用@来声明参数。所以你会在tableadapter中创建这样的查询:
Are you using a tableadapter? That makes it a lot easier. From the tableadapter you can add the query, and when creating the query use an @ to declare parameters. So you''d create teh query like this in the tableadapter:
展开 | 选择 | Wrap | 行号


其实我现在想到这个..我目前在我的表单中有一个数据网格视图,它只有一个


从异常查询中选择*。我在表单的开头声明了这些变量:


公共payPeriodStartDate作为日期

公共payPeriodEndDate作为日期


我想做的是修改我对datagrid视图的查询更像是这样的


select * from exceptions

where payperiodstartdate和payperiodenddate之间的例外情况。


最好的方法是什么?


谢谢
Actually now that I think about this ... I currently have a datagrid view in my form and it has just a

Select * from exceptions query. I declare these variables at the start of my form:

Public payPeriodStartDate As Date
Public payPeriodEndDate As Date

and what I''d like to do is to modify my query for the datagrid view to be something more like this

select * from exceptions
where exceptiondate between payperiodstartdate and payperiodenddate.

What''s the best way to do that?

Thank you


好的,你可以做的是在查询向导中有一个选项,当你在tableadapter中创建查询时,使用查询来填充数据表。因此,在Form中声明数据表(而不是在OnClick事件中),并在onclick事件中运行查询以填充数据表。然后,您可以将DataGrid源设置为新填充的数据表。
Ok, what you can do then is there is an option in the query wizard, when you create the query in the tableadapter, to use the query to fill a datatable. So declare the datatable in the Form (not in the OnClick event), and in the onclick event run the query to fill the datatable. Then you can just set the DataGrid source to the newly filled datatable.


这篇关于如何将公共变量传递给sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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