Net.Core

NET console application using Visual Studio Code 101

https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code

Extensions I use, C# is the one to install now.

Prerequisites
Visual Studio Code with the C# extension installed. For information about how to install extensions on Visual Studio Code, see VS Code Extension Marketplace.
The .NET 5.0 SDK or later

Yea…

The folder name becomes the project name and the namespace name by default. You’ll add code later in the tutorial that assumes the project namespace is HelloWorld.

Terminal

dotnet new console

Run the app

Run the following command in the Terminal:

Enhance the app

Open Program.cs by clicking on it.

The first time you open a C# file in Visual Studio Code, OmniSharp loads in the editor.
Select Yes when Visual Studio Code prompts you to add the missing assets to build and debug your app.

After you have done some changes, In Visual Studio Code, you have to explicitly save changes. Unlike Visual Studio, file changes are not automatically saved when you build and run an app.

Great, we done some changes, not lets add a separate class in a different folder.

Classes and Objects:

https://www.tutlane.com/tutorial/csharp/csharp-classes-and-objects-with-examples

So here we created a new class and made two instances of it, the field W_id and Name we can get, the property Location we can set and get.

Some more refresh on dotnet, arry, arraylist (using systems.collections), hashtable (dictionary) and the cool goto with for and foreach st.

using System;
using System.Collections;

namespace HelloWorld
{
    // class name
    class Worker
    {
        // fields
        public int W_id;
        public string Name;
        // constructor
        public Worker(string name)
        {
            Random rnd = new Random();
            W_id = rnd.Next(1, 5555);
            Name = name;
            work();

        }
        public void work()
        {
            Console.WriteLine("loops");
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(i);

            }

            for (int i = 0, j = 1; i < 3; i++, j++)
            {
                Console.WriteLine(i + " " + j);
            }
            string[] names = new string[3];
            names[0] = "Dimmu B";
            names[1] = "Gorgoroth";
            names[2] = "Trelldom";

            for (int i = 0; i < names.Length; i++)
            {
                Console.WriteLine(names[i]);
            }

            ArrayList arr = new ArrayList();
            arr.Add(45);
            arr.Add(55);
            foreach (int aa in arr)
            {
                Console.WriteLine(aa);
            }
            Hashtable ht = new Hashtable();
            ht.Add("Jones", "Tom");
            ht.Add("Oneil", "Jane");
            ht.Add("Boris", "Petro");
            foreach (DictionaryEntry item in ht)
            {
                Console.WriteLine(item.Key + " " + item.Value);
                if (item.Value.ToString() == "Petro")
                {
                    goto removeit;
                }

            }
        removeit:
            Console.WriteLine("Can safefully remove it");

        }
        // method
        public void getDetails()
        {
            Console.WriteLine(Name + " " + W_id + " " + Location);
        }
        // properties
        public string Location { get; set; }



    }
}

Add a package

Go to terminal and type

dotnet add package System.Data.SqlClient



using System.Data.SqlClient;

public void listDatabases()
        {
            try
            {

                using (SqlConnection sqlCon = new SqlConnection(connectionString))
                {
                    string q = "SELECT name, database_id FROM sys.databases";
                    SqlCommand command = new SqlCommand(q, sqlCon);
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    SqlDataReader sqlReader = command.ExecuteReader();
                    while(sqlReader.Read()) {
                        string name = sqlReader["name"].ToString();
                        string id = sqlReader["database_id"].ToString();
                        Console.WriteLine(name + " " + id);
                    }
                }

            }
            catch (SqlException e)
            {
                Console.WriteLine(e);
            }

        }

Debug a .NET console application using Visual Studio Code

Comments Off on NET console application using Visual Studio Code 101