How to XML in VB.NET

posted under by Prav

XML is a general purpose tag based language and very easy to transfer and store data across applications. Like HTML , XML is a subset of SGML - Standard Generalized Markup Language. XML is a platform independent language, so the information formatted in XML can be used in any other platforms (Operating Systems). XML is a self describing language and it gives the data as well as the rules to identify what information it contains.

XML files are made up of tags that contains data. Generally a start tag and end tag to hold the data. For example, if you want to create an XML tag name "Header" , the start tag is like <> and the end tag is like < /Header > . We can fill our information between these tags.

<> Header Content Here < /Header >

While creating an XML file , some important points have to remember :

* XML is case sensitive

ex: <> is not same as <> .

* Tags must be closed in the reverse order that they were opened

ex : <><> Data here < /second-tag > < /first-tag >

Sample XML File

product.xnl

The .Net technology is widely supported XML file format. The .Net Framework provides the Classes for read, write, and other operations in XML formatted files . These classes are stored in the namespaces like System.Xml, System.Xml.Schema, System.Xml.Serialization, System.Xml.XPath, System.Xml.Xsl etc. The Dataset in ADO.NET uses XML as its internal storage format.

You can use any text editor to create an XML file . More over XML files are readable by humans as well as computers. From the following links you can see how to use XML in VB.NET.

How to create a DataView

posted under by Prav

The DataView provides different views of the data stored in aDataTable. DataView can be used to sort, filter, and search in a DataTable , additionally we can add new rows and modify the content in a DataTable. DataViews can be created and configured both design time and run time . Changes made to a DataView affect the underlying DataTable automatically, and changes made to the underlying DataTable automatically affect any DataView objects that are viewing the DataTable.

We can create DataView in two different ways. We can use theDataView constructor, or you can create a reference to the DefaultView property of the DataTable. The DataView constructor can be empty, or it can take either a DataTable as a single argument, or a DataTable along with filter criteria, sort criteria, and a row state filter.

dataView = dataSet.Tables(0).DefaultView

The following source code shows how to create a DataView in VB.NET. Create a new VB.NET project and drag a DataGridView and a Button on default Form Form1 , and copy and paste the following Source Codeon button click event.

Imports System.Data.SqlClient Public Class Form1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         Dim connetionString As String         Dim connection As SqlConnection         Dim command As SqlCommand         Dim adapter As New SqlDataAdapter         Dim ds As New DataSet         Dim dv As DataView         Dim sql As String   connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"         sql = "Select  * from product"         connection = New SqlConnection(connetionString)         Try             connection.Open()             command = New SqlCommand(sql, connection)             adapter.SelectCommand = command             adapter.Fill(ds, "Create DataView")             adapter.Dispose()             command.Dispose()             connection.Close()              dv = ds.Tables(0).DefaultView             DataGridView1.DataSource = dv          Catch ex As Exception             MsgBox(ex.ToString)         End Try     End Sub End Class  

Dataadapter with dataset - sql sever

posted under by Prav

SqlDataAdapter provides the communication between the Dataset and the Data Source with the help of SqlConnection Object . TheSqlConnection Object has no information about the data it retrieves . Similarly a Dataset has no knowledge of the Data Source where the data coming from. So the SqlDataAdapter manage the communication between these two Objects.

The SqlDataAdapter object allows us to populate Data Tables in a DataSet. We can use Fill method of the SqlDataAdapter for populating data in a Dataset. The following source code shows a simple program that uses SqlDataAdapter to retrieve data from Data Source with the help of SqlConnection object and populate the data in a Dataset.

Imports System.Data.SqlClient Public Class Form1     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         Dim connetionString As String         Dim connection As SqlConnection         Dim adapter As SqlDataAdapter         Dim ds As New DataSet         Dim i As Integer   connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"         connection = New SqlConnection(connetionString)         Try             connection.Open()             adapter = New SqlDataAdapter("Your SQL Statement Here", connection)             adapter.Fill(ds)             connection.Close()             For i = 0 To ds.Tables(0).Rows.Count - 1                 MsgBox(ds.Tables(0).Rows(i).Item(1))             Next         Catch ex As Exception             MsgBox(ex.ToString)         End Try     End Sub End Class  

What is ADO.NET Dataset

posted under by Prav

The ADO.NET DataSet contains DataTableCollection and their DataRelationCollection . It represents a collection of data retrieved from the Data Source. We can use Dataset in combination with DataAdapterclass. The DataSet object offers a disconnected data source architecture. The Dataset can work with the data it contain, without knowing the source of the data coming from. That is , the Dataset can work with a disconnected mode from its Data Source . It gives a better advantage over DataReader , because the DataReader is working only with the connection oriented Data Sources.

ado.net-dataset

The Dataset contains the copy of the data we requested. The Dataset contains more than one Table at a time. We can set up Data Relationsbetween these tables within the DataSet. The data set may comprise data for one or more members, corresponding to the number of rows.

dataset

The DataAdapter object allows us to populate DataTables in a DataSet. We can use Fill method of the DataAdapter for populating data in a Dataset. The DataSet can be filled either from a data source or dynamically. A DataSet can be saved to an XML file and then loaded back into memory very easily. The following links shows more information of Dataset in details.


Advantages of ADO.Net over ADO

posted under by Prav

ADO stands for ActiveX Data Objects and it relies on COM whereas ADO.NET relies on managed providers defined by the .NET CLR (Common Language Runtime). ADO.NET provides consistent access todata sources such as SQL Server, as well as data sources exposed through OLE DB and XML. While there are similarities between ADO and ADO.NET, the way they operate and their foundations are quite different. The following are some Advantages of ADO.Net over ADO in basic level.

A major difference in creating connections with ADO and ADO.NET is that ADO fits all connections to all types of data sources into a single Connection object. ADO.NET can have separate Objects that represent connections to different data sources. In ADO.NET you can create multiple data provider namespaces to connect specifically with a particular data source, making access faster and more efficient and allowing each namespace to exploit the features of its targeted data provider.

  Dim connection As SqlConnection 

connection = New SqlConnection("connetionString")

  Dim connection As OleDbConnection 

connection = New OleDbConnection("connetionString")

ADO allows you to create client side cursors only whereas ADO.NET gives you the choice of either using client side or server side cursors.

ADO.NET introduces a new way of getting a single value from a query's results when you expect only one row and one column to return. The ADO.NET command object has an ExecuteScalar method which returns the first row and column's value from its associated query.

ADO.Net dataset represents in memory representation of a database. ADO recordsets is merely a set of rows retrieved from a data source.

ADO recordsets can hold data from one data source at a time. ADO.Net datasets can hold data from various sources and integrate the data and write it back to one or several data sources.

The ADO.NET Framework supports two models of Data Access Architecture, Connection Oriented Data Access Architecture and Disconnected Data Access Architecture.

In the case of Data Communication , ADO objects communicate in binary mode while ADO.NET uses XML for passing the data.

You can find more information on ADO to ADO.NET from the following link :

http://msdn.microsoft.com/en-us/magazine/cc163954.aspx

ADO.NET Data Providers

posted under by Prav

The .Net Framework includes mainly three Data Providers for ADO.NET. They are the Microsoft SQL Server Data Provider ,OLEDB Data Provider and ODBC Data provider. You can see from the following links how these Data Providers making connection to the specified data Sources.

SQL Server Connection

OLEDB Connection

ODBC Connection

dataprovider.JPG

The four Objects from the .Net Framework provide the functionality of Data Providers in ADO.NET. They are Connection Object, Command Object , DataReader Object and DataAdapter Object. The following link shows in details about these Objects.

Connection

Command

DataReader

DataAdapter

ADO.NET ConnectionString

posted under by Prav

Connection String is a normal String representation which contains Database connection information to establish the connection between Datbase and the Application. The Connection String includes parameters such as the name of the driver, Server name and Database name , as well as security information such as user name and password. Data providers use a connection string containing a collection of parameters to establish the connection with the database.

The .NET Framework provides mainly three data providers: MicrosoftSQL Server, OLEDB and ODBC. Here you can see how to make connection string to these ADO.NET Data Providers.

Microsoft SQL Server Connection String

connetionString ="Data Source = ServerName; Initial Catalog = Databasename; User ID = UserName; Password=Password"

OLEDB Data Provider Connection String

connetionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = yourdatabasename.mdb;"

ODBC Connection String

connetionString = "Driver = {Microsoft Access Driver (*.mdb)}; DBQ = yourdatabasename.mdb;"

Note : You have to provide the necessary informations to the Connection String attributes.

In the following section you can see how to these ADO.NET Data Providers establish connection to the Databse in detail.

SQL Server Connection

OLEDB Connection

ODBC Connection

top