Creating a UniqueConstraint Object
Số trang: 3
Loại file: pdf
Dung lượng: 23.36 KB
Lượt xem: 10
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:
Creating a UniqueConstraint Object In this section, youll learn how to create a UniqueConstraint object. The UniqueConstraint constructor is overloaded as follows
Nội dung trích xuất từ tài liệu:
Creating a UniqueConstraint ObjectCreating a UniqueConstraint ObjectIn this section, youll learn how to create a UniqueConstraint object. TheUniqueConstraint constructor is overloaded as follows:UniqueConstraint(DataColumn myDataColumn)UniqueConstraint(DataColumn[] myDataColumns)UniqueConstraint(DataColumn myDataColumn, bool isPrimaryKey)UniqueConstraint(DataColumn[] myDataColumns, bool isPrimaryKey)UniqueConstraint(string constraintName, DataColumn myDataColumn)UniqueConstraint(string constraintName, DataColumn[] myDataColumns)UniqueConstraint(string constraintName, DataColumn myDataColumn, boolisPrimaryKey)UniqueConstraint(string constraintName, DataColumn[] myDataColumns, bool isPrimaryKey)UniqueConstraint(string constraintName, string[] columnNames, bool isPrimaryKey)where • myDataColumn and myDataColumns are the DataColumn objects that you want to ensure are unique. • constraintName is the name you want to assign to the ConstraintName property of your UniqueConstraint. • isPrimaryKey indicates whether your UniqueConstraint is for a primary key.Before creating and adding a UniqueConstraint to a DataTable, you first need aDataTable. The following example creates and populates two DataTable objects namedcustomersDT and ordersDT (the ordersDT object will be used later in the sectionCreating a ForeignKeyConstraint Object):SqlCommand mySqlCommand = mySqlConnection.CreateCommand();mySqlCommand.CommandText = SELECT CustomerID, CompanyName + FROM Customers + WHERE CustomerID = ALFKI + SELECT OrderID, CustomerID + FROM Orders + WHERE CustomerID = ALFKI;;SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();mySqlDataAdapter.SelectCommand = mySqlCommand;DataSet myDataSet = new DataSet();mySqlConnection.Open();mySqlDataAdapter.Fill(myDataSet);mySqlConnection.Close();myDataSet.Tables[Table].TableName = Customers;myDataSet.Tables[Table1].TableName = Orders;DataTable customersDT = myDataSet.Tables[Customers];DataTable ordersDT = myDataSet.Tables[Orders];The following example creates a UniqueConstraint object on the CustomerIDDataColumn of the customersDT DataTable, and then adds this UniqueConstraint tocustomersDT; notice that the third parameter to the UniqueConstraint constructor is set totrue, indicating that the constraint is for a primary key:UniqueConstraint myUC = new UniqueConstraint( UniqueConstraintCustomerID, customersDT.Columns[CustomerID], true );customersDT.Constraints.Add(myUC);Note To successfully add a UniqueConstraint to the DataColumn of a DataTable, the DataColumn value in each DataRow object in the DataTable must be unique.The final example retrieves the constraint just added to customersDT and displays itsproperties:myUC = (UniqueConstraint) customersDT.Constraints[UniqueConstraintCustomerID];Console.WriteLine(Columns:);foreach (DataColumn myDataColumn in myUC.Columns){ Console.WriteLine( + myDataColumn);}Console.WriteLine(myUC.ConstraintName = + myUC.ConstraintName);Console.WriteLine(myUC.IsPrimaryKey = + myUC.IsPrimaryKey);Console.WriteLine(myUC.Table = + myUC.Table);This example displays the following output:Columns: CustomerIDmyUC.ConstraintName = UniqueConstraintCustomerIDmyUC.IsPrimaryKey = TruemyUC.Table = CustomersThe IsPrimaryKey property is true because the constraint is for a primary key; this wasspecified in the constructor when the constraint was created earlier.
Nội dung trích xuất từ tài liệu:
Creating a UniqueConstraint ObjectCreating a UniqueConstraint ObjectIn this section, youll learn how to create a UniqueConstraint object. TheUniqueConstraint constructor is overloaded as follows:UniqueConstraint(DataColumn myDataColumn)UniqueConstraint(DataColumn[] myDataColumns)UniqueConstraint(DataColumn myDataColumn, bool isPrimaryKey)UniqueConstraint(DataColumn[] myDataColumns, bool isPrimaryKey)UniqueConstraint(string constraintName, DataColumn myDataColumn)UniqueConstraint(string constraintName, DataColumn[] myDataColumns)UniqueConstraint(string constraintName, DataColumn myDataColumn, boolisPrimaryKey)UniqueConstraint(string constraintName, DataColumn[] myDataColumns, bool isPrimaryKey)UniqueConstraint(string constraintName, string[] columnNames, bool isPrimaryKey)where • myDataColumn and myDataColumns are the DataColumn objects that you want to ensure are unique. • constraintName is the name you want to assign to the ConstraintName property of your UniqueConstraint. • isPrimaryKey indicates whether your UniqueConstraint is for a primary key.Before creating and adding a UniqueConstraint to a DataTable, you first need aDataTable. The following example creates and populates two DataTable objects namedcustomersDT and ordersDT (the ordersDT object will be used later in the sectionCreating a ForeignKeyConstraint Object):SqlCommand mySqlCommand = mySqlConnection.CreateCommand();mySqlCommand.CommandText = SELECT CustomerID, CompanyName + FROM Customers + WHERE CustomerID = ALFKI + SELECT OrderID, CustomerID + FROM Orders + WHERE CustomerID = ALFKI;;SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();mySqlDataAdapter.SelectCommand = mySqlCommand;DataSet myDataSet = new DataSet();mySqlConnection.Open();mySqlDataAdapter.Fill(myDataSet);mySqlConnection.Close();myDataSet.Tables[Table].TableName = Customers;myDataSet.Tables[Table1].TableName = Orders;DataTable customersDT = myDataSet.Tables[Customers];DataTable ordersDT = myDataSet.Tables[Orders];The following example creates a UniqueConstraint object on the CustomerIDDataColumn of the customersDT DataTable, and then adds this UniqueConstraint tocustomersDT; notice that the third parameter to the UniqueConstraint constructor is set totrue, indicating that the constraint is for a primary key:UniqueConstraint myUC = new UniqueConstraint( UniqueConstraintCustomerID, customersDT.Columns[CustomerID], true );customersDT.Constraints.Add(myUC);Note To successfully add a UniqueConstraint to the DataColumn of a DataTable, the DataColumn value in each DataRow object in the DataTable must be unique.The final example retrieves the constraint just added to customersDT and displays itsproperties:myUC = (UniqueConstraint) customersDT.Constraints[UniqueConstraintCustomerID];Console.WriteLine(Columns:);foreach (DataColumn myDataColumn in myUC.Columns){ Console.WriteLine( + myDataColumn);}Console.WriteLine(myUC.ConstraintName = + myUC.ConstraintName);Console.WriteLine(myUC.IsPrimaryKey = + myUC.IsPrimaryKey);Console.WriteLine(myUC.Table = + myUC.Table);This example displays the following output:Columns: CustomerIDmyUC.ConstraintName = UniqueConstraintCustomerIDmyUC.IsPrimaryKey = TruemyUC.Table = CustomersThe IsPrimaryKey property is true because the constraint is for a primary key; this wasspecified in the constructor when the constraint was created earlier.
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 Creating a UniqueConstraint ObjectGợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 311 0 0 -
74 trang 294 0 0
-
96 trang 290 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 288 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 278 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 272 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 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 262 0 0