从VB.NET上的DataSource中排除记录

时间:2021-12-03 01:40:20

I trying to figure out a way of excluding records from a DataSource (As in DataSource component in Visual Studio) comming from a SQL Server 2005 database table.

我试图找出一种从SQL Server 2005数据库表中的DataSource(在Visual Studio中的DataSource组件中)中排除记录的方法。

Let say I have a Users table (tblUsers) which has a Boolean field (isActive) to determine if the user is Active or not. So I add my DataSource and can clearly see it (on runtime) displaying all my users, now lets say I want to show only those who are actually active. Pretty much like on SQL would be a simple SELECT * FROM tblUsers WHERE isActive = 1

假设我有一个Users表(tblUsers),它有一个布尔字段(isActive)来确定用户是否处于活动状态。所以我添加了我的DataSource并且可以清楚地看到它(在运行时)显示我的所有用户,现在假设我想只显示那些实际上活跃的用户。非常类似于SQL将是一个简单的SELECT * FROM tblUsers WHERE isActive = 1

I figured I could create a View excluding such users and use it as my DataSource but I thought of asking if there's a way of accomplishing the same on runtime, might come in handy for other tasks in the future.

我想我可以创建一个不包含这些用户的View并将其用作我的DataSource,但我想问一下在运行时是否有一种方法可以完成相同的操作,可能会在将来为其他任务派上用场。

1 个解决方案

#1


I personally like views, but to all their own. If you want to execute a SQL statement to filter some results, you can always populate a dataset like this.

我个人喜欢观点,但他们都喜欢。如果要执行SQL语句来过滤某些结果,可以始终填充这样的数据集。

 Dim oConn As New SqlConnection("<your connection string>")

 Dim sSQL As String = "SELECT * FROM tblUsers WHERE isActive = 1"

 Dim oCmd As New SqlCommand(sSQL, oConn)
 Dim oDA As New SqlDataAdapter(oCmd)
 Dim oDS As New DataSet

 oDA.Fill(oDS)

 oConn.Dispose()
 oCmd.Dispose()
 oDA.Dispose()
 oDS.Dispose()

#1


I personally like views, but to all their own. If you want to execute a SQL statement to filter some results, you can always populate a dataset like this.

我个人喜欢观点,但他们都喜欢。如果要执行SQL语句来过滤某些结果,可以始终填充这样的数据集。

 Dim oConn As New SqlConnection("<your connection string>")

 Dim sSQL As String = "SELECT * FROM tblUsers WHERE isActive = 1"

 Dim oCmd As New SqlCommand(sSQL, oConn)
 Dim oDA As New SqlDataAdapter(oCmd)
 Dim oDS As New DataSet

 oDA.Fill(oDS)

 oConn.Dispose()
 oCmd.Dispose()
 oDA.Dispose()
 oDS.Dispose()