run test code

  • Thread starter Vicky via DotNetMonster.com
  • Start date
V

Vicky via DotNetMonster.com

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(string myConnString)
{
string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read())
{
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
}
__________________________________________________________________________
 
L

Lebesgue

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.WriteLine("Type your connection string");
string myConnectionString = Console.ReadLine(); //read your connection
string from console, just to show you how console reading works
ReadMyData(myConnectionString);
Console.ReadLine(); //to be sure your window will not close immediately
}
public static void ReadMyData(string myConnString)
{
//the method definition is here
}
}
 
L

Landi

you would call it like:
ReadMyData(connectionString);
where connectionString is something like:
server=(servername where sql is located);Initial Catalog=(database
name);uid=username;password=password;

server=mymachinename;Initial Catalog=Northwind;uid=sa;password=test;

PS: I would put a try catch on the function as an addition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top