This article shows how to create database in XML format using datatable, dataset and datarow classes in Visual Studio 2010.
Insert the data in xml file
Use DataSet class to read and write the data on disc.
- WriteXml(string filename)
- ReadXml(string filename)
The dataset is a collection data table type object. It manages Tables collections.
DataTable - represents a table in XML format in memory. It manages data using Rows andColumns collections.
Open the visual studio and drag one button named (save) three textbox and three TextBlock controls on the form. Form looks like this.
Figure 1.
XAML code
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="TextBlock1"Text="Customerid" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,43,0,0" Name="TextBlock2"Text="Name" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,76,0,0" Name="TextBlock3"Text="Email" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="82,7,0,0" Name="txtCid"VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="82,43,0,0" Name="txtName"VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="82,76,0,0" Name="txtEmail" VerticalAlignment="Top" Width="120" />
<Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="82,134,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
Grid>
Window>
Now add the following VB code.
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.IO
Imports System.Xml
Class MainWindow
Private dt As DataTable
Private ds As DataSet
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
ds = New DataSet()
dt = New DataTable("Customer")
dt.Columns.Add("Cid")
dt.Columns.Add("Name")
dt.Columns.Add("Email")
ds.Tables.Add(dt)
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim dr As DataRow = dt.NewRow()
dr(0) = txtCid.Text
dr(1) = txtName.Text
dt.Rows.Add(dr)
dr(2) = txtEmail.Text
ds.WriteXml("E:/customer4.xml")
MessageBox.Show(" Data has been saved")
txtCid.Clear()
txtEmail.Clear()
txtName.Clear()
End Sub
End Class
Now run the application and enter the customer id value, name and email id and click on the Button save.
Figure 2.
Now click on the OK and record has been saved in the XML file and XML file looks like this.
- <NewDataSet>
- <Customer>
<Cid>3Cid>
<Name>RahulName>
<Email>rahul.20@gmail.comEmail>
Customer>
NewDataSet>
ssource : http://www.vbdotnetheaven.com
0 komentar:
Posting Komentar