Results 1 to 10 of 10

Thread: Console App Help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    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

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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?

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    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

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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:
    1. Sub Main()
    2.  
    3.     myConnection = New SqlCeConnection("Data Source=C:\Users\Andy\Documents\uniapp5.sdf")
    4.  
    5.     myConnection.Open()
    6.     Console.WriteLine("Connection Status: {0}", myConnection.State.ToString())
    7.  
    8.     'run simple select query
    9.     Dim DataReader As SqlCeDataReader = Nothing
    10.     Query = New SqlCeCommand("Select * from Students JOIN Students_StudentRecord ON Students.StudentId = Students_StudentRecord.StudentId", myConnection)
    11.     DataReader = Query.ExecuteReader()
    12.  
    13.     While DataReader.Read()
    14.         Console.WriteLine(DataReader("StudentId").ToString())
    15.         Console.WriteLine(DataReader("FirstName").ToString())
    16.         Console.WriteLine(DataReader("LastName").ToString())
    17.         Console.WriteLine(DataReader("ClassificationAwarded").ToString())
    18.  
    19.         Console.WriteLine()
    20.     End While
    21.  
    22.     Duration = DateTime.Now.Subtract(StartTime)
    23.     Console.WriteLine(String.Format("Query took {0} Seconds", Duration.TotalSeconds.ToString()))
    24.     Console.ReadLine()
    25.  
    26.     ' <--- First Query has finished executing and displaying at this point.
    27.  
    28.     'close connection
    29.     myConnection.Close()
    30.     Console.WriteLine("Connection Status: {0}", myConnection.State.ToString())
    31.  
    32.     Console.ReadKey()
    33.  
    34. End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    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

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Console App Help!

    Heh, don't feel like that, we all have those days

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    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()

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    Re: Console App Help!

    iv never used the stop watch before, how would the same idea translate into that?

    Thanks for the advice!

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width