Mapping Table and Column Names Between a Data Source and DataSet : DataSet « ADO.Net « C# / CSharp Tutorial






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

    class Program
    {
        static void Main(string[] args)
        {
            string sqlConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=AdventureWorks;";

            string sqlSelect = "SELECT TOP 5 Title, FirstName, LastName FROM Person.Contact";

            SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);

            DataTableMapping dtm = da.TableMappings.Add("Table", "myContact");

            dtm.ColumnMappings.Add("Title", "myTitle");
            dtm.ColumnMappings.Add("FirstName", "myFirstName");
            dtm.ColumnMappings.Add("LastName", "myLastName");

            DataSet ds = new DataSet();
            da.Fill(ds);

            Console.WriteLine("DataTable name = {0}",ds.Tables[0].TableName);

            foreach(DataColumn col in ds.Tables["myContact"].Columns)
            {
                Console.WriteLine(col.Ordinal);
                Console.WriteLine(col.ColumnName);
            }
            foreach(DataRow row in ds.Tables["myContact"].Rows)
            {
                Console.WriteLine(
                    "Title = {0}, FirstName = {1}, LastName = {2}",
                    row["myTitle"], row["myFirstName"],
                    row["myLastName"]);
            }
        }
    }








32.35.DataSet
32.35.1.Simple Query Dataset
32.35.2.Fill a DataSet
32.35.3.Find Rows In Data
32.35.4.Load XML to DataSet
32.35.5.DataSet Merge event
32.35.6.Merge two DataSet
32.35.7.Creating a Strongly Typed DataSet
32.35.8.DataSet Read with SqlDataAdapter
32.35.9.DataSet.DataTable count
32.35.10.DataSet.DataTable name
32.35.11.Mapping Table and Column Names Between a Data Source and DataSet
32.35.12.Retrieving Schema and Constraints for a DataSet
32.35.13.Read schema and reload data with DataSet
32.35.14.Navigating Between Parent and Child Tables in an Untyped DataSet
32.35.15.Multitabled DataSet App