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()
{
List<Customer> customers =
dataManager.Find<Customer>(
"City = 'London'", "CompanyName");
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()
{
Customer customer = new Customer();
customer.CustomerID = "CACTU";
dataManager.Load(customer);
Console.WriteLine(
"ID: {0}, Name: {1}, Old Title: {2}",
customer.CustomerID,
customer.ContactName,
customer.ContactTitle);
string oldTitle = customer.ContactTitle;
customer.ContactTitle = "Sales Manager";
dataManager.Save(customer);
Customer customer2 = new Customer();
customer2.CustomerID = "CACTU";
dataManager.Load(customer2);
Console.WriteLine(
"ID: {0}, Name: {1}, New Title: {2}",
customer2.CustomerID,
customer2.ContactName,
customer2.ContactTitle);
customer2.ContactTitle = oldTitle;
dataManager.Save(customer2);
}
}
}