intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Adding Restrictions to DataTable and DataColumn Objects phần 3

Chia sẻ: Tuan Nghia | Ngày: | Loại File: PDF | Số trang:7

96
lượt xem
8
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

myPrimaryKey = OrderID myPrimaryKey = ProductID myDataColumn.ColumnName = OrderID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False

Chủ đề:
Lưu

Nội dung Text: Adding Restrictions to DataTable and DataColumn Objects phần 3

  1. myPrimaryKey = OrderID myDataColumn.ColumnName = OrderID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = False myDataColumn.Unique = True Reading from the Order Details DataTable: myPrimaryKey = OrderID myPrimaryKey = ProductID myDataColumn.ColumnName = OrderID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = False myDataColumn.Unique = False myDataColumn.ColumnName = ProductID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = False myDataColumn.Unique = False myDataColumn.ColumnName = UnitPrice myDataColumn.DataType = System.Decimal myDataColumn.AllowDBNull = True myDataColumn.AutoIncrement = False myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1
  2. myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = False myDataColumn.Unique = False Adding Restrictions by Calling the DataAdapter Object's FillSchema() Method Instead of adding restrictions yourself, you can add them by calling the FillSchema() method of your DataAdapter. The FillSchema() method does the following: • Copies the schema information from the database. • Creates DataTable objects in your DataSet if they don't already exist. • Adds the constraints to the DataTable objects. • Sets the properties of the DataColumn objects appropriately. The properties of the DataColumn objects set by FillSchema() include the following: • The DataColumn name-which is stored in the ColumnName property. • The DataColumn .NET data type-which is stored in the DataType property. • The maximum length of a variable length data type-which is stored in the MaxLength property. • Whether the DataColumn can accept a null value-which is stored in the AllowDBNull property. • Whether the DataColumn value must be unique-which is stored in the Unique property. • Any auto-increment information-which is stored in the AutoIncrement, AutoIncrementSeed, and AutoIncrementStep properties. The FillSchema() method will also determine whether the DataColumn is part of a primary key and store that information in the PrimaryKey property of the DataTable. Warning FillSchema() does not automatically add ForeignKeyConstraint objects to the DataTable objects. Neither does it retrieve the actual rows from the database; it retrieves only the schema information. The FillSchema() method is overloaded, with the most commonly used version of this method being the following: DataTable[] FillSchema(DataSet myDataSet, SchemaType mySchemaType) where mySchemaType specifies how you want to handle any existing schema mappings. You set mySchemaType to one of the constants defined in the System.Data.SchemaType enumeration. Table 11.7 shows the constants defined in the SchemaType enumeration.
  3. Table 11.7: SchemaType ENUMERATION MEMBERS CONSTANT DESCRIPTION Mapped Apply any existing table mappings to the incoming schema and configure the DataSet with the transformed schema. This is the constant you should typically use. Source Ignore any table mappings and configure the DataSet without any transformations. Let's take a look at an example that contains a call to the FillSchema() method. Notice the call uses the SchemaType.Mapped constant to apply any existing table mappings: 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"; The call to FillSchema() copies the schema information from the Products, Orders, and Order Details tables to myDataSet, setting the PrimaryKey property of each DataTable and the properties of the DataColumn objects appropriately. Listing 11.2 shows the use of the FillSchema() method. Listing 11.2: FILLSCHEMA.CS /* FillSchema.cs illustrates how to read schema information using the FillSchema() method of a DataAdapter object */ using System;
  4. using System.Data; using System.Data.SqlClient; class FillSchema { public static void Main() { SqlConnection mySqlConnection = new SqlConnection( "server=localhost;database=Northwind;uid=sa;pwd=sa" ); 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"; // display the details of the DataColumn objects for // the DataTable objects foreach (DataTable myDataTable in myDataSet.Tables) { Console.WriteLine("\n\nReading from the " + myDataTable + "DataTable:\n"); // display the primary key foreach (DataColumn myPrimaryKey in myDataTable.PrimaryKey) { Console.WriteLine("myPrimaryKey = " + myPrimaryKey); }
  5. // display the constraints foreach (Constraint myConstraint in myDataTable.Constraints) { Console.WriteLine("myConstraint.IsPrimaryKey = " + ((UniqueConstraint) myConstraint).IsPrimaryKey); foreach (DataColumn myDataColumn in ((UniqueConstraint) myConstraint).Columns) { Console.WriteLine("myDataColumn.ColumnName = " + myDataColumn.ColumnName); } } // display some of the details for each column 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); } } } } The output from this program is as follows:
  6. Reading from the Products DataTable: myPrimaryKey = ProductID myConstraint.IsPrimaryKey = True myDataColumn.ColumnName = ProductID myDataColumn.ColumnName = ProductID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = True myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = True myDataColumn.Unique = True myDataColumn.ColumnName = ProductName myDataColumn.DataType = System.String myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = 40 myDataColumn.ReadOnly = False myDataColumn.Unique = False Reading from the Orders DataTable: myPrimaryKey = OrderID myConstraint.IsPrimaryKey = True myDataColumn.ColumnName = OrderID myDataColumn.ColumnName = OrderID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = True myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = True myDataColumn.Unique = True
  7. Reading from the Order Details DataTable: myPrimaryKey = OrderID myPrimaryKey = ProductID myConstraint.IsPrimaryKey = True myDataColumn.ColumnName = OrderID myDataColumn.ColumnName = ProductID myDataColumn.ColumnName = OrderID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = False myDataColumn.Unique = False myDataColumn.ColumnName = ProductID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = False myDataColumn.Unique = False myDataColumn.ColumnName = UnitPrice myDataColumn.DataType = System.Decimal myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False myDataColumn.AutoIncrementSeed = 0 myDataColumn.AutoIncrementStep = 1 myDataColumn.MaxLength = -1 myDataColumn.ReadOnly = False myDataColumn.Unique = False
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2