Adding Restrictions to DataTable and DataColumn Objects phần 3
Số trang: 7
Loại file: pdf
Dung lượng: 30.29 KB
Lượt xem: 12
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
myPrimaryKey = OrderID myPrimaryKey = ProductID myDataColumn.ColumnName = OrderID myDataColumn.DataType = System.Int32 myDataColumn.AllowDBNull = False myDataColumn.AutoIncrement = False
Nội dung trích xuất từ tài liệu:
Adding Restrictions to DataTable and DataColumn Objects phần 3myPrimaryKey = OrderIDmyDataColumn.ColumnName = OrderIDmyDataColumn.DataType = System.Int32myDataColumn.AllowDBNull = FalsemyDataColumn.AutoIncrement = FalsemyDataColumn.AutoIncrementSeed = 0myDataColumn.AutoIncrementStep = 1myDataColumn.MaxLength = -1myDataColumn.ReadOnly = FalsemyDataColumn.Unique = TrueReading from the Order Details DataTable:myPrimaryKey = OrderIDmyPrimaryKey = ProductIDmyDataColumn.ColumnName = OrderIDmyDataColumn.DataType = System.Int32myDataColumn.AllowDBNull = FalsemyDataColumn.AutoIncrement = FalsemyDataColumn.AutoIncrementSeed = 0myDataColumn.AutoIncrementStep = 1myDataColumn.MaxLength = -1myDataColumn.ReadOnly = FalsemyDataColumn.Unique = FalsemyDataColumn.ColumnName = ProductIDmyDataColumn.DataType = System.Int32myDataColumn.AllowDBNull = FalsemyDataColumn.AutoIncrement = FalsemyDataColumn.AutoIncrementSeed = 0myDataColumn.AutoIncrementStep = 1myDataColumn.MaxLength = -1myDataColumn.ReadOnly = FalsemyDataColumn.Unique = FalsemyDataColumn.ColumnName = UnitPricemyDataColumn.DataType = System.DecimalmyDataColumn.AllowDBNull = TruemyDataColumn.AutoIncrement = FalsemyDataColumn.AutoIncrementSeed = 0myDataColumn.AutoIncrementStep = 1myDataColumn.MaxLength = -1myDataColumn.ReadOnly = FalsemyDataColumn.Unique = FalseAdding Restrictions by Calling the DataAdapter Objects FillSchema() MethodInstead 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 dont 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 aprimary 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 thismethod 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.SchemaTypeenumeration. Table 11.7 shows the constants defined in the SchemaType enumeration. Table 11.7: SchemaType ENUMERATION MEMBERSCONSTANT DESCRIPTIONMapped 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.Lets take a look at an example that contains a call to the FillSchema() method. Notice thecall 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, andOrder Details tables to myDataSet, setting the PrimaryKey property of each DataTableand 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;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 (DataTabl ...
Nội dung trích xuất từ tài liệu:
Adding Restrictions to DataTable and DataColumn Objects phần 3myPrimaryKey = OrderIDmyDataColumn.ColumnName = OrderIDmyDataColumn.DataType = System.Int32myDataColumn.AllowDBNull = FalsemyDataColumn.AutoIncrement = FalsemyDataColumn.AutoIncrementSeed = 0myDataColumn.AutoIncrementStep = 1myDataColumn.MaxLength = -1myDataColumn.ReadOnly = FalsemyDataColumn.Unique = TrueReading from the Order Details DataTable:myPrimaryKey = OrderIDmyPrimaryKey = ProductIDmyDataColumn.ColumnName = OrderIDmyDataColumn.DataType = System.Int32myDataColumn.AllowDBNull = FalsemyDataColumn.AutoIncrement = FalsemyDataColumn.AutoIncrementSeed = 0myDataColumn.AutoIncrementStep = 1myDataColumn.MaxLength = -1myDataColumn.ReadOnly = FalsemyDataColumn.Unique = FalsemyDataColumn.ColumnName = ProductIDmyDataColumn.DataType = System.Int32myDataColumn.AllowDBNull = FalsemyDataColumn.AutoIncrement = FalsemyDataColumn.AutoIncrementSeed = 0myDataColumn.AutoIncrementStep = 1myDataColumn.MaxLength = -1myDataColumn.ReadOnly = FalsemyDataColumn.Unique = FalsemyDataColumn.ColumnName = UnitPricemyDataColumn.DataType = System.DecimalmyDataColumn.AllowDBNull = TruemyDataColumn.AutoIncrement = FalsemyDataColumn.AutoIncrementSeed = 0myDataColumn.AutoIncrementStep = 1myDataColumn.MaxLength = -1myDataColumn.ReadOnly = FalsemyDataColumn.Unique = FalseAdding Restrictions by Calling the DataAdapter Objects FillSchema() MethodInstead 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 dont 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 aprimary 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 thismethod 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.SchemaTypeenumeration. Table 11.7 shows the constants defined in the SchemaType enumeration. Table 11.7: SchemaType ENUMERATION MEMBERSCONSTANT DESCRIPTIONMapped 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.Lets take a look at an example that contains a call to the FillSchema() method. Notice thecall 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, andOrder Details tables to myDataSet, setting the PrimaryKey property of each DataTableand 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;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 (DataTabl ...
Tìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Adding Restrictions to DataTable and DataColumn Objects phần 3Tài liệu liên quan:
-
52 trang 432 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 318 0 0 -
74 trang 302 0 0
-
96 trang 297 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 284 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 277 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 268 0 0