473,408 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Querying a database using vb

I have a SQL table comprised of 31 columns.
The first column is simply an id column, the next 30 columns are labelled
[1],[2]...[30].
The numerical columns have a tinyint type and the data stored is either 1 or
null.
I wish to count the number of times a one appears in one column
simultaneously with another column:

eg count the number of times 1 appears in column [1] and 1 also appears in
column[15] in the same row:

Column [1] Column[15]
1 1 Count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count
1 Don't count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count,
etc.

The following has been suggested to me as a possible solution:
Imports MWFN

Partial Class MembersPages_MyPage

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim pairsAdapter As New MWFNTableAdapters.MWFNTableAdapter

Dim pairs As MWFN.MWFNDataTable

pairs = pairsAdapter.GetData

Dim myColumns As Data.DataColumnCollection = pairs.Columns
Dim columnA As Data.DataColumn
Dim columnB As Data.DataColumn
Dim num3 As Hashtable

For Each columnA In myColumns

If columnA.ColumnName <"Id" Then

For Each columnB In myColumns

If columnB.ColumnName <"Id" And columnB.Ordinal >
columnA.Ordinal Then

'if columnB's name doesn't equal Id and columnB's
position number is over columnA's, sum columnA and columnB

num3.Add(columnA.ColumnName & "," &
columnB.ColumnName, pairs.Compute("SUM(" & columnA.ColumnName & ") + SUM( " &
columnB.ColumnName & ")", ""))

End If
Next
End If
Next
End Sub
End Class

I have used a dataset to obtain the columns from the database datatable in
an effort to use a data access layer in my project. The dataset is MWFN.
The above code produces the following:
Warning: num3 is used before it has been assigned a value.
And when the programme is run:
Expecting a single column argument with possible child qualifier.

At this point in time , the code only sums/counts two columns. I will wish
to broaden that to sum/count 3 , 4 and possibly 5 columns, so the code needs
to be extensible. The number of columns will alos vary (probably from 27 to
59).

It doesn't matter if your suggestion is to use transact sql is a stored
procedure before the data gets to the project, or use vb as per the above
mentioned suggestion.
Any assistance would be appreciated.
Thank you

onecorp
Can anyone help point me in the right direction
Sep 22 '06 #1
4 2258
PGC
Hi onecorp,

I don't get this. Are you saying that the datatable can have a variable
number of columns? Can you provide the table definition? If the number of
columns is going to change when you go looking for the data then maybe they
should be rows in an other table instead.

PGC

"onecorp" <on*****@community.nospamwrote in message
news:1D**********************************@microsof t.com...
>I have a SQL table comprised of 31 columns.
The first column is simply an id column, the next 30 columns are labelled
[1],[2]...[30].
The numerical columns have a tinyint type and the data stored is either 1
or
null.
I wish to count the number of times a one appears in one column
simultaneously with another column:

eg count the number of times 1 appears in column [1] and 1 also appears in
column[15] in the same row:

Column [1] Column[15]
1 1 Count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count
1 Don't count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count,
etc.

The following has been suggested to me as a possible solution:
Imports MWFN

Partial Class MembersPages_MyPage

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim pairsAdapter As New MWFNTableAdapters.MWFNTableAdapter

Dim pairs As MWFN.MWFNDataTable

pairs = pairsAdapter.GetData

Dim myColumns As Data.DataColumnCollection = pairs.Columns
Dim columnA As Data.DataColumn
Dim columnB As Data.DataColumn
Dim num3 As Hashtable

For Each columnA In myColumns

If columnA.ColumnName <"Id" Then

For Each columnB In myColumns

If columnB.ColumnName <"Id" And columnB.Ordinal >
columnA.Ordinal Then

'if columnB's name doesn't equal Id and columnB's
position number is over columnA's, sum columnA and columnB

num3.Add(columnA.ColumnName & "," &
columnB.ColumnName, pairs.Compute("SUM(" & columnA.ColumnName & ") +
SUM( " &
columnB.ColumnName & ")", ""))

End If
Next
End If
Next
End Sub
End Class

I have used a dataset to obtain the columns from the database datatable in
an effort to use a data access layer in my project. The dataset is MWFN.
The above code produces the following:
Warning: num3 is used before it has been assigned a value.
And when the programme is run:
Expecting a single column argument with possible child qualifier.

At this point in time , the code only sums/counts two columns. I will wish
to broaden that to sum/count 3 , 4 and possibly 5 columns, so the code
needs
to be extensible. The number of columns will alos vary (probably from 27
to
59).

It doesn't matter if your suggestion is to use transact sql is a stored
procedure before the data gets to the project, or use vb as per the above
mentioned suggestion.
Any assistance would be appreciated.
Thank you

onecorp
Can anyone help point me in the right direction

Sep 22 '06 #2
Hi PGC,

Thank you for your reply.

No the datatable cannot have a variable number of columns. There are a
number of different datatables. The simplest has 27 columns, the most complex
has 59 columns.

Regards

Onecorp

"PGC" wrote:
Hi onecorp,

I don't get this. Are you saying that the datatable can have a variable
number of columns? Can you provide the table definition? If the number of
columns is going to change when you go looking for the data then maybe they
should be rows in an other table instead.

PGC

"onecorp" <on*****@community.nospamwrote in message
news:1D**********************************@microsof t.com...
I have a SQL table comprised of 31 columns.
The first column is simply an id column, the next 30 columns are labelled
[1],[2]...[30].
The numerical columns have a tinyint type and the data stored is either 1
or
null.
I wish to count the number of times a one appears in one column
simultaneously with another column:

eg count the number of times 1 appears in column [1] and 1 also appears in
column[15] in the same row:

Column [1] Column[15]
1 1 Count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count
1 Don't count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count,
etc.

The following has been suggested to me as a possible solution:
Imports MWFN

Partial Class MembersPages_MyPage

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim pairsAdapter As New MWFNTableAdapters.MWFNTableAdapter

Dim pairs As MWFN.MWFNDataTable

pairs = pairsAdapter.GetData

Dim myColumns As Data.DataColumnCollection = pairs.Columns
Dim columnA As Data.DataColumn
Dim columnB As Data.DataColumn
Dim num3 As Hashtable

For Each columnA In myColumns

If columnA.ColumnName <"Id" Then

For Each columnB In myColumns

If columnB.ColumnName <"Id" And columnB.Ordinal >
columnA.Ordinal Then

'if columnB's name doesn't equal Id and columnB's
position number is over columnA's, sum columnA and columnB

num3.Add(columnA.ColumnName & "," &
columnB.ColumnName, pairs.Compute("SUM(" & columnA.ColumnName & ") +
SUM( " &
columnB.ColumnName & ")", ""))

End If
Next
End If
Next
End Sub
End Class

I have used a dataset to obtain the columns from the database datatable in
an effort to use a data access layer in my project. The dataset is MWFN.
The above code produces the following:
Warning: num3 is used before it has been assigned a value.
And when the programme is run:
Expecting a single column argument with possible child qualifier.

At this point in time , the code only sums/counts two columns. I will wish
to broaden that to sum/count 3 , 4 and possibly 5 columns, so the code
needs
to be extensible. The number of columns will alos vary (probably from 27
to
59).

It doesn't matter if your suggestion is to use transact sql is a stored
procedure before the data gets to the project, or use vb as per the above
mentioned suggestion.
Any assistance would be appreciated.
Thank you

onecorp
Can anyone help point me in the right direction


Sep 24 '06 #3


I have run a sql suggestion and had the following queries,:

A)
(I made one small adjustment to your first suggestion in order to exclude
the Id column from the query, as follows:

If @Cols <'Id'
Set @qry='Select Count(*) From SPF Where (' + @qry + ')>1'

--Print @qry
Execute(@qry)
)
I executed the query on a table of 30 columns (adjusted the count as
necessary) using columns labelled 1, 2, and 3 . It returned a value of 34,
however, the answer should have been 3. Three is the number of times that one
simultaneously appears in each of the aforementioned columns ie . I only
wish to count the number or times that '1' appears in the nominted columns
simultaneously, whether I am checking two columns at a time or three columns
( or even 4 columns....which is why I thought that the query should be built
in managed code(VB .net) using SQL CLR ?????).

B)When I tried to use a table with 38 columns , I received the following
error:

Msg 217, Level 16, State 1, Procedure spMyTest2, Line 45
Maximum stored procedure, function, trigger, or view nesting level exceeded
(limit 32).

even after ensuring that I adjusted the count correctly....

C) This procedure appears to only return one value based on inputting the
paramters as indicated. For a table that contains 38 columns, there should be
8436 values. How do I return a list of all the combinations, not just one?

......Hence I thought managed code SQL CLR should be used. Does anyone have
any suggestions please ?

Any assistance would be appreciated.
Thank you
Onecorp

"onecorp" wrote:
Hi PGC,

Thank you for your reply.

No the datatable cannot have a variable number of columns. There are a
number of different datatables. The simplest has 27 columns, the most complex
has 59 columns.

Regards

Onecorp

"PGC" wrote:
Hi onecorp,

I don't get this. Are you saying that the datatable can have a variable
number of columns? Can you provide the table definition? If the number of
columns is going to change when you go looking for the data then maybe they
should be rows in an other table instead.

PGC

"onecorp" <on*****@community.nospamwrote in message
news:1D**********************************@microsof t.com...
>I have a SQL table comprised of 31 columns.
The first column is simply an id column, the next 30 columns are labelled
[1],[2]...[30].
The numerical columns have a tinyint type and the data stored is either 1
or
null.
I wish to count the number of times a one appears in one column
simultaneously with another column:
>
eg count the number of times 1 appears in column [1] and 1 also appears in
column[15] in the same row:
>
Column [1] Column[15]
1 1 Count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count
1 Don't count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count,
etc.
>
The following has been suggested to me as a possible solution:
>
>
Imports MWFN
>
Partial Class MembersPages_MyPage
>
Inherits System.Web.UI.Page
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim pairsAdapter As New MWFNTableAdapters.MWFNTableAdapter
>
Dim pairs As MWFN.MWFNDataTable
>
pairs = pairsAdapter.GetData
>
Dim myColumns As Data.DataColumnCollection = pairs.Columns
Dim columnA As Data.DataColumn
Dim columnB As Data.DataColumn
Dim num3 As Hashtable
>
For Each columnA In myColumns
>
If columnA.ColumnName <"Id" Then
>
For Each columnB In myColumns
>
If columnB.ColumnName <"Id" And columnB.Ordinal >
columnA.Ordinal Then
>
'if columnB's name doesn't equal Id and columnB's
position number is over columnA's, sum columnA and columnB
>
num3.Add(columnA.ColumnName & "," &
columnB.ColumnName, pairs.Compute("SUM(" & columnA.ColumnName & ") +
SUM( " &
columnB.ColumnName & ")", ""))
>
End If
Next
End If
Next
End Sub
End Class
>
I have used a dataset to obtain the columns from the database datatable in
an effort to use a data access layer in my project. The dataset is MWFN.
The above code produces the following:
Warning: num3 is used before it has been assigned a value.
And when the programme is run:
Expecting a single column argument with possible child qualifier.
>
At this point in time , the code only sums/counts two columns. I will wish
to broaden that to sum/count 3 , 4 and possibly 5 columns, so the code
needs
to be extensible. The number of columns will alos vary (probably from 27
to
59).
>
It doesn't matter if your suggestion is to use transact sql is a stored
procedure before the data gets to the project, or use vb as per the above
mentioned suggestion.
Any assistance would be appreciated.
Thank you
>
onecorp
>
>
Can anyone help point me in the right direction
Oct 3 '06 #4
There are a number of ways to solve your issue, here is one that I think
will help.

Create a temp (@MatchCounts) table for to hold values.

@MatchCounts
FieldList Varchar(255) Not Null
MatchCount Int Not Null

Loop over the combinations you want to retrieve

Insert into @MatchCount (FieldList, MatchCount)
Select "010203" as FieldList, Count(ID) as MatchCount
From SPF
Where SPF.Field1=1 and SPF.Field2 =1 and SPF.Field3 = 1

Return the @MatchCount resultset

dim sResults as string = string.empty
dim sField as string = string.empty
for each dr as datarow in ds.tables(1).rows

sResults = string.empty

for i = 1 to ctype(dr("FieldList"),string).length step 2
sField = mid(dr("FieldList"),i,2)
if sResults.length = 0 then
sResults =
string.format("Field{0}",ctytpe(ctype(sfield,integ er),string))
else
sResults = string.format("{0},
Field{1}",sResults,ctytpe(ctype(sfield,integer),st ring))
end if
next i

console.writeline string.format("The match count for fields:{0} is
{1}",sresults,dr("MatchCount"))

next for
"onecorp" <on*****@community.nospamwrote in message
news:58**********************************@microsof t.com...
>

I have run a sql suggestion and had the following queries,:

A)
(I made one small adjustment to your first suggestion in order to exclude
the Id column from the query, as follows:

If @Cols <'Id'
Set @qry='Select Count(*) From SPF Where (' + @qry + ')>1'

--Print @qry
Execute(@qry)
)
I executed the query on a table of 30 columns (adjusted the count as
necessary) using columns labelled 1, 2, and 3 . It returned a value of 34,
however, the answer should have been 3. Three is the number of times that
one
simultaneously appears in each of the aforementioned columns ie . I only
wish to count the number or times that '1' appears in the nominted columns
simultaneously, whether I am checking two columns at a time or three
columns
( or even 4 columns....which is why I thought that the query should be
built
in managed code(VB .net) using SQL CLR ?????).

B)When I tried to use a table with 38 columns , I received the following
error:

Msg 217, Level 16, State 1, Procedure spMyTest2, Line 45
Maximum stored procedure, function, trigger, or view nesting level
exceeded
(limit 32).

even after ensuring that I adjusted the count correctly....

C) This procedure appears to only return one value based on inputting the
paramters as indicated. For a table that contains 38 columns, there should
be
8436 values. How do I return a list of all the combinations, not just one?

.....Hence I thought managed code SQL CLR should be used. Does anyone have
any suggestions please ?

Any assistance would be appreciated.
Thank you
Onecorp

"onecorp" wrote:
>Hi PGC,

Thank you for your reply.

No the datatable cannot have a variable number of columns. There are a
number of different datatables. The simplest has 27 columns, the most
complex
has 59 columns.

Regards

Onecorp

"PGC" wrote:
Hi onecorp,

I don't get this. Are you saying that the datatable can have a variable
number of columns? Can you provide the table definition? If the number
of
columns is going to change when you go looking for the data then maybe
they
should be rows in an other table instead.

PGC

"onecorp" <on*****@community.nospamwrote in message
news:1D**********************************@microsof t.com...
I have a SQL table comprised of 31 columns.
The first column is simply an id column, the next 30 columns are
labelled
[1],[2]...[30].
The numerical columns have a tinyint type and the data stored is
either 1
or
null.
I wish to count the number of times a one appears in one column
simultaneously with another column:

eg count the number of times 1 appears in column [1] and 1 also
appears in
column[15] in the same row:

Column [1] Column[15]
1 1 Count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count
1 Don't count this
1 Don't count this
1 1 add 1 to the count
1 1 add 1 to the count,
etc.

The following has been suggested to me as a possible solution:
Imports MWFN

Partial Class MembersPages_MyPage

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim pairsAdapter As New MWFNTableAdapters.MWFNTableAdapter

Dim pairs As MWFN.MWFNDataTable

pairs = pairsAdapter.GetData

Dim myColumns As Data.DataColumnCollection = pairs.Columns
Dim columnA As Data.DataColumn
Dim columnB As Data.DataColumn
Dim num3 As Hashtable

For Each columnA In myColumns

If columnA.ColumnName <"Id" Then

For Each columnB In myColumns

If columnB.ColumnName <"Id" And columnB.Ordinal >
columnA.Ordinal Then

'if columnB's name doesn't equal Id and columnB's
position number is over columnA's, sum columnA and columnB

num3.Add(columnA.ColumnName & "," &
columnB.ColumnName, pairs.Compute("SUM(" & columnA.ColumnName & ") +
SUM( " &
columnB.ColumnName & ")", ""))

End If
Next
End If
Next
End Sub
End Class

I have used a dataset to obtain the columns from the database
datatable in
an effort to use a data access layer in my project. The dataset is
MWFN.
The above code produces the following:
Warning: num3 is used before it has been assigned a value.
And when the programme is run:
Expecting a single column argument with possible child qualifier.

At this point in time , the code only sums/counts two columns. I will
wish
to broaden that to sum/count 3 , 4 and possibly 5 columns, so the
code
needs
to be extensible. The number of columns will alos vary (probably from
27
to
59).

It doesn't matter if your suggestion is to use transact sql is a
stored
procedure before the data gets to the project, or use vb as per the
above
mentioned suggestion.
Any assistance would be appreciated.
Thank you

onecorp
Can anyone help point me in the right direction

Oct 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ravi Shankar | last post by:
Hi all, I have a calendar application( like Microsoft Outlook) writtn in Java.Whenever an event is created, we can set SMS/EMAIL notification. Hence when an event is created, I am storing that...
3
by: Keith | last post by:
I am fairly new to SQL so sorry if this is a really dumb question. I have a small (still) SQL database, which I am trying to query from an ASP page. The field I am querying is of DATETIME data...
1
by: valexena | last post by:
After a few minutes of querying the database my session disconnects abruptly. Can somebody help me and tell why can be happening? -- Posted via http://dbforums.com
2
by: Elliot Rodriguez | last post by:
As I continue to read more about the benefits of database querying using ADO.NET, I am having a more difficult time distinguishing what the best approach to data retrieval is anymore. When...
1
by: radicool | last post by:
What is the User ID and Password for querying Follett's TextLink database. I am using Adaptive Server Anywhere. If this is not the best group to ask this, where would be?
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
5
by: Shane | last post by:
I wonder if someone has any ideas about the following. I am currently producing some reports for a manufacturing company who work with metal. A finished part can contain multiple sub-parts to...
0
by: roiavidan | last post by:
Hi, I'm having a bit of a problem with a small application I wrote in C#, which uses an Access database (mdb file) for storing financial data. After looking for a similiar topic and failing to...
5
by: sql_er | last post by:
Guys, I have an XML file which is 233MB in size. It was created by loading 6 tables from an sql server database into a dataset object and then writing out the contents from this dataset into an...
2
by: RajSharma | last post by:
Hi, I am facing a problem regarding querying thru a large table having millions of rows....... Its hanging in between while querying for all those rows Can anybody suggest me a query regarding :...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.