Visual Studio 2005 Cookbook – Database, Reading an XML File into In-Memory Data Tables
Recipe 13.10. Reading an XML File into In-Memory Data Tables
Problem
You previously exported a DataSet to an XML file, and now you need to get it back.
Solution
Use the DataSet object’s ReadXML() method to restore data from a previously generated XML export.
Discussion
Recipe 13.9 exports some XML and a related schema for a table with state-specific information. To read it back into a DataSet object, use the following code:
Dim stateSet As New Data.DataSet
stateSet.ReadXmlSchema("c:StateSchema.xml")
stateSet.ReadXml("c:StateInfo.xml")
You do not need to import a previously saved schema into a DataSet before retrieving the related data, but it helps. Without the schema, Read More >>
Visual Studio 2005 Cookbook – Database, Writing In-Memory Data Tables to an XML File
Recipe 13.9. Writing In-Memory Data Tables to an XML File
Problem
You have some data in a DataSet object, and you would like to export it to an XML file for later reimportation.
Solution
Use the DataSet’s WriteXML() method to send the DataSet content to the file in a common XML format.
Discussion
Recipe 13.8 builds a DataTable object with two state-specific records. The following code adds that table to a DataSet object and writes its records to an XML file:
Dim fullDataSet As New Data.DataSet
fullDataSet.Tables.Add(stateTable)
fullDataSet.WriteXml("C:StateInfo.xml")
These statements generate the following XML content:
<?xml version="1.0" standalone="yes"?> <NewDataSet> <UnitedStates> <ShortName>WA</ShortName> <FullName>Washington</FullName> <Admission>1889-11-11T00:00:00-08:00</Admission> <Population>5894121</Population> </UnitedStates> <UnitedStates> <ShortName>MT</ShortName> <FullName>Montana</FullName> <Admission>1889-11-08T00:00:00-08:00</Admission> <Population>902195</Population> </UnitedStates> </NewDataSet>
You can also output the XML directly Read More >>
Visual Studio 2005 Cookbook – Database, Creating In-Memory Data Tables Manually
Recipe 13.8. Creating In-Memory Data Tables Manually
Problem
You want to manage some data in a database-table-like fashion, but the source data is not coming from a database, or from anything that looks like a table.
Solution
Build a DataTable manually, and fill in all the table details and data yourself.
Discussion
The following code builds a simple table of state information and adds two records:
Dim stateTable As DataTable = New DataTable("UnitedStates")
' ----- Use the abbreviation as the primary key.
Dim priKeyCol(0) As Data.DataColumn
priKeyCol(0) = stateTable.Columns.Add("ShortName", GetType(String))
stateTable.PrimaryKey = priKeyCol
' ----- Add other data columns.
stateTable.Columns.Add("FullName", GetType(String))
stateTable.Columns.Add("Admission", GetType(Date))
stateTable.Columns.Add("Population", GetType(Long))
' ----- Add a record.
Dim stateInfo As Data.DataRow = stateTable.NewRow()
stateInfo!ShortName = "WA"
stateInfo!FullName = "Washington"
stateInfo!Admission = #11/11/1889#
stateInfo!Population = 5894121
stateTable.Rows.Add(stateInfo)
' ----- Add another record.
stateInfo = stateTable.NewRow()
stateInfo!ShortName = "MT"
stateInfo!FullName = "Montana"
stateInfo!Admission = #11/8/1889#
stateInfo!Population = 902195
stateTable.Rows.Add(stateInfo)
' ----- Prove that the data arrived.
MsgBox(stateTable.Rows.Count) ' Displays "2"
MsgBox(stateTable.Rows(0)!FullName) ' Displays "Washington"
ADO.NET defines the basic structures for tables, Read More >>
Visual Studio 2005 Cookbook – Database, Storing the Results of a Query in Memory
Recipe 13.7. Storing the Results of a Query in Memory
Problem
While a data reader is fast and convenient, you would like to keep the retrieved data around for a while, even when you are disconnected from the database or other data source.
Solution
Use the data reader to bring the results into a data set. The DataSet object represents one or more in-memory database tables, each with its records stored in a separate DataTable object.
Discussion
The following code loads all records from the Table1 table into a DataSet object, creating a DataTable object named Table1 within that data set:
' ----- Connect to the database. Dim connectionString As String = _ “Data Source=MySystemSQLEXPRESS;” & _ “Initial Catalog=MyDatabase;Integrated Security=true” Dim theDatabase As New SqlClient.SqlConnection(connectionString) theDatabase.Open() ‘ —– Prepare the SQL statement for use by the data set. Dim sqlStatement As New SqlClient.SqlCommand( _ “SELECT * FROM Table1″, theDatabase) Read More >>
Visual Studio 2005 Cookbook – Database, Using Transactions
Recipe 13.6. Using Transactions
Problem
You need to issue multiple database updates in the context of a single, atomic transaction.
Solution
Use an ADO.NET transaction to envelop the various SQL statements that need to be processed as a unit.
Discussion
The following block of code connects to a database via ADO.NET and makes several database updates within a single transaction:
' ----- Connect to the database. Dim connectionString As String = _ "Data Source=MySystemSQLEXPRESS;" & _ "Initial Catalog=MyDatabase;Integrated Security=true" Dim theDatabase As New SqlClient.SqlConnection(connectionString) theDatabase.Open( ) ' ----- Create a command object that will hold each ' processed SQL statement. Dim sqlStatement As New SqlClient.SqlCommand sqlStatement.Connection = theDatabase ' ----- Start the transaction. Dim theTransaction As Read More >>
Visual Studio 2005 Cookbook – Database, Using Stored Procedures
Recipe 13.5. Using Stored Procedures
Problem
You need to use a stored procedure in your database, and you’re not sure how to specify values for its input and output parameters.
Solution
Use the command object’s Parameters collection to set and retrieve stored procedure argument values.
Discussion
Here’s a simple SQL Server stored procedure that does nothing more than retrieve a field from a table given its ID value:
CREATE PROCEDURE GetRecordName @PriKey int, @NameResult varchar(50) OUT AS BEGIN -- Given an ID value, return the RecordName field. SET @NameResult = (SELECT RecordName FROM Table1 WHERE ID = @PriKey); END
To use this stored procedure, create a command object that calls it, and add separate input and output parameters: Read more…
Ebook List
Ebook page contain :
- Visual Basic 2005 Cookbook
- Visual Basic 2005, a developers notebook.chm
- OReilly.Visual.Basic.2005.in.a.Nutshell.3rd.Edition.Jan.2006.chm
- Visual Basic 2005 Express Edition For Dummies Dec 2005 eBook-DDU.pdf
You can download from here
Tags: ebook, vb 2005, visual basic 2005
Visual Studio 2005 Cookbook – Database, Using SQL Parameters
Recipe 13.4. Using SQL Parameters
Problem
You need to insert a lot of records into a table, and you would like to simply replace the new data values in the SQL statement without having to build the SQL statement from scratch each time.
Solution
Attach one or more Parameter objects to the Command object.
Discussion
The following SQL Server example inserts new records Read more »
Visual Studio 2005 Cookbook – Database, Retrieving Results from a Database Query
Recipe 13.3. Retrieving Results from a Database Query
Problem
You’ve issued INSERT, UPDATE, and DELETE statements through a command object, but you need to retrieve some data with a SELECT statement.
Solution
Use a DataReader object to quickly review the results of a SELECT statement.
Discussion
The following code retrieves a Read more »
Visual Basic 2005 Cookbook – Database, Issuing SQL Commands
Recipe 13.2. Issuing SQL Commands
Problem
Now that you’ve established a connection to a database through a provider, you’re ready to issue SQL commands. But how?
Solution
Use a Command object to issue SQL commands directly to your database through the provider connection.
Discussion
The following code updates a SQL Server table named Table1, changing every Column2 field to 25 whenever Column1 has a value of 0:
' ----- Connect to the database. Dim connectionString As String = _ "Data Source=MySystem\SQLEXPRESS;" & _ "Initial Catalog=MyDatabase;Integrated Security=true" Dim theDatabase As New SqlClient.SqlConnection(connectionString) theDatabase.Open() ' ----- Prepare the SQL statement for use. Dim sqlStatement As New SqlClient.SqlCommand( _ "UPDATE Table1 SET Column2 = 25 WHERE Column1 = 0", _ theDatabase) sqlStatement.ExecuteNonQuery() ' ----- Clean up. theDatabase.Close() theDatabase.Dispose()
Just like connections, command objects are provider-specific. When using the SQL Server Read more »



