Hi guys,
The revolution of the computer
programming technology rapidly change, Industry leaders such as Microsoft,
Adobe try to add some values to their product in each releases. Within large
amount of various product Microsoft own “C#” do an incredible revolution change
in computer program developing history. They try to build universal language as
well as universal IDE instead of different IDEs and different programing
languages.
According to their huge vision,
Microsoft introduce Language Integrated Query with their amazing IDE called
Visual Studio in 2008. Basically LINQ provide power full language capability to
C#. Very simple explanation is, you can use only one programing language
instead of SQL, XML etc.
In this demo, I’ll explain how to
retrieve a data from SQL data base without using SQL query. LINQ consider table
as a class, as well as data tuple as an object. That feature make computer
programing technology to an amazing place J.
Demo (Retrieving data from SQL
database to WPF Application)
Create a windows presentation
foundation (WPF) application.
After that drag and drop a ‘list box’ from tool box, then formatted that list box according to our SQL table. In my case, Vehicle table formatted like this.
Using XAML code, we can easily format
list box according to our requirement. Following XAML code segment format list
box according to Vehicle table.
<ListBox x:Name="vehicleListBox" ItemsSource="{Binding}" Margin="0,40,0,0" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Vehicle_Name}" Grid.Column="0" Width="150" FontSize="18" />
<TextBlock Text="{Binding Vehicle_Price}" Grid.Column="1" Width="150" FontSize="18"/>
<Image Source="{Binding Vehicle_Image}" Grid.Column="2" Width="150"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Vehicle_Name}" Grid.Column="0" Width="150" FontSize="18" />
<TextBlock Text="{Binding Vehicle_Price}" Grid.Column="1" Width="150" FontSize="18"/>
<Image Source="{Binding Vehicle_Image}" Grid.Column="2" Width="150"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now we need to build a connection
between SQL server and application, traditionally connection string responsibility
for do this part. But in LINQ, add a .dbml Data classes and call when we need.
Add a .dbml data class to the
project.
Add tables on to design surface.
After that, need to create a data
classes data context object inside the application.
DataClasses1DataContext DB = new DataClasses1DataContext();
Finally we can easily call a values
into special data type call ‘var’, this data type formatted according to
values, which is assign to the variable.
var result = from dt in DB.Vehicles
select dt;
vehicleListBox.ItemsSource = result.ToList<Vehicle>();
select dt;
vehicleListBox.ItemsSource = result.ToList<Vehicle>();
Source code/Project Repo.
Happy coding....
Best regards,
Denuwan Himanga.