Showing posts with label tablename. Show all posts
Showing posts with label tablename. Show all posts

Wednesday, March 21, 2012

DataSet Filter

I have one dataset that contains thousands of records i want to filter the dataset by one of the name

select col1,col2,col3 from tablename

this col1 contains various names i want to check wheather an name exists in the col1 or not

please guide me

Hi, Febin

For your problem there is two possible solutions, First one is RowFilter and second one is Select

Solution 1 (RowFilter)

Dim ds As New Data.DataSet

Dim CN As New SqlClient.SqlConnection(MyClass1.ConnStr)

Dim Stmt As String

Stmt = "select * from USER_MASTER"

Dim DA As New SqlClient.SqlDataAdapter(stmt, CN)

DA.Fill(ds, "USER_MASTER")

ds.Tables("USER_MASTER").DefaultView.RowFilter = "DEPT ='ADMIN'"

MsgBox(ds.Tables("USER_MASTER").DefaultView.Count)

Solution 2 (Select)

Dim ds As New Data.DataSet

Dim CN As New SqlClient.SqlConnection(MyClass1.ConnStr)

Dim Stmt As String

Stmt = "select * from USER_MASTER"

Dim DA As New SqlClient.SqlDataAdapter(stmt, CN)

DA.Fill(ds, "USER_MASTER")

Dim R() As DataRow

R = ds.Tables("USER_MASTER").Select("DEPT ='ADMIN'", "EMP_NAME")

If R.Length > 0 Then

MsgBox(R.Length & " Record(s) Found")

End If

In the sample code it shows the number of records found for dept called "ADMIN",

Happy Coding

sql

Friday, February 24, 2012

DatabaseName Problem

DECLARE @.DatabaseName varchar(10)
DECLARE @.OwnerName varchar(10)
DECLARE @.TableName varchar(10)

SET @.DatabaseName='TempDatabase'
SET @.OwnerName='XYZ'
SET @.TableName='TempTable'

select * from @.DatabaseName.@.OwnerName.@.TableName

this is not working.

SEND ME HELP AS SOON AS POSSIBLEDECLARE @.DatabaseName varchar(10)
DECLARE @.OwnerName varchar(10)
DECLARE @.TableName varchar(10)

SET @.DatabaseName='TempDatabase'
SET @.OwnerName='XYZ'
SET @.TableName='TempTable'

select * from @.DatabaseName.@.OwnerName.@.TableName

this is not working.

SEND ME HELP AS SOON AS POSSIBLE
you will have to use dynamic sql something like this:

DECLARE @.DatabaseName varchar(10)
DECLARE @.OwnerName varchar(10)
DECLARE @.TableName varchar(10)
declare @.sql nvarchar(1000)
SET @.DatabaseName='databasename'
SET @.OwnerName='dbo'
SET @.TableName='tablename'

set @.sql='select * from '+@.DatabaseName+'.'+@.OwnerName+'.'+@.TableName

exec @.sql|||It would also help to not assign 12 characters to a 10 character varchar().|||...but who's counting...