Not a member? click here

Forgot your password? click here

Sample Code

Following is a very simple program that demonstrates searching and updating. It uses the Customers table from the Northwind database. The main method calls three methods. The first initializes Dali to use the specified SQL Server™ database. The second searches for all customers in the database that are in London and creates Customer objects for them. It then displays some customer info. The third selects a specific customer using its primary key and updates the contact's title.

This code, along with the Customer class represents an entire working program. You can find the Customer class code here. You may also review the Dali User's Guide, which provides more code examples in C# and VB.NET as well as step-by-step instructions for creating programs using Dali.


using System;
using System.Collections.Generic;
using Revelation.Dali;

namespace Revelation.Sample
{
    class Program
    {
        private static DataManager dataManager;

        private static void Main(string[] args)
        {
            InitializeDali();
            FindExample();
            UpdateExample();
            Console.Write("Press any key to exit...");
            Console.ReadKey();
        }

        private static void InitializeDali()
        {
            string connectString = 
                "server=dbsvr;database=Northwind;" +
                "trusted_connection=true";
            dataManager = new DataManager(
                new SqlDaliProvider(), connectString);
        }

        private static void FindExample()
        {
            // Create a list of all customers in London, 
            // ordered by company
            List<Customer> customers = 
                dataManager.Find<Customer>(
                    "City = 'London'", "CompanyName");

            // Iterate the list, displaying info
            foreach (Customer customer in customers)
            {
                Console.WriteLine(customer.CompanyName);
                Console.WriteLine(customer.ContactName);
                Console.WriteLine(customer.Phone);
                Console.WriteLine();
            }
            Console.WriteLine("----------------");
        }

        private static void UpdateExample()
        {
            // Get a specific customer by Primary Key
            Customer customer = new Customer();
            customer.CustomerID = "CACTU";
            dataManager.Load(customer);

            // Display this customer's info
            Console.WriteLine(
                "ID: {0}, Name: {1}, Old Title: {2}",
                customer.CustomerID, 
                customer.ContactName, 
                customer.ContactTitle);

            // Remember the contact's current title 
            // so we can restore it later
            string oldTitle = customer.ContactTitle;

            // Change the contact's title
            customer.ContactTitle = "Sales Manager";

            // Save it to the database
            dataManager.Save(customer);

            // Just to show it changed we'll create a new
            // customer object and load it
            Customer customer2 = new Customer();
            customer2.CustomerID = "CACTU";
            dataManager.Load(customer2);

            // Display this customer's info
            Console.WriteLine(
                "ID: {0}, Name: {1}, New Title: {2}",
                customer2.CustomerID, 
                customer2.ContactName, 
                customer2.ContactTitle);

            // Put the data back the way it was
            customer2.ContactTitle = oldTitle;
            dataManager.Save(customer2);
        }
    }
}