Logo

Creating a Windows Application Using an MSDE Database

 

Introduction

One of the ways you can use or test an MSDE database is to create Windows application. Your computer may have everything that is necessary already. To proceed, you would start by creating a database.

 

Practical Learning Practical Learning: Creating an MSDE Database

  1. At the prompt, type CREATE DATABASE Acquaintances; and press Enter
  2. Type GO and press Enter
  3. In the same way, type the following and press Enter at the end of each line:
     
    CREATE TABLE Members
    (
    FirstName varchar(20),
    LastName varchar(20),
    Gender varchar(24),
    TypeOfRelationship varchar(40)
    );
    GO
    INSERT INTO Members
    VALUES('Gertrude', 'Sandt', 'Female', 'Family Member');
    GO
    INSERT INTO Members
    VALUES('Bertrand', 'Collins', 'Male', 'Friend');
    GO
    INSERT INTO Members
    VALUES('Sandrine', 'Valley', 'Female', 'Coworker');
    GO
    INSERT INTO Members
    VALUES('Claudette', 'Nkomo', 'Female', 'Business Partner');
    GO


     

  4. Type Exit and press Enter
  5. Type Exit again and press Enter

Windows Application Test

 

If you will use a database environment other than Microsoft SQL Server or MSDE, for example if you will use MS Access, Paradox, etc, in order to create a graphical application that uses your SQL database, you can first create a data source. The data source serves as an intermediary object or a translator between your SQL database and Microsoft Windows. Before creating a data source for MySQL, first download the ODBC Connector from the MySQL web site.

If you will use either Microsoft SQL Server or MSDE for our lessons, you can skip the following section.

Windows Creation

After creating the database, you can create a Windows application. To do this, you can use the C# language. If you know C#, fine. If you don't know it, you can learn C# from FunctionX. Normally, you can use a free C# programming environment. For example, you can download SharpDevelop.

In order to use C# in Microsoft Windows, you need the .NET Framework. If you are using Microsoft Windows XP or Windows Server 2003, you already have it. If you are using Windows 2000, you may not have it (yet). To get the .NET Framework, you can download it free from the Microsoft web site. This site assumes that the .NET Framework is installed in your computer. In our Introduction to C# lesson, we review how to use the csc compiler.

Practical Learning Practical Learning: Creating a Windows Application

  1. Start Notepad and type the following. Remember to change the name of the computer in the below Workstation ID assignment and Data Source:
     
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    
    class Exercise : Form
    {
        private DataGrid grdMembers;
        private Button   btnClose;
    
        public Exercise()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
    	// grdMembers
            this.grdMembers = new DataGrid();
    	this.grdMembers.Location = new System.Drawing.Point(16, 8);
    	this.grdMembers.Size = new System.Drawing.Size(376, 128);
    	this.grdMembers.TabIndex = 1;
     
    	// btnClose
    	this.btnClose   = new Button();
            this.btnClose.Location = new System.Drawing.Point(312, 144);
    	this.btnClose.TabIndex = 2;
    	this.btnClose.Text = "Close";
    	this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
    
    	// Exercise Form
    	this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    	this.ClientSize = new System.Drawing.Size(408, 182);
    	this.Controls.Add(this.grdMembers);
    	this.Controls.Add(this.btnClose);
    	this.Text = "Acquaintances";
    	this.Load += new System.EventHandler(this.LoadExercise);			
        }
    
        private void LoadExercise(object sender, System.EventArgs e)
        {
    	SqlConnection conAcquaintances = 
    		new SqlConnection("workstation id=TELES;" +
    				  "packet size=4096;" +
    				  "integrated security=SSPI;" +
    				  "data source=TELES;" +
    				  "persist security info=False;" +
    				  "initial catalog=Acquaintances");
    	SqlDataAdapter slqAdapter = 
    		new SqlDataAdapter("SELECT * FROM Members", conAcquaintances);
    	DataSet dsMembers = new DataSet();
    			
    	slqAdapter.Fill(dsMembers, "Members");
    	grdMembers.DataSource = dsMembers;
    	grdMembers.DataMember = "Members";
        }
    
        private void btnClose_Click(object sender, System.EventArgs e)
        {
    	Close();
        }
    
        static int Main()
        {
    	Application.Run(new Exercise());
    	return 0;
        }
    }
  2. To save the file, on the main menu of Notepad, click File -> Save
  3. Locate your C drive or the drive of your choice and display it in the Save combo box:
  4. Click the Create New Folder button
  5. Type SQL Lessons and press Enter twice to display the new folder in the Save In combo box
  6. Click the Create New Folder button again
  7. Type Acquaintances1 and press Enter twice to display the new folder in the Save In combo box
  8. Change the name of the file to Exercise.cs
     
  9. Click Save
  10. Open the Command Prompt and change to the directory that contains the above file
     
  11. To compile the application, type csc /out:"People In My Life".exe /target:winexe Exercise.cs and press Enter
     
  12. Type Exit to close the Command Prompt
  13. Open Windows Explorer or My Computer and locate the folder that contains the above C# file
     
  14. Double-click the People In My Life icon
     
  15. Close the form
 

Home Copyright © 2004 FunctionX, Inc.