Faster way to pull data from an sql query?

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2797 Views - Last Post: 08 April 2009 - 07:10 AM Rate Topic: -----

#1 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Faster way to pull data from an sql query?

Posted 23 March 2009 - 02:03 PM

My program pulls a weeks worth of records at a time and filters it into daily columns on a form, the problem is that there are ~80 fields to be filled in this manner and as far as i know each one has to be check for null before it can be filled. Could someone tell me if there is a faster way to do this? Below is my current code for one set of fields.

Thanks in advance

   Private Sub cboDigit_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDigit.SelectedIndexChanged, cboOffice.SelectedIndexChanged, dtEndDate.ValueChanged, dtStartDate.ValueChanged

		If blnLoad = True Then
			If cboDigit.SelectedIndex <> -1 And cboOffice.SelectedIndex <> -1 Then
				Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= \\Bkesl\VOL2\Common\updates\CaseAdmin\CaseAdmin.accdb")
				conn.Open()

				Dim userDetailsAdapter As New OleDbDataAdapter("SELECT * FROM tblCaseAdmin where Digit =" + cboDigit.Text + " AND Office = '" + cboOffice.Text + "' And Day between #" + dtStartDate.Text + "# and #" + dtEndDate.Text + "#", conn)

				Dim userDetailsDataSet As New DataSet
				userDetailsAdapter.Fill(userDetailsDataSet, "Table1")
				userDetailsAdapter.Dispose()
				Dim userDetailsTable As DataTable = userDetailsDataSet.Tables(0)

				conn.Close()

				'Fill Cases Report
				If userDetailsTable.Rows.Count <> 0 Then
					If userDetailsTable.Rows(0).Item(3) Is DBNull.Value Then
						moncasesreport.Text = ""
					Else
						moncasesreport.Text = userDetailsTable.Rows(0).Item(3)
					End If

					If userDetailsTable.Rows(1).Item(3) Is DBNull.Value Then
						tuecasesreport.Text = ""
					Else
						tuecasesreport.Text = userDetailsTable.Rows(1).Item(3)
					End If

					If userDetailsTable.Rows(2).Item(3) Is DBNull.Value Then
						wedcasesreport.Text = ""
					Else
						wedcasesreport.Text = userDetailsTable.Rows(2).Item(3)
					End If

					If userDetailsTable.Rows(3).Item(3) Is DBNull.Value Then
						thurcasesreport.Text = ""
					Else
						thurcasesreport.Text = userDetailsTable.Rows(3).Item(3)
					End If

					If userDetailsTable.Rows(4).Item(3) Is DBNull.Value Then
						fricasesreport.Text = ""
					Else
						fricasesreport.Text = userDetailsTable.Rows(4).Item(3)
					End If
				End If
			End If
		End If
	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Faster way to pull data from an sql query?

#2 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 38
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Faster way to pull data from an sql query?

Posted 23 March 2009 - 02:06 PM

So, you are running this code EVERY time?

I would recommend to pull ALL of the data at once into a DataSet and manipulate that, or call ALL of it with a SQL call (with the filter) into the dataset all at once.

thats what I did with my program which has like 750,000 fields.

it runs smooth!
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 6613
  • View blog
  • Posts: 23,952
  • Joined: 12-June 08

Re: Faster way to pull data from an sql query?

Posted 23 March 2009 - 02:25 PM

A few things..

Don't use select *.
When you name your columns in your SQL select statement use an 'ISNULL' to get put the empty string in for you.

Example..
I have columns: col_a and col_b.

I want to select them and replace any nulls with an empty string.

SELECT
ISNULL(col_a, '') as col_a
,ISNULL(col_b, '') as col_b
FROM <table name>
Was This Post Helpful? 0
  • +
  • -

#4 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: Faster way to pull data from an sql query?

Posted 24 March 2009 - 09:40 AM

@Modi

Could you please elaborate a little on what this does: ISNULL(col_a, '')

Regards
/Jens
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Faster way to pull data from an sql query?

Posted 24 March 2009 - 09:49 AM

@jens: ISNULL replaces the null value with a specified value that you provide. Like

SELECT ISNULL(@MyColumn,'IsEmpty') FROM MyTable



If @MyColumn is a NULL value IsEmpty (the stirng) will be returned.

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#6 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Re: Faster way to pull data from an sql query?

Posted 24 March 2009 - 01:48 PM

View PostPsychoCoder, on 24 Mar, 2009 - 08:49 AM, said:

@jens: ISNULL replaces the null value with a specified value that you provide. Like

SELECT ISNULL(@MyColumn,'IsEmpty') FROM MyTable



If @MyColumn is a NULL value IsEmpty (the stirng) will be returned.

Hope that helps :)


Just to clarify would you need the ISNULL(@MyColumn,'IsEmpty') for each column you are grabbing data from IE

"SELECT ISNULL(ColumnA,'IsEmpty'), ISNULL(ColumnB, 'IsEmpty') FROM MyTable"


Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 6613
  • View blog
  • Posts: 23,952
  • Joined: 12-June 08

Re: Faster way to pull data from an sql query?

Posted 24 March 2009 - 02:07 PM

Yes. Hence why using the 'select *' is a bad habit.
Was This Post Helpful? 0
  • +
  • -

#8 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Re: Faster way to pull data from an sql query?

Posted 26 March 2009 - 08:25 AM

View Postmodi123_1, on 24 Mar, 2009 - 01:07 PM, said:

Yes. Hence why using the 'select *' is a bad habit.


When I try this method I am getting the following error:
OledbException was unhandled: Wrong number of arguments used with function in query expression 'ISNull(CasesReport,'IsEmpty''.

The error is triggered when it attempts to fill the dataset

The backend database is ms access 2008 if that makes a difference, and i double checked the name from the database.

Have i got something wrong in the syntax?

Thanks

	Private Sub cboDigit_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDigit.SelectedIndexChanged, cboOffice.SelectedIndexChanged, dtEndDate.ValueChanged, dtStartDate.ValueChanged

		If blnLoad = True Then
			If cboDigit.SelectedIndex <> -1 And cboOffice.SelectedIndex <> -1 Then
				Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= \\Bkesl\VOL2\Common\updates\CaseAdmin\CaseAdmin.accdb")
				conn.Open()

				Dim userDetailsAdapter As New OleDbDataAdapter("SELECT ISNull(CasesReport,'IsEmpty') FROM tblCaseAdmin where Digit =" + cboDigit.Text + " AND Office = '" + cboOffice.Text + "' And Day between #" + dtStartDate.Text + "# and #" + dtEndDate.Text + "#", conn)

				Dim userDetailsDataSet As New DataSet
				userDetailsAdapter.Fill(userDetailsDataSet, "Table1")
				userDetailsAdapter.Dispose()
				Dim userDetailsTable As DataTable = userDetailsDataSet.Tables(0)

				conn.Close()

				'Fill Cases Report
				If userDetailsTable.Rows.Count <> 0 Then
					moncasesreport.Text = userDetailsTable.Rows(0).Item(3)
					tuecasesreport.Text = userDetailsTable.Rows(1).Item(3)
					wedcasesreport.Text = userDetailsTable.Rows(2).Item(3)
					thurcasesreport.Text = userDetailsTable.Rows(3).Item(3)
					fricasesreport.Text = userDetailsTable.Rows(4).Item(3)

				End If
			End If
		End If
	End Sub


Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 6613
  • View blog
  • Posts: 23,952
  • Joined: 12-June 08

Re: Faster way to pull data from an sql query?

Posted 26 March 2009 - 09:20 AM

http://msdn.microsof...10(SQL.80).aspx

It looks like 'isnull' is being set up right.

What is 'CasesReport' column? You should be returning all your column names needed to fill in the adapter's columns.
Was This Post Helpful? 0
  • +
  • -

#10 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Re: Faster way to pull data from an sql query?

Posted 26 March 2009 - 11:01 AM

CasesReport is a text column, was attempting the code with one column for ease of testing, i had thought that using a select statement you could select one column at a time?

I tried adding in all of the columns and received the same results, there is one number column and one date column, the remaining columns are all text
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 6613
  • View blog
  • Posts: 23,952
  • Joined: 12-June 08

Re: Faster way to pull data from an sql query?

Posted 26 March 2009 - 03:04 PM

this is in SQL right? Not something silly like Access, right?
Was This Post Helpful? 0
  • +
  • -

#12 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Re: Faster way to pull data from an sql query?

Posted 27 March 2009 - 06:01 AM

SQL query to pull data from an Acccess database
Was This Post Helpful? 0
  • +
  • -

#13 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 38
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Faster way to pull data from an sql query?

Posted 27 March 2009 - 06:04 AM

Access is weird with SQL calls...and the older the version of access the weirder it gets...it's hit and miss, it could be an access problem, what version of access are you using?
Was This Post Helpful? 0
  • +
  • -

#14 Paul Washburn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 02-October 08

Re: Faster way to pull data from an sql query?

Posted 30 March 2009 - 08:24 AM

Access 2007
Was This Post Helpful? 0
  • +
  • -

#15 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 38
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Faster way to pull data from an sql query?

Posted 30 March 2009 - 08:31 AM

lets see the query, make sure you didn't make a mistake or something.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2