Getting
all data from a table
In the previous “how to add
database to project” post,
you see we add a database, now here we are querying on that database.
This is the
connection string which let you connect to the server:
·
Data Source = "Your Server Name"
·
Initial Catalog = "The name of your database"
·
Integrated Security = "true" for getting your
credentials automatic to your current login account in windows, in this way you
don't have to use user id and password while establishing connection with SQL
server.
static string conString = @"Data
Source = MOAVIA_OSAMA-PC\SQLEXPRESS;Initial Catalog=MyFirst;Integrated
Security=true";
The following is the class
generated when you add "Linq
to SQL classes" to
project, which takes an argument of connection string:
DataClassesDataContext dc = new DataClassesDataContext(conString);
Query
The following query
is a simple query which gets all the data from Customer table from database:
var query = from Customer c in dc.Customers select c;
foreach (var item in query)
Console.WriteLine(item.ID + " " + item.Name + " " +
item.Address);
Note
Customer is the table from database but here
when you querying on it using dc.Customers you see, Customer here becomes Customers the additional "s" is automatic attached. If there is Item table
in your database then here will be Items.
0 comments:
Post a Comment