
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.Data.SqlClient;
namespace LinqExamples
{
public partial class LINQtoDataSet : Form
{
string connectionString = "Data Source=LocalHost; Initial CataLog=ERPFinAccounting;
User ID=kris; Password=rooman123";
SqlConnection myConnection;
string str_Account_Select = "SELECT * FROM AccountsTable";
SqlCommand myCommand;
SqlDataAdapter myAdapter;
DataSet myDataSet;
private void LINQtoDataSet_Load(object sender, EventArgs e)
{ //Instantiate the Connection object
myConnection = new SqlConnection(connectionString);
myConnection.Open();
myCommand = new SqlCommand(str_Account_Select, myConnection);
//Instantiate DataSet object
myDataSet = new DataSet();
myAdapter= new SqlDataAdapter();
//Set DataAdapter command properties
myAdapter.SelectCommand = myCommand;
//Populate the Dataset
myAdapter.Fill(myDataSet, "AccountsTable");
DataTable accounts = myDataSet.Tables["AccountsTable"];
var query =
from o in accounts.AsEnumerable()
where o.Field("AccountName")== "Account1"
select o;
listBox1.DataSource = query.ToList();
listBox1.DisplayMember = "AccountName";
}
}
}
Copyright © 2012 - All Rights Reserved - VKInfotek.com