run test code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vicky via DotNetMonster.com

    run test code

    HI,

    I found some sample code in .net documentation, and want to run it in
    console application.

    I am not sure how to call it in Main, could anyone help.
    Thanks!

    _______________ _______________ _______________ _______________ __________
    public void ReadMyData(stri ng myConnString)
    {
    string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
    SqlConnection myConnection = new SqlConnection(m yConnString);
    SqlCommand myCommand = new SqlCommand(mySe lectQuery,myCon nection);
    myConnection.Op en();
    SqlDataReader myReader;
    myReader = myCommand.Execu teReader();
    // Always call Read before accessing data.
    while (myReader.Read( ))
    {
    Console.WriteLi ne(myReader.Get Int32(0) + ", " + myReader.GetStr ing(1));
    }
    // always call Close when done reading.
    myReader.Close( );
    // Close the connection when done with it.
    myConnection.Cl ose();
    }
    _______________ _______________ _______________ _______________ ______________

    --
    Message posted via http://www.dotnetmonster.com
  • Lebesgue

    #2
    Re: run test code

    You need to write a class which will have this method.
    If you need just to test, the class which contains the Main method will do
    fine.
    If you are using VS.NET, create new console application project and paste
    your method to the Class1 that VS has generated for you.
    Otherwise, you need to write you class in another editor, notepad will be
    quite enough for a quick start.
    So this is how it should look like

    using System;
    class Class1
    {
    public static void Main(string[] args)
    {
    Console.WriteLi ne("Type your connection string");
    string myConnectionStr ing = Console.ReadLin e(); //read your connection
    string from console, just to show you how console reading works
    ReadMyData(myCo nnectionString) ;
    Console.ReadLin e(); //to be sure your window will not close immediately
    }
    public static void ReadMyData(stri ng myConnString)
    {
    //the method definition is here
    }
    }



    "Vicky via DotNetMonster.c om" <forum@nospam.D otNetMonster.co m> wrote in
    message news:d295852395 f6417a906e28140 60e4073@DotNetM onster.com...[color=blue]
    > HI,
    >
    > I found some sample code in .net documentation, and want to run it in
    > console application.
    >
    > I am not sure how to call it in Main, could anyone help.
    > Thanks!
    >
    > _______________ _______________ _______________ _______________ __________
    > public void ReadMyData(stri ng myConnString)
    > {
    > string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
    > SqlConnection myConnection = new SqlConnection(m yConnString);
    > SqlCommand myCommand = new SqlCommand(mySe lectQuery,myCon nection);
    > myConnection.Op en();
    > SqlDataReader myReader;
    > myReader = myCommand.Execu teReader();
    > // Always call Read before accessing data.
    > while (myReader.Read( ))
    > {
    > Console.WriteLi ne(myReader.Get Int32(0) + ", " + myReader.GetStr ing(1));
    > }
    > // always call Close when done reading.
    > myReader.Close( );
    > // Close the connection when done with it.
    > myConnection.Cl ose();
    > }
    > _______________ _______________ _______________ _______________ ______________
    >
    > --
    > Message posted via http://www.dotnetmonster.com[/color]


    Comment

    • Landi

      #3
      Re: run test code

      you would call it like:
      ReadMyData(conn ectionString);
      where connectionStrin g is something like:
      server=(servern ame where sql is located);Initia l Catalog=(databa se
      name);uid=usern ame;password=pa ssword;

      server=mymachin ename;Initial Catalog=Northwi nd;uid=sa;passw ord=test;

      PS: I would put a try catch on the function as an addition.
      --
      info@donotspam dowhileloop.com
      http://www.dowhileloop.com web development
      http://publicjoe.dowhileloop.com -- C# Tutorials

      "Vicky via DotNetMonster.c om" <forum@nospam.D otNetMonster.co m> wrote in
      message news:d295852395 f6417a906e28140 60e4073@DotNetM onster.com...[color=blue]
      > HI,
      >
      > I found some sample code in .net documentation, and want to run it in
      > console application.
      >
      > I am not sure how to call it in Main, could anyone help.
      > Thanks!
      >
      > _______________ _______________ _______________ _______________ __________
      > public void ReadMyData(stri ng myConnString)
      > {
      > string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
      > SqlConnection myConnection = new SqlConnection(m yConnString);
      > SqlCommand myCommand = new SqlCommand(mySe lectQuery,myCon nection);
      > myConnection.Op en();
      > SqlDataReader myReader;
      > myReader = myCommand.Execu teReader();
      > // Always call Read before accessing data.
      > while (myReader.Read( ))
      > {
      > Console.WriteLi ne(myReader.Get Int32(0) + ", " + myReader.GetStr ing(1));
      > }
      > // always call Close when done reading.
      > myReader.Close( );
      > // Close the connection when done with it.
      > myConnection.Cl ose();
      > }
      > _______________ _______________ _______________ _______________ ______________
      >
      > --
      > Message posted via http://www.dotnetmonster.com[/color]


      Comment

      • Vicky via DotNetMonster.com

        #4
        Re: run test code

        Lebesgue,

        Thank you very much!

        Vicky

        --
        Message posted via http://www.dotnetmonster.com

        Comment

        • Vicky via DotNetMonster.com

          #5
          Re: run test code

          Landi,

          Thanks a lot!

          Vicky

          --
          Message posted via http://www.dotnetmonster.com

          Comment

          Working...