When the database schema is defined, the data can be retrived and modified by the traditional database querying methods.

The only difference is that queries should be issued against the table views that are created for each table but not the tables themselves.

Only the users that have the permission to change the schema have access to the backend tables.

The following example shows how to populate the database created in the previous example with data using a set of methods of LoBDb.NET itself.

            // Granting all permissions to the current user
            groupTable.GrantAll();
            studentTable.GrantAll();

            // Creating a group
            var groupId = groupTable.InsertRow(groupName.Value("My group"));

            // Creating two students
            var firstStudentId = studentTable.InsertRow(
                studentName.Value("John Smith"),
                studentGender.Value(0),
                studentGroup.Value(groupId));

            var secondStudentId = studentTable.InsertRow(
                studentName.Value("Julia Roberts"),
                studentGender.Value(1),
                studentGroup.Value(groupId));

            // Reading the student list
            Console.WriteLine("Students: count = {0}", studentTable.RowCount);

            // Reading the first student
            var firstStudentName = studentName.EmptyValue();

            studentTable.ReadRow(firstStudentId, firstStudentName);

            Console.WriteLine("The first student: id = {0}, name = {1}",
               firstStudentId, firstStudentName.GetString());

            // Reading the second student
            Console.WriteLine("The second student: id = {0}, name = {1}",
               secondStudentId, studentName.Read(secondStudentId).GetString());

            Console.WriteLine("Updating the students");

            studentTable.UpdateRow(firstStudentId, studentName.Value("Antony Smith"));
            studentName.Update(secondStudentId, "Barbara Roberts");

            // Reading all students
            var currStudentId = studentTable.SystemColumns.Id.EmptyValue();
            var currStudentName = studentName.EmptyValue();

            studentTable.ReadAllRows(() =>
                {
                    Console.WriteLine("Student: id = {0}, name = {1}", currStudentId.GetGuid(), currStudentName.GetString());
                }, currStudentId, currStudentName);

            // Deleting the group
            Console.WriteLine("Deleting the group");

            groupTable.DeleteRow(groupId);

            Console.WriteLine("Groups: count = {0}", groupTable.RowCount);
            Console.WriteLine("Students: count = {0}", studentTable.RowCount);

            Console.WriteLine("Restoring the first student");

            studentTable.RestoreRow(firstStudentId);

            Console.WriteLine("Groups: count = {0}", groupTable.RowCount);
            Console.WriteLine("Students: count = {0}", studentTable.RowCount);
            }
      }
}

Last edited Mar 8, 2012 at 6:19 PM by mapase, version 11

Comments

No comments yet.