SqlDataAdapter.TableMappings : SqlDataAdapter « System.Data.SqlClient « C# / C Sharp by API






SqlDataAdapter.TableMappings

  
using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

class Program {
    static void Main(string[] args) {
        string cnStr = "uid=sa;pwd=;Initial Catalog=yourDatabase;Data Source=(local)";
        DataSet myDS = new DataSet("Cars");
        SqlDataAdapter dAdapt = new SqlDataAdapter("Select * From Inventory", cnStr);
        DataTableMapping custMap = dAdapt.TableMappings.Add("Inventory", "Current Inventory");
        custMap.ColumnMappings.Add("CarID", "Car ID");
        custMap.ColumnMappings.Add("PetName", "Name of Car");

        try {
            dAdapt.Fill(myDS, "Inventory");
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
        PrintDataSet(myDS);
    }
    static void PrintDataSet(DataSet ds) {
        Console.WriteLine("Tables in '{0}' DataSet.\n", ds.DataSetName);
        foreach (DataTable dt in ds.Tables) {
            Console.WriteLine("{0} Table.\n", dt.TableName);
            for (int curCol = 0; curCol < dt.Columns.Count; curCol++) {
                Console.Write(dt.Columns[curCol].ColumnName.Trim() + "\t");
            }
            for (int curRow = 0; curRow < dt.Rows.Count; curRow++) {
                for (int curCol = 0; curCol < dt.Columns.Count; curCol++) {
                    Console.Write(dt.Rows[curRow][curCol].ToString().Trim() + "\t");
                }
                Console.WriteLine();
            }
        }
    }

}

   
    
  








Related examples in the same category

1.new SqlDataAdapter
2.SqlDataAdapter.Fill
3.SqlDataAdapter.DeleteCommand
4.SqlDataAdapter.InsertCommand
5.SqlDataAdapter.RowUpdated
6.SqlDataAdapter.RowUpdating
7.SqlDataAdapter.SelectCommand
8.SqlDataAdapter.SelectCommand.Parameters.Add
9.SqlDataAdapter.TableMappings.Add
10.SqlDataAdapter.Update
11.SqlDataAdapter.UpdateCommand