PDA

View Full Version : اجرای یک کوعری بر روی یک دیتاست



pirmard
شنبه 29 فروردین 1388, 21:30 عصر
سلام
خسته نباشید
یه سوال داشتم
فرض کنید دو تا تیبل داریم که از این دو تا تیبل مثلا به وسیله ی یک inner join یک دیتاست ایجاد کردیم و اونو توی گرید نمایش میدیم
حالا چطور میشه به این دیتا ست به شکل یک تیبل نگاه کنیم و یک کوعری روی اون اجرا کنیم و نتیجه اونو توی یک دیتاست بزاریم و بعد توی گرید نمایش بدیم ؟

ممنون

khaste_007
یک شنبه 30 فروردین 1388, 02:27 صبح
با سلام البته دیتا ست رو نمی تونید به شکل یک تیبل نگاه کنید بلکه مجموعه ای از تیبل ها درون اون ذخیره می شه و شما یک روش نوین که می تونید کوئری بنویسید هم برای sql یا oracle یا xml یا منابع داده ای نظیر دیتا ست، LINQ هست.

pirmard
یک شنبه 30 فروردین 1388, 02:31 صبح
با سلام البته دیتا ست رو نمی تونید به شکل یک تیبل نگاه کنید بلکه مجموعه ای از تیبل ها درون اون ذخیره می شه و شما یک روش نوین که می تونید کوئری بنویسید هم برای sql یا oracle یا xml یا منابع داده ای نظیر دیتا ست، LINQ هست.


امکانش هست یه مثال هم بزارین در این مورد ؟

khaste_007
دوشنبه 31 فروردین 1388, 18:03 عصر
با سلام یک مثال خیلی خوب و کامل از کتاب معتبر Wrox Beginning Microsoft Visual C Sharp 2008 قرار می دم امیدوارم مفید باشه.



Using LINQ over DataSet :
Follow these steps to create the LINQoverDataSet program in Visual Studio 2008:
1. Create a new console application project called LINQoverDataSet in the directory
C:\BegVCSharp\Chapter28.
2. Add a reference to the System.Data.Linq component as shown in Figure 28 - 7 .
(خطا در آپلود فایل)
Figure 28-7
Note that LINQ over DataSet also uses the System.Data.DataSetExtensions namespace,
which is automatically added by Visual Studio.
When you use LINQ over DataSet you are using a small part of the LINQ to Entity facility mentioned
at the start of this chapter. While LINQ to Entity in its entirety is beyond the scope of a beginning book
like this one, LINQ to DataSet is a simple part of LINQ to Entity that you can use immediately based
on what you have already learned about LINQ.
3. Begin by adding the using directives for the ADO.NET and LINQ classes you will be using:

#region Using Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data; // Use ADO.NET namespace
using System.Data.SqlClient; // Use SQL Server data provider namespace
using System.Data.Linq; // Use LINQ / ADO.NET connector
using System.Data.Common;
using System.Text;
#endregion4. Add the following code to the Main() method. Because this code is the same as the start of the
code from the DataRelationExample program, you may find it convenient to cut and paste
from that source file:

static void Main(string[] args)
{
// Specify SQL Server-specific connection string
SqlConnection thisConnection = new SqlConnection(
@”Data Source=.\SQLEXPRESS;”+
@”AttachDbFilename=’C:\SQL Server 2000 Sample Databases\
NORTHWND.MDF’;” +
@”Integrated Security=True;Connect Timeout=30;User Instance=true” );
// Create DataAdapter object for update and other operations
SqlDataAdapter thisAdapter = new SqlDataAdapter(
“SELECT CustomerID, CompanyName FROM Customers”, thisConnection);
// Create CommandBuilder object to build SQL commands
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
// Create DataSet to contain related data tables, rows, and columns
DataSet thisDataSet = new DataSet();
// Set up DataAdapter objects for each table and fill
SqlDataAdapter custAdapter = new SqlDataAdapter(
“SELECT * FROM Customers”, thisConnection);
SqlDataAdapter orderAdapter = new SqlDataAdapter(
“SELECT * FROM Orders”, thisConnection);
custAdapter.Fill(thisDataSet, “Customers”);
orderAdapter.Fill(thisDataSet, “Orders”);
// Set up DataRelation between customers and orders
DataRelation custOrderRel = thisDataSet.Relations.Add(“CustOrders”,
thisDataSet.Tables[“Customers”].Columns[“CustomerID”],
thisDataSet.Tables[“Orders”].Columns[“CustomerID”]);5. Now add the following code that uses LINQ:

var customers = thisDataSet.Tables[“Customers”].AsEnumerable();
var orders = thisDataSet.Tables[“Orders”].AsEnumerable();
var preferredCustomers =
from c in customers
where c.GetChildRows(“CustOrders”).Length > 10
orderby c.GetChildRows(“CustOrders”).Length
select c;
Console.WriteLine(“Customers with > 10 orders:”);
foreach (var customer in preferredCustomers)
{
Console.WriteLine(“{0} orders: {1} {2}, {3} {4}”,
customer.GetChildRows(“CustOrders”).Length,
customer[“CustomerID”], customer[“CompanyName”],
customer[“City”], customer[“Region”] );
}6. Finish the Main() method with the standard closing logic used in our other examples:

thisConnection.Close();
Console.Write(“Program finished, press Enter/Return to
continue:”);
Console.ReadLine();7. Compile and execute the application. You should see this output:
12 orders: LINOD LINO-Delicateses, I. de Margarita Nueva Esparta
12 orders: REGGC Reggiani Caseifici, Reggio Emilia
12 orders: SUPRD Supr ê mes d é lices, Charleroi
13 orders: AROUT Around the Horn, London
13 orders: MEREP M è re Paillarde, Montr é al Qu é bec
13 orders: QUEEN Queen Cozinha, Sao Paulo SP
14 orders: BOTTM Bottom-Dollar Markets, Tsawassen BC
14 orders: HANAR Hanari Carnes, Rio de Janeiro RJ
14 orders: KOENE K ö niglich Essen, Brandenburg
14 orders: LAMAI La maison d’Asie, Toulouse
14 orders: LILAS LILA-Supermercado, Barquisimeto Lara
14 orders: WHITC White Clover Markets, Seattle WA
15 orders: FRANK Frankenversand, M ü nchen
15 orders: LEHMS Lehmanns Marktstand, Frankfurt a.M.
15 orders: WARTH Wartian Herkku, Oulu
17 orders: BONAP Bon app’, Marseille
18 orders: BERGS Berglunds snabbk ö p, Lule å
18 orders: HILAA HILARION-Abastos, San Crist ó bal T á chira
18 orders: RATTC Rattlesnake Canyon Grocery, Albuquerque NM
19 orders: FOLKO Folk och f ä HB, Br ä cke
19 orders: HUNGO Hungry Owl All-Night Grocers, Cork Co. Cork
28 orders: QUICK QUICK-Stop, Cunewalde
30 orders: ERNSH Ernst Handel, Graz
31 orders: SAVEA Save-a-lot Markets, Boise ID
Program finished, press Enter/Return to continue: