Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver

In the previous sections, you saw programs that connect to various databases, retrieve a row from a table, and display the column values for that row on your computer screen. This type of program is known as a console application because it displays output directly on the screen on which the program is running.

You can use Visual Studio .NET (VS .NET) to create console applications, as well as the following types of applications:

This is not an exhaustive list of the types of applications you can develop with VS .NET, but it does give you flavor for the broad range of VS .NET's capabilities.

In the rest of this section, you'll see how to develop and run a console application using VS .NET. If you've installed VS .NET on your computer, you'll be able to follow along with the example. If you don't have VS .NET, don't worry; you'll still be able to see what's going on from the figures provided.

Starting Visual Studio .NET and Creating a Project

All of your work in VS .NET is organized into projects. Projects contain the source and executable files for your program, among other items. If you have VS .NET installed, start it by selecting Start ➣ Programs ➣ Microsoft Visual Studio .NET ➣ Microsoft Visual Studio .NET. Once VS .NET has started, you'll see the Start page (see Figure 1.1).

Figure 1.1: The Start page

From the Start page, you can see any existing projects you've created. You can open and create projects using the Open Project and New Project buttons, respectively. You'll create a new project shortly.

Using the VS .NET Links

As you can see from Figure 1.1, VS .NET contains a number of links on the left of the Start page. Some of these links provide access to useful information on the Internet about .NET; the links are as follows:

Click these links and explore the information provided. As you'll see, there's a lot of information about .NET on the Internet.

Creating a New Project

When you're finished examining the information in the previous links, create a new project by clicking the New Project button on the Get Started page.

Note 

You can also create a new project by selecting File ➣ New ➣ Project, or by pressing Ctrl+Shift+N on your keyboard.

When you create a new project, VS .NET displays the New Project dialog box, which you use to select the type of project you want to create. You also enter the name and location of your new project; the location is the directory where you want to store the files for your project.

Because you're going to be creating a C# console application, select Visual C# Projects from the Project Types section on the left of the New Project dialog box, and select Console Application from the Templates section on the right. Enter MyConsoleApplication in the Name field, and keep the default directory in the Location field. Figure 1.2 shows the completed New Project dialog box with these settings.

Figure 1.2: The New Project dialog box with the appropriate settings for a C# console application

Click the OK button to create the new project.

Working in the VS .NET Environment

Once you've created a new project, the main development screen is displayed (see Figure 1.3). This screen is the environment in which you'll develop your project. As you can see, VS .NET has already created some starting code for you. This code is a skeleton for your program; you'll see how to modify it shortly. In this section, I'll give you a brief description of the different parts of the VS .NET environment.

Figure 1.3: The VS .NET environment

Note 

Depending on your settings for VS .NET, your screen might look slightly different from that shown in Figure 1.3.

The VS .NET menu contains the following items:

The VS .NET toolbar contains a series of buttons that act as shortcuts to some of the menu options. For example, you can save a file or all files, cut and paste text from the Clipboard, and start a program using the debugger. You'll learn how to use some of these features later in this chapter.

The code shown in the window (below the toolbar) with the title Class1.cs is code that is automatically generated by VS .NET, and in the next section you'll modify this code.

Modifying the VS .NET-Generated Code

Once VS .NET has created your project, it will display some starting code for the console application with a class name of Class1.cs. You can use this code as the beginning for your own program. Figure 1.3, shown earlier, shows the starting code created by VS .NET.

The Main() method created by VS .NET is as follows:

static void Main(string[] args) { // // TODO: Add code to start application here // }

As you can see, this code contains comments that indicate where you add your own code. Replace the Main() method with the following code taken from the Main() method in FirstExample.cs, shown earlier in Listing 1.1:

public static void Main() { try { // step 1: create a SqlConnection object to connect to the // SQL Server Northwind database SqlConnection mySqlConnection = new SqlConnection( "server=localhost;database=Northwind;uid=sa;pwd=sa" ); // step 2: create a SqlCommand object SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); // step 3: set the CommandText property of the SqlCommand object to // a SQL SELECT statement that retrieves a row from the Customers table mySqlCommand.CommandText = "SELECT CustomerID, CompanyName, ContactName, Address "+ "FROM Customers "+ "WHERE CustomerID = 'ALFKI'"; // step 4: open the database connection using the // Open() method of the SqlConnection object mySqlConnection.Open(); // step 5: create a SqlDataReader object and call the ExecuteReader() // method of the SqlCommand object to run the SELECT statement SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(); // step 6: read the row from the SqlDataReader object using // the Read() method mySqlDataReader.Read(); // step 7: display the column values Console.WriteLine("mySqlDataReader[\" CustomerID\"] = "+ mySqlDataReader["CustomerID"]); Console.WriteLine("mySqlDataReader[\" CompanyName\"] = "+ mySqlDataReader["CompanyName"]); Console.WriteLine("mySqlDataReader[\" ContactName\"] = "+ mySqlDataReader["ContactName"]); Console.WriteLine("mySqlDataReader[\" Address\"] = "+ mySqlDataReader["Address"]); // step 8: close the SqlDataReader object using the Close() method mySqlDataReader.Close(); // step 9: close the SqlConnection object using the Close() method mySqlConnection.Close(); } catch (SqlException e) { Console.WriteLine("A SqlException was thrown"); Console.WriteLine("Number = "+ e.Number); Console.WriteLine("Message = "+ e.Message); Console.WriteLine("StackTrace:\n" + e.StackTrace); } }

Note 

You'll also need to add the following line near the start of your class: using System.Data.SqlClient;

Once you've added the previous code, your next steps are to compile and run your program.

Compiling and Running the Program Using VS .NET

As always, you must first compile your program before you can run it. Because programs in VS .NET are organized into projects, you must compile the project; this is also known as building the project. To build your project, select Build ➣ Build Solution. This compiles the Class1.cs source file into an executable file.

Tip 

You can also press Ctrl+Shift+B on your keyboard to build your project.

Finally, you can now run your program. Select Debug ➣ Start Without Debugging. When you select Start Without Debugging, the program will pause at the end, allowing you to view the output.

Tip 

You can also press Ctrl+F5 on your keyboard to run your program.

When you run your program, VS .NET will run the program in a new Command Prompt window, as shown in Figure 1.4. Your program is run in this window because it is a console application.

Figure 1.4: The running program

To end the program, press any key. This will also close the Command Prompt window.

You've barely scratched the surface of VS .NET in this section. You'll explore some of the other features of VS .NET later in this book. In the next section, you'll learn how to use the extensive documentation that comes with .NET.

Категории