📄 key-constraints.aspx
字号:
<%@Page Language="VB"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Adding Primary Keys and Foreign Keys to a Table</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Adding Primary Keys and Foreign Keys to a Table</span><hr />
<!--------------------------------------------------------------------------->
<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>
<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>Books SELECT command: <b><span id="outSelectBooks" runat="server"></span></b></div>
<div>Authors SELECT command: <b><span id="outSelectAuthors" runat="server"></span></b></div>
<div id="outError" runat="server"> </div>
<b>DataSet.Tables Collection</b>
<asp:datagrid id="dgrTables" runat="server" /><br />
<b>DataSet.Tables("Books").Constraints Collection</b>
<asp:datagrid id="dgrBookCons" runat="server" /><br />
<b>DataSet.Tables("Authors").Constraints Collection</b>
<asp:datagrid id="dgrAuthorCons" runat="server" /><br />
<b>Contents of DataSet.Tables("Books")</b>
<asp:datagrid id="dgrBooksData" runat="server" /><br />
<b>Contents of DataSet.Tables("Authors")</b>
<asp:datagrid id="dgrAuthorsData" runat="server" /><br />
<script language="vb" runat="server">
Sub Page_Load()
'get connection string from ..\global\connect-strings.ascx user control
Dim strConnect = ctlConnectStrings.OLEDBConnectionString
outConnect.innerText = strConnect 'and display it
'specify the SELECT statement to extract the BookList data
Dim strSelectBooks As String
strSelectBooks = "SELECT * FROM BookList WHERE ISBN LIKE '18610033%'"
outSelectBooks.innerText = strSelectBooks 'and display it
'specify the SELECT statement to extract the BookAuthor data
Dim strSelectAuthors As String
strSelectAuthors = "SELECT * FROM BookAuthors WHERE ISBN LIKE '18610033%'"
outSelectAuthors.innerText = strSelectAuthors 'and display it
'declare a variable to hold a DataSet object
Dim objDataSet As New DataSet()
Try
'create a new Connection object using the connection string
Dim objConnect As New OleDbConnection(strConnect)
'create a new Command object
Dim objCommand As New OleDbCommand()
'set the properties
objCommand.Connection = objConnect
objCommand.CommandType = CommandType.Text
objCommand.CommandText = strSelectBooks
'create a new DataAdapter object
Dim objDataAdapter As New OleDbDataAdapter
'and assign the Command object to it
objDataAdapter.SelectCommand = objCommand
'get the data from the "BookList" table in the database and
'put it into a table named "Books" in the DataSet object
objDataAdapter.Fill(objDataSet, "Books")
'change the SELECT statement in the Command object
objCommand.CommandText = strSelectAuthors
'then get data from "BookAuthors" table into the DataSet
objDataAdapter.Fill(objDataSet, "Authors")
Catch objError As Exception
'display error details
outError.innerHTML = "<b>* Error while accesing data</b>.<br />" _
& objError.Message & "<br />" & objError.Source
Exit Sub ' and stop execution
End Try
'now that the DataSet is filled, we can modify the tables it contains
'declare variables to refer to the DataTable and a DataColumn objects
Dim objParentTable As DataTable = objDataSet.Tables("Books")
Dim objChildTable As DataTable = objDataSet.Tables("Authors")
Dim objParentColumn As DataColumn = objParentTable.Columns("ISBN")
Dim objChildColumn As DataColumn = objChildTable.Columns("ISBN")
'create a new UniqueConstraint object and add to Constraints collection
Dim objUnique As New UniqueConstraint("Unique_ISBN", objParentColumn)
objParentTable.Constraints.Add(objUnique)
'prevent the column from accepting Null values
objParentColumn.AllowDBNull = False
'create an array of columns containing this column only
Dim objColumnArray(0) As DataColumn
objColumnArray(0) = objParentColumn
'and set this array as the columns for the Primary Key of the table
objParentTable.PrimaryKey = objColumnArray
'now we can process the child table named "Authors"
'create an array of columns containing the ISBN and Lastname columns
ReDim objColumnArray(1)
objColumnArray(0) = objChildColumn 'the ISBN column
objColumnArray(1) = objChildTable.Columns("Lastname")
'prevent either of these columns containing Null
objColumnArray(0).AllowDBNull = False
objColumnArray(1).AllowDBNull = False
'set this column array as the primary key
objChildTable.PrimaryKey = objColumnArray
'create a new ForeignKeyConstraint object
Dim objFKey As New ForeignKeyConstraint("FK_BookAuthors", _
objParentColumn, objChildColumn)
'set the "update" properties
objFKey.DeleteRule = Rule.Cascade
objFKey.UpdateRule = Rule.Cascade
'and add it to the Constraints collection
objChildTable.Constraints.Add(objFKey)
'--------------------------------------------------------------
'now we're ready to display the contents of the DataSet object
'bind the collection of Tables to the first DataGrid on the page
dgrTables.DataSource = objDataSet.Tables
dgrTables.DataBind()
'bind the collection of Constraints to the second DataGrid on the page
dgrBookCons.DataSource = objDataSet.Tables("Books").Constraints
dgrBookCons.DataBind()
'bind the collection of Constraints to the third DataGrid on the page
dgrAuthorCons.DataSource = objDataSet.Tables("Authors").Constraints
dgrAuthorCons.DataBind()
'create a DataView object to use with the tables in the DataSet
Dim objDataView As New DataView
'get the default view of the Books table into the DataView object
objDataView = objDataSet.Tables("Books").DefaultView
'and bind it to the next DataGrid on the page
dgrBooksData.DataSource = objDataView
dgrBooksData.DataBind()
'then do the same for the Authors table
objDataView = objDataSet.Tables("Authors").DefaultView
dgrAuthorsData.DataSource = objDataView
dgrAuthorsData.DataBind()
End Sub
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -