|
-
Apr 14th, 2011, 03:49 PM
#1
Thread Starter
New Member
Console App Help!
Hello!
I am new to vb.net, im in the middle of creating a console application that will return queries from an .sdf database and display the results in the console. My problem is i want to run a few queries one after each other but i have no idea what to do! I have done a similar app in java where I created new methods in the main code which helped but where do i go from here??
The code is as follows
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data.SqlClient
Imports System.Data.SqlServerCe
Module Module1
Dim myConnection As SqlCeConnection
Dim Query As SqlCeCommand
Dim Duration As TimeSpan
Dim StartTime As DateTime = DateTime.Now
Sub Main()
myConnection = New SqlCeConnection("Data Source=C:\Users\Andy\Documents\uniapp5.sdf")
myConnection.Open()
Console.WriteLine("Connection Status: {0}", myConnection.State.ToString())
'run simple select query
Dim DataReader As SqlCeDataReader = Nothing
Query = New SqlCeCommand("Select * from Students JOIN Students_StudentRecord ON Students.StudentId = Students_StudentRecord.StudentId", myConnection)
DataReader = Query.ExecuteReader()
While DataReader.Read()
Console.WriteLine(DataReader("StudentId").ToString())
Console.WriteLine(DataReader("FirstName").ToString())
Console.WriteLine(DataReader("LastName").ToString())
Console.WriteLine(DataReader("ClassificationAwarded").ToString())
Console.WriteLine()
End While
Duration = DateTime.Now.Subtract(StartTime)
Console.WriteLine(String.Format("Query took {0} Seconds", Duration.TotalSeconds.ToString()))
Console.ReadLine()
'close connection
myConnection.Close()
Console.WriteLine("Connection Status: {0}", myConnection.State.ToString())
Console.ReadKey()
End Sub
End Module
Any help is greatly appreciated!
Thanks Andy
-
Apr 14th, 2011, 05:24 PM
#2
Re: Console App Help!
First of all, please use code tags so that your code is properly formatted and can be read effectively. Second, can you please clarify what you actually want? You said:
i want to run a few queries one after each other but i have no idea what to do
And I don't know how to interpret that. Are you saying you just want to run the code you already have there multiple times? Are you saying you want the user to define the query so that they can run whatever query they type?
-
Apr 14th, 2011, 06:06 PM
#3
Thread Starter
New Member
Re: Console App Help!
Hello,
I am wanting to run a different query after that one, like a different SELECT statement that will run and display its results after the first query has returned all its results if that makes any sense?
Andy
-
Apr 14th, 2011, 06:37 PM
#4
Re: Console App Help!
Is there some reason you can't put the code to run the subsequent queries after the code that runs the first query?
vbnet Code:
Sub Main()
myConnection = New SqlCeConnection("Data Source=C:\Users\Andy\Documents\uniapp5.sdf")
myConnection.Open()
Console.WriteLine("Connection Status: {0}", myConnection.State.ToString())
'run simple select query
Dim DataReader As SqlCeDataReader = Nothing
Query = New SqlCeCommand("Select * from Students JOIN Students_StudentRecord ON Students.StudentId = Students_StudentRecord.StudentId", myConnection)
DataReader = Query.ExecuteReader()
While DataReader.Read()
Console.WriteLine(DataReader("StudentId").ToString())
Console.WriteLine(DataReader("FirstName").ToString())
Console.WriteLine(DataReader("LastName").ToString())
Console.WriteLine(DataReader("ClassificationAwarded").ToString())
Console.WriteLine()
End While
Duration = DateTime.Now.Subtract(StartTime)
Console.WriteLine(String.Format("Query took {0} Seconds", Duration.TotalSeconds.ToString()))
Console.ReadLine()
' <--- First Query has finished executing and displaying at this point.
'close connection
myConnection.Close()
Console.WriteLine("Connection Status: {0}", myConnection.State.ToString())
Console.ReadKey()
End Sub
-
Apr 14th, 2011, 06:45 PM
#5
Thread Starter
New Member
Re: Console App Help!
I feel like a bit of a fool, i had tried that before but i had for some reason i had got an error and thought nothing of it. But thanks for the help there Evil_Giraffe! Its been a long day stuck infront of a computer and head in the books studying for exam brain is a bit frazzled!
Thanks again
Andy
-
Apr 14th, 2011, 07:16 PM
#6
Re: Console App Help!
Heh, don't feel like that, we all have those days
-
Apr 18th, 2011, 08:12 AM
#7
Thread Starter
New Member
Re: Console App Help!
Another question im hoping someone might b able to help me with, the doe at the bottom that outputs a time is there anyway i can reset the time so it isnt continous after each query?
For example the first time it will output the query took 0.43534 seconds, but the second time i run it it will return that it took x amount of seconds(the time between hitting enter basicly) i thought because i closed the connection and reopened it that it would reset the time?
Code:
Duration = DateTime.Now.Subtract(StartTime)
Console.WriteLine(String.Format("Query took {0} Seconds", Duration.TotalSeconds.ToString()))
Console.ReadLine()
-
Apr 18th, 2011, 08:24 AM
#8
Re: Console App Help!
If you are going to use the subtraction of one datetime from another as a duration, then use UTC times. The preferred method would be to use a StopWatch.
-
Apr 18th, 2011, 08:36 AM
#9
Thread Starter
New Member
Re: Console App Help!
iv never used the stop watch before, how would the same idea translate into that?
Thanks for the advice!
-
Apr 18th, 2011, 02:51 PM
#10
Re: Console App Help!
StopWatch basics @ MSDN
Code:
Dim stpw As New Stopwatch
'
'
'
stpw.Start() 'start it
'
'
'
Threading.Thread.Sleep(1100) 'simulate some activity - 1.1 secs
'
'
'
stpw.Stop()
Debug.WriteLine(stpw.Elapsed.ToString)
'remeber to reset if you are going to use it again
stpw.Reset() 'reset the elapsed time
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|