quarta-feira, 17 de outubro de 2012

Table Mapping in DOT.NET



static void Main(string[] args)
{

// Configure Sql Provider - to connect to server or database
  string sqlConnectString = @"Data Source=(local)\sqlexpress;" +
  "Integrated security=SSPI; Initial Catalog=DataBase;";

// Creating a table Cliente in a DataBase (Sql, Access...)

 // Query to get desired data
  String sqlSelect = "SELECT TOP 5 IDCliente,Apelido,PrimNome " +
  "From Cliente";

 //Get data from sql to adapter
  SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);

//Mappeding the table
  DataTableMapping dtm =da.TableMappings.Add("Table", "mappedCliente");

// Mapping the column
  dtm.ColumnMappings.Add("IDCliente", "mappedIDCliente");
  dtm.ColumnMappings.Add("Apelido", "mappedApelido"); //...

   dtm.ColumnMappings.Add("PrimNome", "mappedPrimNome"); //...

 // (DataSet = DataBase Virtual)
  DataSet ds = new DataSet();

 // dataset get data from adapter 
   da.Fill(ds);
  Console.WriteLine("DataTable name = {0}",ds.Tables[0].TableName);

// show the mapped columns
 foreach (DataColumn col in ds.Tables["mappedCliente"].Columns)

  {
   Console.WriteLine("\tDataColumn {0} name = {1}",col.Ordinal, col.ColumnName);
  }

 Console.WriteLine();

// A break to view the mapped columns - Enter to continue
 Console.ReadKey(); 

// show data in rows
  foreach (DataRow row in ds.Tables[ "mappedCliente"].Rows)

  {
  Console.WriteLine("IDCliente = {0}, Apelido = {1}, PrimNome = {2}",
      row["mappedIDCliente"], row["mappedApelido"],row["mappedPrimNome"]);

  }

  Console.WriteLine("\nEnd DataTableMapping - Press any Key to           continue");
  Console.ReadKey(); 

}


Sem comentários:

Enviar um comentário