Starcounter
HomeDownloadDocsCommunity
2.3.1
2.3.1
  • Starcounter Documentation
  • Getting Started
  • Starcounter
    • Collapsing the Stack
      • Complexity and Scalability Tradeoff
      • The Future of Micro-Services
      • 10 Benefits of Collapsing the Stack
    • Integrated Database and Web Server
  • Hello World - Tutorial
    • Create a Database Class
    • Create a Real Time UI
    • First Interactive UI
    • Computed Properties
    • Expense Tracker
    • Cancel and Delete
    • The Next Step
  • Guides
    • Database
      • Database Classes
      • Data manipulation
      • Object Identity and Object References
      • Querying with SQL
      • Data Types
      • Relations
      • Inheritance
      • Sharing data
      • Database Configuration
      • Comparing Database Objects
      • Referential Integrity and Constraints
    • SQL
      • Identifiers
      • Path Expressions
      • Data operators
      • Joins
      • Aggregates
      • Comparisons and Logical Operators
      • Sorting
      • Fetch
      • Offset Key
      • Indexes
      • Literals
      • Query Plan Hints
      • Reserved words
      • Query for Database Classes
      • SQL Isolation Between Applications
    • Transactions
      • Short-Running Transactions
      • Long running transactions
      • Using Transactions
      • Running Background Jobs
      • Commit Hooks
    • Typed JSON
      • JSON-by-example
      • Code-Behind
      • Data Bindings
      • Callback Methods
      • Responding with JSON
      • Accepting JSON in Requests
      • Primitive Arrays and Single Value Types
      • Typed JSON Internals
    • Blendable Web Apps
      • Starcounter MVVM
      • Palindrom
      • Client-Side Stack
      • Sessions
      • HTML Views
      • App Shell
      • Web Components
      • View Attaching
      • View Composing
      • HTML Compositions
      • HTML Views Blending Guidelines
      • Avoiding CSS conflicts
      • Debugging
    • Network
      • HTTP
      • Internal Self Calls
      • Middleware
      • Anonymous or Substitute Handlers
      • URL Aliases and Redirects
      • Network Gateway
      • Static File Server
      • External HTTP Calls
      • WebSocket
      • Avoiding URI conflicts
      • TCP Sockets
      • UDP Sockets
    • Publishing Apps
    • Working with Starcounter
      • Release Channels
      • Starting and Stopping Apps
      • Administrator Web UI
      • Star CLI
      • StarAdmin CLI
      • StarDump CLI
      • Working in Visual Studio
      • Error Log
      • Using HTTPS on NGINX
      • Using HTTPS on IIS
      • Run Starcounter in Production
      • Weaver
      • Investigating App Crashes
      • Configuration Structure
      • Database Refactoring
      • Using Unload/Reload to Modify Database Schema
      • Kernel Questions and Answers
      • Log Files
  • Cookbook
    • Attach an HTTP Request to an Existing Long-Running Transaction
    • Cookie-Based Authentication
    • Timestamp on Object Creation
    • Creating Strongly Typed JSON Collections
    • Migrating From 2.2 to 2.3+
    • Multiple Pages
    • Icons
    • Proposed Project Structure
    • Acceptance Testing with Selenium
    • Requesting a User to Authenticate
    • How to delete unused tables and columns
Powered by GitBook
On this page
  • Add a Persistent Class
  • Add an Instance to the Class
  • Result
  1. Hello World - Tutorial

Create a Database Class

PreviousHello World - TutorialNextCreate a Real Time UI

Last updated 7 years ago

Welcome to the first part of our Hello World tutorial!

We will start by creating a Starcounter application in Visual Studio by going to New Project -> Templates -> Visual C# -> Starcounter -> Starcounter Application. We will name the application HelloWorld.

Add a Persistent Class

Create a new class called Person with the attribute in the Program.cs file. This attribute tag will make all instances of the class persistent.

Add the properties FirstName and LastName to this class. Your code should now look like this:

Program.cs
using Starcounter;

namespace HelloWorld
{
    [Database]
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    class Program
    {
        static void Main()
        {

        }
    }
}

Add an Instance to the Class

Program.cs
class Program
{
    static void Main()
    {
        Db.Transact(() =>
        {
            var person = Db.SQL<Person>("SELECT p FROM Person p")
                .FirstOrDefault();
            if (person == null)
            {
                new Person
                {
                    FirstName = "John",
                    LastName = "Doe"
                };
            }
        });
    }
}

The if statement here checks if you already have a Person in the database by accessing the first result that we get from the query. If that is the case, you do not need to create a new one. Without it, we would create a new instance of Person every time we run the program, which we do not intend to do. Remember to import System.Linq for FirstOrDefault.

Result

Start your program with Starcounter by clicking F5 in Visual Studio. To see for yourself, open the administrator at localhost:8181/#/databases/default/sql and enter SELECT * FROM HelloWorld.Person. This will display all the instances, represented as rows, of the Person class. Note that these instances are persistent. You can restart the application, or even the computer, and the instances will still be there.

For the next step, we'll add a UI which will help us to display the data in the browser.

Add a first instance to the class by defining a new person, its properties, and wrapping it in a . Using a transaction allows us to access database objects and makes the changes inside the transaction atomic and isolated.

If you get any errors, check your code against the .

[Database]
Transactions
source code
Db.Transact()