Code for VB.NET教程源码 很好的源码

源代码在线查看: get-dataset-control.ascx

软件大小: 1359 K
上传用户: a195767236
关键词: Code for NET 源码
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				
				
				
				
				
				
				Connection string: 
				Books SELECT command: 
				Authors SELECT command: 
				Prices SELECT command: 
				 
				
				
				
				Public Function BooksDataSet(strConnect As String, _
				                             strWhere As String) _
				                             As DataSet
				   outConnect.innerText = strConnect  'display connection string
				
				   'specify the SELECT statement to extract the BookList data
				   Dim strSelectBooks As String
				   strSelectBooks = "SELECT * FROM BookList WHERE " & strWhere
				   outSelectBooks.innerText = strSelectBooks   'and display it
				
				   'specify the SELECT statement to extract the BookAuthor data
				   Dim strSelectAuthors As String
				   strSelectAuthors = "SELECT * FROM BookAuthors WHERE " & strWhere
				   outSelectAuthors.innerText = strSelectAuthors   'and display it
				
				   'specify the SELECT statement to extract the BookPrices data
				   Dim strSelectPrices As String
				   strSelectPrices = "SELECT * FROM BookPrices WHERE " & strWhere
				   outSelectPrices.innerText = strSelectAuthors   'and display it
				
				   'declare a variable to hold a DataSet object
				   'note that we have to create it outside the Try..Catch block
				   'as this is a separate block and so is a different scope
				   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 ADOCommand object
				      objCommand.CommandText = strSelectAuthors
				      'then get data from "BookAuthors" table into the DataSet
				      objDataAdapter.Fill(objDataSet, "Authors")
				
				      'and do the same again to get the "BookPrices" data
				      objCommand.CommandText = strSelectPrices
				      objDataAdapter.Fill(objDataSet, "Prices")
				
				      'declare a variable to hold a DataRelation object
				      Dim objRelation As DataRelation
				
				      'create a Relation object to link Books and Authors
				      objRelation = New DataRelation("BookAuthors", _
				                        objDataSet.Tables("Books").Columns("ISBN"), _
				                        objDataSet.Tables("Authors").Columns("ISBN"))
				      'and add it to the DataSet object's Relations collection
				      objDataSet.Relations.Add(objRelation)
				
				      'now do the same to link Books and Prices
				      objRelation = New DataRelation("BookPrices", _
				                        objDataSet.Tables("Books").Columns("ISBN"), _
				                        objDataSet.Tables("Prices").Columns("ISBN"))
				      objDataSet.Relations.Add(objRelation)
				
				   Catch objError As Exception
				
				      'display error details
				      outError.innerHTML = "* Error while accesing data." _
				          & objError.Message & "" & objError.Source
				      'return Nothing on error
				      BooksDataSet = Nothing
				      Exit Function  ' and stop execution
				
				   End Try
				
				   'display a success message
				   outError.innerHTML = "DataSet created and loaded."
				
				   'return our new disconnected DataSet object
				   Return objDataSet
				
				End Function
				
				
							

相关资源