Connecting to a SQL Server Database Using C#

Written by admin on February 24, 2013 Categories: C#

PREREQUISITES

  • Visual Studio Express 2012 for Windows Desktop
  • AdventureWorks 2008 R2 Database

INTRODUCTION

Connecting to a database, especially Microsoft SQL Server, is simple.  But, if you’re going to do any type of programming you’re going to need to know how.  In today’s applications everything is database driven.  Users want flexibility, personalization, redundancy, and more.  But if you’re new to C# it’s not immediately evident how you might connect to a SQL Server database with the .NET Framework out of the box.

So we are going to do just that in this video.  In this video you will cover the following things:

  1. Establishing the data you want.
  2. Creating a console application.
  3. Establishing a connection to the database.
  4. Selecting data from the database.
  5. Displaying data from the database.
  6. Cleaning up your connection.

No Comments

Creating a New Project in Visual Studio Express 2012 Using the Start Page

Written by admin on February 19, 2013 Categories: Visual Studio Express 2012 for Web, Visual Studio Express 2012 for Windows Desktop

To do anything in Visual Studio you’re going to need to know how to create a new project. In this article I’m going to break down the abstracted view on creating a project in Visual Studio Express 2012. When I say abstracted I mean this same process – though the screenshots are from the Windows Desktop version – works for both Windows Desktop and Web.

Of course there are numerous other ways of getting to the New Project interface, but today we are going to focus on the Start Page.

OPEN THE START PAGE

The Visual Studio Express 2012 Start Page.If the Start Page isn’t already open, let’s get it open by clicking View and then Start Page. You can do a lot of things from the Start Page, and it’s going to become a pivotal part of your development experience, but today let’s stay focused on just one of the links that exists in the top left hand corner of the page – New Project…

CREATE A NEW PROJECT

The Visual Studio Express 2012 New Project DialogClicking on that link will present you with the dialog you see above, and this is where the magic begins. We are about to create our first new project! However, before we can continue, we need to determine what kind of project we want, so pick the project type you’re looking for and click OK.

Congratulations! You just created a new project in Visual Studio Express 2012.

1 Comment

The Quintessential Hello World

Written by admin on  Categories: C#

Note that I am still editing this post and I will be adding platforms over time. But in an effort to get even more content out there for the community it’s going to be bits and pieces over time. When it’s all said and done I want to have the Hello World application using the console, Windows Forms, WPF, ASP.NET, ASP.NET MVC, and Silverlight.

So keep checking back, sign up for our RSS feed, and keep sending new programmers this way!

PREREQUISITES

OVERVIEW OF THE QUINTESSENTIAL HELLO WORLD

Hello World is the most common programming example – anywhere. But one thing I’ve yet to see is the quintessential hello world. You know, the one that not only exemplifies it in a console application, but also exemplifies it in many other platforms. Well today you’re in luck because that’s exactly what I’m going to do.

Before we get into the code let’s get a set of specifications together so we know what we’re building. The customer, ACME, wants us to build an application that will tell the user “Hello” when they open it up – and since they don’t want the user to login – they just want it to say “Hello World!”

THE CONSOLE APPLICATION

The console application has got to be the most common example – anywhere – but for this to be the quintessential hello world we are going to have to include it.

  1. Open Visual Studio Express 2012 for Windows Desktop.
  2. Create a new project and select the Console Application template.
  3. Name the project TheQuintessentialHelloWorld and click OK.

Once you click OK you’ll notice that Visual Studio creates a project and seeds it with the proper files for a basic Windows console application.

Open up the Program.cs file, and add the following code to the Main method:

Console.WriteLine("Hello World!");
 
if (Debugger.IsAttached)
{
    Console.ReadLine();
}

so when you’re done your program should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Diagnostics;
 
namespace TheQuintessentialHelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
 
            if (Debugger.IsAttached)
            {
                Console.ReadLine();
            }
        }
    }
}

Press F5 and you’ll notice Visual Studio doing some things, and then a console window will popup, and it should say Hello World!

Congratulations! You just created the Hello World application using the Windows console!

THE WINDOWS APPLICATION

So the Windows application is probably the next most common example, so to ensure that this is in fact the quintessential hello world post we are going to have to include it – but we’re going to have to +1 all of the others out there. We’re going to do it for both Windows Forms and WPF.

WINDOWS FORMS

  1. Open Visual Studio Express 2012 for Windows Desktop.
  2. Create a new project and select the Windows Forms Application template.
  3. Name the project TheQuintessentialHelloWorld_WF and click OK.

You’ll notice that Visual Studio has created a new project and there is a file in there named Form1.cs. You’ll also notice there is a visual designer in front of you with the Form1.cs file already open. Now there’s a lot we could cover here, but let’s stay focused, we’re building a hello world application, so let’s put a label on here and get started.

  1. Open up the Toolbox by clicking on it’s anchor on the left hand side of the Visual Studio window.
  2. Now expand the Common Controls band and left-click, hold, drag, and then drop a Label control onto the design surface.
  3. Press CTRL+ALT+0 – this will open up the code window for this form.
  4. Place the following code after the InitializeComponent() call.
this.label1.Text = "Hello World!";

So, with that line, your code should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace TheQuintessentialHelloWorld_WF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            this.label1.Text = "Hello World!";
        }
    }
}

Press F5, you’ll notice that Visual Studio is doing some things, and when it’s done you’ll get a screen that says Hello World!

Congratulations! You just created the Hello World application in a Windows Forms application!

WPF

So we’ve created the Hello World application in the console, and in Windows Forms, but there is one other available Windows application – WPF – or Windows Presentation Foundation. There are some minor differences between this one and the Windows Forms application, but in the end, we’ll get a window that loads in the OS that says Hello World!

No Comments