LINQ: problem obtaining data

  • Thread starter Thread starter Jon Skeet [C# MVP]
  • Start date Start date
J

Jon Skeet [C# MVP]

K Viltersten said:
I got me a DB called "deebee.sdf" and by

DataContext db = new DataContext
(@"C:\deebee.sdf");

i create connection to it. In there a table
called "tabula" resides. Now, the problem
is that i can't get any info from it. I've
tried to follow the tutorial on MS' site by

var query = from tabula in db select tabula;

as well as

IEnumerable<Object> query =
from tabula in db select tabula;

but, as you surely expect, the compiler though
otherwise. I suspect that it's a matter of a
simple sytax confusion. Is it and if so how?

As far as I'm aware, you can't do an awful lot with a "bare"
DataContext. You need to create a specific DataContext - most easily
done with the DBML designer in VS2008.
 
I got me a DB called "deebee.sdf" and by

DataContext db = new DataContext
(@"C:\deebee.sdf");

i create connection to it. In there a table
called "tabula" resides. Now, the problem
is that i can't get any info from it. I've
tried to follow the tutorial on MS' site by

var query = from tabula in db select tabula;

as well as

IEnumerable<Object> query =
from tabula in db select tabula;

but, as you surely expect, the compiler though
otherwise. I suspect that it's a matter of a
simple sytax confusion. Is it and if so how?
 
Konrad,

You need more than just a data context to access the underlying
database, you need to have classes that will map to the underlying tables in
the database.

The easiest way to do this is to add a new LINQ to SQL classes file in
VS.NET which will give you a design surface which you can drag and drop
tables from your database onto. From that designer file, classes will be
created which you can then use in your queries.

You can also do this by hand, creating your own classes and mapping the
attributes in the table to various fields/properties. Take a look at the
section of the MSDN documentation titled "Attribute-Based Mapping (LINQ to
SQL)", located at:

http://msdn2.microsoft.com/en-us/library/bb386971.aspx

It will give you a good starting point if you want to do this by hand.
 
You need more than just a data context
to access the underlying database, you
need to have classes that will map to
the underlying tables in the database.
The easiest way to do this is to add a
new LINQ to SQL classes file in VS.NET...
You can also do this by hand, creating
your own classes and mapping the
attributes in the table to various
fields/properties...
http://msdn2.microsoft.com/en-us/library/bb386971.aspx

Yes, i've seen the description but i
disregarded it (obviously foolishly)
since i expected something far less
advanced, when only accessing a simple
database and performing a short query.

:(

Perhaps it's more appriopriate to
execute a standard SQL query. How does
one do that? I remember something about
sending in strings with SQL statements...
 
Konrad,

Did you not try the designer in VS.NET 2008 (I am not sure if the
Express Editions have it or not, or if you have a version of VS.NET that has
the designer).

With the designer, it is really becomes a drag-and-drop operation.
 
Did you not try the designer in VS.NET 2008.
With the designer, it is really becomes a
drag-and-drop operation.

I believe that i have the designer but when i
drag and drop my table into the area, i get
the error message telling me that the selected
object uses an unsupported data provider.

What i did was to create a new LINQ to SQL
object and try to drag-and-drop the table of
mine onto the empty region in the middle.
 
Back
Top