posts - 82, comments - 0, trackbacks - 0

My Links

News

Archives

Post Categories

C# Download

Facebook

Informativa cookie

Linkedin

Microsoft

Msdn Gallery

Twitter

VisualBasic

Wiki Tecknet

Querying Data with LINQ to DataSet

This simple code example demonstrates how you can make the query a dataset by using LINQ.Linq and integral part of VisualStudio 2008 and as of version. NETFramework 3.5.Sebbene Linq is subdivided into different types, Linq to Objects, LINQ to adonet, Linq to Sql, Linq to Datset, Linq to Entities and LINQ to XML, this example focuses on using LINQ to DataSet. Will be shown how to perform search queries on the dataset, see the syntax used and we will also see how the query result is then displayed within the form.

The example consists of a database called Rubrica.sdf with an internal table called DatiPersonali, used by a ComboBox control to select the type of research to be performed within the database, a TextBox control where you need to enter what you want to try in the datbase, a Button control to start the search, a Button control to run the Refresco to redisplay all the data after completing the search and finally a DataGrid control that is generated by the wizard of the dataset.

The sample includes the ability to make key commands for manipulating the data or the insert, update, and delete the. Below I show the complete code.



/ / Namespace
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

/ / Sample_query_Linq
namespace Sample_query_Linq
{
   
/ / Class frmMain
   
public partial class frmMain: Form
   
{
       
/ / Constructor of clsee frmMain
       
public frmMain () {/ / Call the method InitializeComponent
           
InitializeComponent ();
           
/ / Call the method SetUpDataGridView
           
SetUpDataGridView (dATIPERSONALIDataGridView);
       
}

       
/ / Button Click Event dATIPERSONALIBindingNavigatorSaveItem
       
private void dATIPERSONALIBindingNavigatorSaveItem_Click (object sender, EventArgs e)
       
{
           
this.Validate ();
           
this.dATIPERSONALIBindingSource.EndEdit ();
           
this.tableAdapterManager.UpdateAll (this.rubricaDataSet);
       
}

       
/ / Load event of the form frmMain
       
private void Form1_Load (object sender, EventArgs e)
       
{
           
/ / TODO: This line of code loads data into table 'rubricaDataSet.DATIPERSONALI'. You can move it or remove it if necessary.
           
this.dATIPERSONALITableAdapter.Fill (this.rubricaDataSet.DATIPERSONALI);
       
}

       
/ / Button Click Event btnTrova
       
private void btnTrova_Click (object sender, EventArgs e)
       
{
           
/ / Execute switch construct the value of cbxRicerca
           
switch (this.cbxRicerca.Text)
           
{
               
/ / If equal to cbxRicerca NAME
               
case "NAME":
                   
/ / Run the query in the table DATIPERSONALI
                   
/ / If there are c.NOME values ​​equal to the values ​​txtRicerca
                   
/ / I choose c
                   
var = from c in queryNome this.rubricaDataSet.DATIPERSONALI
                                   
where c.NOME.ToString (). ToUpper () == this.txtRicerca.Text.ToString (). ToUpper ()
                                   
select c;

                   
/ / Check the DataSource property of the result dATIPERSONALIBindingSource
                   
/ / Search queries through an object with support for LINQ that Data.DataView
                   
/ / Represents the LINQ to DataSet
                   
this.dATIPERSONALIBindingSource.DataSource queryNome.AsDataView = ();


                   
break;

               
/ / If equal to cbxRicerca SURNAME
               
case "NAME":
                   
/ / Run the query in the table DATIPERSONALI
                   
/ / If there are c.COGNOME values ​​equal to the values ​​txtRicerca
                   
/ / I choose c
                   
var = from c in queryCognome this.rubricaDataSet.DATIPERSONALI
                                      
where c.COGNOME.ToString (). ToUpper () == this.txtRicerca.Text.ToString (). ToUpper ()
                                      
select c;

                   
/ / Check the DataSource property of the result dATIPERSONALIBindingSource
                   
/ / Search queries through an object with support for LINQ that Data.DataView
                   
/ / Represents the LINQ to DataSet
                   
this.dATIPERSONALIBindingSource.DataSource queryCognome.AsDataView = ();
                   
break;

               
/ / If equal to cbxRicerca ADDRESS
               
case "ADDRESS":
                   
/ / Run the query in the table DATIPERSONALI
                   
/ / If there are c.INDIRIZZO values ​​equal to the values ​​txtRicerca
                   
/ / I choose c
                   
var = from c in queryIndirizzo this.rubricaDataSet.DATIPERSONALI
                                        
where c.INDIRIZZO.ToString (). ToUpper () == this.txtRicerca.Text.ToString (). ToUpper ()
                                        
select c;

                   
/ / Check the DataSource property of the result dATIPERSONALIBindingSource
                   
/ / Search queries through an object with support for LINQ that Data.DataView
                   
/ / Represents the LINQ to DataSet
                   
this.dATIPERSONALIBindingSource.DataSource queryIndirizzo.AsDataView = ();
                   
break;

               
/ / If equal to cbxRicerca TELEPHONE
               
case "TELEPHONE"
                   
/ / Run the query in the table DATIPERSONALI
                   
/ / If there are c.TELEFONO values ​​equal to the values ​​txtRicerca
                   
/ / I choose c
                   
var = from c in queryTelefono this.rubricaDataSet.DATIPERSONALI
                                       
where c.TELEFONO.ToString (). ToUpper () == this.txtRicerca.Text.ToString (). ToUpper ()
                                       
select c;

                   
/ / Check the DataSource property of the result dATIPERSONALIBindingSource
                   
/ / Search queries through an object with support for LINQ that Data.DataView
                   
/ / Represents the LINQ to DataSet
                   
this.dATIPERSONALIBindingSource.DataSource queryTelefono.AsDataView = ();
                   
break;

               
/ / If equal to cbxRicerca NATIONALITY '
               
case "NATIONALITY '"
                   
/ / Run the query in the table DATIPERSONALI
                   
/ / If c.NAZIONALITA 'values ​​are equal to the values ​​txtRicerca
                   
/ / I choose c
                   
var = from c in queryNazionalita this.rubricaDataSet.DATIPERSONALI
                                          
where c.NAZIONALITA.ToString (). ToUpper () == this.txtRicerca.Text.ToString (). ToUpper ()
                                          
select c;

                   
/ / Check the DataSource property of the result dATIPERSONALIBindingSource
                   
/ / Search queries through an object with support for LINQ that Data.DataView
                   
/ / Represents the LINQ to DataSet
                   
this.dATIPERSONALIBindingSource.DataSource queryNazionalita.AsDataView = ();
                   
break;
           
}
       
}

       
/ / Button Click Event btnRefresch
       
private void btnRefresch_Click (object sender, EventArgs e)
       
{
           
/ / Reassign the data source rubricaDataSet.DATIPERSONALI
           
this.dATIPERSONALIBindingSource.DataSource = rubricaDataSet.DATIPERSONALI;
       
}

       
/ / Method SetUpDataGridView
       
private void SetUpDataGridView (DataGridView myDataGrid)
       
{
           
DataGridViewCellStyle style = myDatagrid.ColumnHeadersDefaultCellStyle;
           
style.BackColor = Color.Navy;
           
style.ForeColor = Color.White;
           
style.Font = new Font (myDatagrid.Font, FontStyle.BOLD);

           
myDatagrid.EditMode = DataGridViewEditMode.EditOnEnter;
           
myDatagrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;

           
myDatagrid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
           
myDatagrid.CellBorderStyle = DataGridViewCellBorderStyle.Single;
           
myDatagrid.GridColor = SystemColors.ActiveBorder;
           
myDatagrid.RowHeadersVisible = false;

           
myDatagrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
           
myDatagrid.MultiSelect = false;
           
myDatagrid.BackgroundColor = Color.White;
       
}
   
}
}


The query in question by looking at the code executes through education confra Where a national field of the table if the DATA is the text in the TextBox control txtRicerca, and returns to the DataSource property of an object datipersonaliBindingSourse System.Data.DataView. For more information on the DataView we mark questolink



/ / If equal to cbxRicerca NATIONALITY '
               
case "NATIONALITY '"
                   
/ / Run the query in the table DATIPERSONALI
                   
/ / If c.NAZIONALITA 'values ​​are equal to the values ​​txtRicerca
                   
/ / I choose c
                   
var = from c in queryNazionalita this.rubricaDataSet.DATIPERSONALI
                                          
where c.NAZIONALITA.ToString (). ToUpper () == this.txtRicerca.Text.ToString (). ToUpper ()
                                          
select c;

                   
/ / Check the DataSource property of the result dATIPERSONALIBindingSource
                   
/ / Search queries through an object with support for LINQ that Data.DataView
                   
/ / Represents the LINQ to DataSet
                   
this.dATIPERSONALIBindingSource.DataSource queryNazionalita.AsDataView = ();
                   
break;

With this simple example and can see how Linq is an excellent tool for language and ricerda data in a Data Source

Print | posted on domenica 3 luglio 2011 18:34 | Filed Under [ Articles C# in English ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 8 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET