How to convert a simple DLINQ Query to a data set object
来源:互联网 发布:笔记本蹭网软件 编辑:程序博客网 时间:2024/06/05 20:18
In this article I will represent a trick of how to convert a given DLINQ query to a data set object in order to make use of it as a data source for several controls such as grid view, data grid view and so forth. As the Query object couldn't be directly used unless in very particular cases with particular controls. So I invite you to follow this walkthrough:
During this tutorial the NorthWind SQL Server data base is used as a data source. In order to download it you can reach it via this link
http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034&displaylang=en
Walkthrough:
1. Create a new windows project and name it LinqQueyToDataSet
2. Add a reference to the System.Data.Linq
3. Add a data grid view into the Form1 and set its Dock property to fill
4. Add a reference to the System.Data.Linq.Mappinq that enables us to define Entity classes witches are a sort of proxies' objects that represent the effective tables located in the NorthWind data base
5. Add this entity class within the scope of the form1 class
[Table(Name="Employees" )]
class Employee
{
[Column(Name = "EmployeeID", IsPrimaryKey = true)]
public int Identifier;
[Column(Name = "LastName" )]
public string LastName;
[Column(Name = "FirstName" )]
public string FirstName;
[Column(Name = "Title")]
public string Title;
[Column(Name = "BirthDate")]
public string BirthDate;
[Column(Name = "Address" )]
public string Address;
[Column(Name = "City")]
public string City;
[Column(Name = "Country" )]
public string Country;
}
6. Establish the connection to the NorthWind data base by defining a DataContext object as bellow
7. Implement the load event handler of the Form1 as follow:
8. Run the application now
That's it.
DataContext oNorthWind = new DataContext(@"Data Source=STANDARD;Initial Catalog=" + @"C:/SQL SERVER 2000 SAMPLE DATABASES/NORTHWND.MDF';" +
"Integrated Security=True");
private void Form1_Load(object sender, EventArgs e)
{
/* This is a simple DLinq Query that selects all rows from the
employee data table
*/
var Query = from emp in oNorthWind.GetTable()
select emp;
/*This command will get all inforamtion from the Query. The transformation
will be done thanks to the GetCommand of the data context object*/
SqlCommand oCommand = oNorthWind.GetCommand(Query) as SqlCommand;
//The data adapter will be used to fill data
SqlDataAdapter oAdapter = new SqlDataAdapter();
//The rest is known....
oAdapter.SelectCommand = oCommand;
DataSet oDataSet = new DataSet();
oAdapter.Fill(oDataSet);
dataGridView1.DataSource = oDataSet;
dataGridView1.DataMember = oDataSet.Tables[0].TableName;
}
- How to convert a simple DLINQ Query to a data set object
- How to convert a type object from unshared to shared.
- How to convert a String to DateTime
- How to divide a data set into equal size buckets
- how to convert a object to json file immediately during debug?
- How to convert a Drawable to a Bitmap?
- How to write a simple Makefile
- NHiberante How to mapping a simple entity?
- How to create a simple Thread
- How to Write a Simple UnitTest Framework
- How To Start a simple OAI Simulaton
- How to write a simple shell program
- How to execute a query in varchar2 ?
- How to Improve Query Performance - A Checklist
- How to set up a simple mail server on Debian in 5 easy steps
- How to convert a date to internal or external format?
- How to convert a latex to pdf file
- Swift: How to convert a String to UInt8 array?
- iframe编码统一问题
- web2.0商业化案例:西祠花嫁频道
- 毕业设计(高校迎新系统)关于C#中数据库循环插入问题
- 事务日志备份与恢复
- one rac troubleshooting
- How to convert a simple DLINQ Query to a data set object
- DLL(动态链接库)专题
- 网站IP地址查询小工具
- 温习static,sealed,abstract,virtual
- 生产力、商业价值和敏捷
- CA认证原理
- DirectShow在VS2005中PVOID64问题和配置问题
- SAP Business One SDK之UI API
- -- HTML标签详解