Read schema information using the FillSchema() method of a DataAdapter object : Table Schema « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Database ADO.net » Table SchemaScreenshots 
Read schema information using the FillSchema() method of a DataAdapter object


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

class FillSchema
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText =
      "SELECT ProductID, ProductName " +
      "FROM Products;" +
      "SELECT OrderID " +
      "FROM Orders;" +
      "SELECT OrderID, ProductID, UnitPrice " +
      "FROM [Order Details];";
    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
    mySqlDataAdapter.SelectCommand = mySqlCommand;
    DataSet myDataSet = new DataSet();
    mySqlConnection.Open();
    mySqlDataAdapter.FillSchema(myDataSet, SchemaType.Mapped);
    mySqlConnection.Close();
    myDataSet.Tables["Table"].TableName = "Products";
    myDataSet.Tables["Table1"].TableName = "Orders";
    myDataSet.Tables["Table2"].TableName = "Order Details";

    foreach (DataTable myDataTable in myDataSet.Tables)
    {
      Console.WriteLine("\n\nReading from the " +
        myDataTable + "DataTable:\n");

      foreach (DataColumn myPrimaryKey in myDataTable.PrimaryKey)
      {
        Console.WriteLine("myPrimaryKey = " + myPrimaryKey);
      }

      foreach (Constraint myConstraint in myDataTable.Constraints)
      {
        Console.WriteLine("myConstraint.IsPrimaryKey = " ((UniqueConstraintmyConstraint).IsPrimaryKey);
        foreach (DataColumn myDataColumn in ((UniqueConstraintmyConstraint).Columns)
        {
          Console.WriteLine("myDataColumn.ColumnName = " + myDataColumn.ColumnName);
        }
      }

      foreach (DataColumn myDataColumn in myDataTable.Columns)
      {
        Console.WriteLine("\nmyDataColumn.ColumnName = " + myDataColumn.ColumnName);
        Console.WriteLine("myDataColumn.DataType = " + myDataColumn.DataType);

        Console.WriteLine("myDataColumn.AllowDBNull = " + myDataColumn.AllowDBNull);
        Console.WriteLine("myDataColumn.AutoIncrement = " + myDataColumn.AutoIncrement);
        Console.WriteLine("myDataColumn.AutoIncrementSeed = " + myDataColumn.AutoIncrementSeed);
        Console.WriteLine("myDataColumn.AutoIncrementStep = " + myDataColumn.AutoIncrementStep);
        Console.WriteLine("myDataColumn.MaxLength = " + myDataColumn.MaxLength);
        Console.WriteLine("myDataColumn.ReadOnly = " + myDataColumn.ReadOnly);
        Console.WriteLine("myDataColumn.Unique = " + myDataColumn.Unique);
      }
    }
  }
}
           
       
Related examples in the same category
1.Get all table namesGet all table names
2.Get specified column data type and column name from OleDbSchemaTableGet specified column data type and column name from OleDbSchemaTable
3.Get Column data type and name from DataColumnGet Column data type and name from DataColumn
4.How to read a table schema
5.Get table Schema
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.