What is C #?

C # (spoken: C Sharp) is an object-oriented programming language that was developed on behalf of Microsoft. In the vast majority of cases it is used as Visual C # in connection with the .NET Framework from Microsoft. Applications for Linux, macOS and Android can also be created in the C # language, among other things thanks to the originally independent Mono framework, which has since been taken over by the Microsoft subsidiary Xamarin.

The first version of the C # language appears in 2001. It has a similar objective as Java and C ++ and is constantly being developed. Version 7.0 of the C # language has been available since March 2017. With the help of the Visual Studio development environment, which is currently available in version 2017, and the .NET Framework, you can program in several languages, including Visual C #. The .NET Framework provides class libraries, programming interfaces, and utilities for developing applications. In addition, a runtime environment is made available for executing the applications.

You can use the freely available community version of the Visual Studio 2017 development environment. It can be downloaded from Microsoft at www.visualstudio.com/de/vs and installed on your Windows PC. The development environment includes an editor for creating the program code, a compiler for creating the executable programs, a debugger for troubleshooting and much more.

What do I do with Visual C #?

Different types of applications can be created with Visual C # and Visual Studio, including:

  • Classic Windows Forms applications with easy-to-create graphical user interfaces and event-oriented programming.
  • Modern WPF applications with XAML. The Windows Presentation Foundation (WPF) class library, which was introduced in 2006, and the eXtensible Application Markup Language (XAML) are used.
  • Database applications with read and write access to many different database systems. To access relational databases, you can work with the ADO.NET framework as part of the .NET framework.
  • Dynamic internet applications in which the web pages serve as interactive user interfaces. The ASP.NET framework is used as part of the .NET framework. ASP stands for Active Server Pages.

An example program

An example database application using Visual C # follows in this section. If you have no programming experience yet, this example is intended to illustrate a typical application, see Figure 1:

  • The creation of a database,
  • the determination and output of information from this database and
  • changing data in this database.

The freely available SQLite is used as the database system. It is straightforward, works with simple files, but according to the SQL standard. It does not require any additional installation of a database server. In the example given, a database is created with standard data if it does not already exist.

If you already have knowledge of another programming language, you will find many familiar elements in the code and the short comments, but also many elements that are specific to Visual C #. Of course, this demo example is not yet suitable for a thorough learning of Visual C #. At this point I refer to my book at Rheinwerk-Verlag .

Figure 1: Database application with Visual C # and SQLite

Structure of the database

A SQLite database is used, which comprises a single table. You can see the structure of the table in Table 1, the sample data in Table 2.

Surname Type
Surname TEXT
First name TEXT
personnel number INTEGER PRIMARY KEY
salary REAL
date of birth TEXT

Table 1: “firma.db” database, “people” table, structure

Surname First name personnel number salary date of birth
Mertens Julia 2297 3621.5 12/30/1959
Maier Hans 6714 3500 03/15/1962
Schmitz Peter 81343 3750 04/12/1958

Table 2: “firma.db” database, “people” table, standard data

The code of the program

The following is the program code in the “Form1.cs” file:
using System;
// Namensraum mit Klassen der SQLite-Bibliothek verfügbar machen
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;

// Namensraum für dieses Visual-C#-Projekt
namespace DBZugriffSQLite
{
// Klasse für das Formular des Projekts
public partial class Form1 : Form
{
// Standardkonstruktor für die Formularklasse
public Form1()
{
InitializeComponent();
}

// Ereignis: Formular wird geladen
private void Form1_Load(object sender, EventArgs e)
{
// Falls die Datenbank bereits existiert: Ende
if (File.Exists(“firma.db”))
return;

// Ab hier: Datenbank neu erzeugen

// Verbindungsobjekt erzeugen
SQLiteConnection con = new SQLiteConnection();
// Verweis auf Objekt für SQL-Befehl erzeugen
SQLiteCommand cmd;
string cmdBeginn;

// Verbindung zu Datenbank bereitstellen
con.ConnectionString = “Data Source=firma.db;”;
// Objekt für SQL-Befehl bereitstellen
cmd = con.CreateCommand();

// Ausnahmebehandlung
try
{
// Verbindung öffnen
con.Open();

// SQL-Befehl zur Erzeugung der Tabelle mit fünf Feldern
cmd.CommandText = “CREATE TABLE personen (name TEXT, ”
+ “vorname TEXT, personalnummer INTEGER PRIMARY KEY, ”
+ “gehalt REAL, geburtstag TEXT)”;
// SQL-Befehl absenden
cmd.ExecuteNonQuery();

// SQL-Befehl zur Erzeugung eines Datensatzes, Beginn
cmdBeginn = “INSERT INTO personen (name, vorname, ”
+ “personalnummer, gehalt, geburtstag) VALUES “;

// SQL-Befehl für ersten Datensatz vervollständigen
cmd.CommandText = cmdBeginn
+ “(‘Maier’, ‘Hans’, 6714, 3500, ‘15.03.1962’)”;
cmd.ExecuteNonQuery();

// … für zweiten Datensatz
cmd.CommandText = cmdBeginn
+ “(‘Schmitz’, ‘Peter’, 81343, 3750, ‘12.04.1958’)”;
cmd.ExecuteNonQuery();

// … für dritten Datensatz
cmd.CommandText = cmdBeginn
+ “(‘Mertens’, ‘Julia’, 2297, 3621.5, ‘30.12.1959’)”;
cmd.ExecuteNonQuery();

// Verbindung schließen
con.Close();
}
// Falls ein Fehler auftritt, zum Beispiel im SQL-Befehl
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

// Ereignis: Benutzer hat Schaltfläche “Alle sehen” betätigt
private void CmdAlleSehen_Click(object sender, EventArgs e)
{
SQLiteConnection con = new SQLiteConnection();
SQLiteCommand cmd;
// Verweis auf Objekt für Ergebnis der Datenbankabfrage erzeugen
SQLiteDataReader reader;

con.ConnectionString = “Data Source=firma.db;”;
cmd = con.CreateCommand();
// SQL-Befehl: Wähle alle Felder für alle Datensätze aus
cmd.CommandText = “SELECT * FROM personen”;

try
{
con.Open();
// SQL-Befehl mit Datenbankabfrage senden, Ergebnis empfangen
reader = cmd.ExecuteReader();
// ListBox leeren
LstAnzeige.Items.Clear();
// Alle Datensätze des Ergebnisses durchlaufen
while (reader.Read())
{
// Zeige Inhalte der Felder für den aktuellen Datensatz an
LstAnzeige.Items.Add(reader[“name”] + ” # ”
+ reader[“vorname”] + ” # ”
+ reader[“personalnummer”] + ” # ”
+ reader[“gehalt”] + ” # ”
+ reader[“geburtstag”]);
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

// Ereignis: Eine der beiden Schaltflächen “Gehälter …” wurde betätigt
private void CmdAendern_Click(object sender, EventArgs e)
{
SQLiteConnection con = new SQLiteConnection();
SQLiteCommand cmd;
int anzahl;
string op;

con.ConnectionString = “Data Source=firma.db;”;
cmd = con.CreateCommand();

// Welche der beiden Schaltflächen wurde betätigt?
if (sender == CmdErhoehen) op = “*”;
else op = “/”;
// SQL-Befehl zum Ändern der Datensätze
cmd.CommandText = “UPDATE personen SET gehalt = gehalt ”
+ op + ” 1.05″;

try
{
con.Open();
// SQL-Befehl senden, Anzahl der geänderten Datensätze empfangen
anzahl = cmd.ExecuteNonQuery();
// Anzahl anzeigen
MessageBox.Show(“Datensätze geändert: ” + anzahl);
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

C # (programming language)