MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

Updating Records Using Mapped Names
Previous Home Next

TableMappings and ColumnMappings help you bridge this translation gap of different column/table names in fetching data. The same concepts can be applied to other database operations, such as inserting a new record or updating an existing

 

Example of Adding a Record Using C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data.Common;
using System.Data;
namespace udatingrecord
{
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet("Users");
try
{
OleDbConnection dbConn =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Password=;User ID=Admin;Data Source=db.mdb");
// Create a data adapter to retrieve records from db
OleDbDataAdapter dataadapt =
new OleDbDataAdapter("SELECT ID,fn,ln,cty,st" +
" FROM tabUsers", dbConn);
// Define each column to map
DataColumnMapping DmUserID =
new DataColumnMapping("ID", "UserID");
DataColumnMapping DmFirstName =
new DataColumnMapping("fn", "FirstName");
DataColumnMapping DmLastName =
new DataColumnMapping("ln", "LastName");
DataColumnMapping DmCity =
new DataColumnMapping("cty", "City");
DataColumnMapping DmState =
new DataColumnMapping("st", "State");
// Define the table containing the mapped columns
new DataTableMapping("Table", "User");
dtmUsers.ColumnMappings.Add(DmUserID);
dtmUsers.ColumnMappings.Add(DmFirstName);
dtmUsers.ColumnMappings.Add(DmLastName);
dtmUsers.ColumnMappings.Add(DmCity);
dtmUsers.ColumnMappings.Add(DmState);
// Activate the mapping mechanism
daUsers.TableMappings.Add(dtmUsers);
// Fill the dataset
daUsers.Fill(ds);
DataColumn[] dckey = { ds.Tables["User"].Columns["UserID"] };
ds.Tables["User"].PrimaryKey = dckey;
// Declare a command builder to create SQL instructions
// to create and update records.
OleDbCommandBuilder cb = new OleDbCommandBuilder(daUsers);
// Update an existing record in the DataSet
DataRow r = ds.Tables["User"].Rows.Find(8);
if (r != null)
{
r["FirstName"] = "Ashish";
r["LastName"] = "Gupta";
r["City"] = "URAI";
r["State"] = "UP";
// Update the record in the database
daUsers.Update(ds.GetChanges());
// Align in-memory data with the data source ones
ds.AcceptChanges();
// Print success message
Console.WriteLine("The record has been updated " +
"successfully.");
}
else
{
Console.WriteLine("No record found...");
}
}
catch (System.Exception ex)
{
ds.RejectChanges();
// An error occurred. Show the error message
Console.WriteLine(ex.Message);
}
}
}
}
Previous Home Next