473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Can I get the column name as a string when using LINQ and SqlMetal?

For example, if I have a DB table called DownloadPoints with a column
named DownloadPointNo, then SqlMetal will create a file with a class
called DownloadPoints with an accessor named DownloadPointNo. I would
like the object to be able to pass me the string "DownloadPointNo".
For example, the code could be something like:

DownloadPoints dp = new DownloadPoints();
string colName = dp.DownloadPointNo.ColumnName;

Or I could write a helper function that would find it like this:

DownloadPoints dp = new DownloadPoints();
string colName = HelperFunction(dp.DownloadPointNo);

Is there any way to do this?

thanks in advance,
John
Nov 17 '08 #1
2 10169
Big Daddy wrote:
For example, if I have a DB table called DownloadPoints with a column
named DownloadPointNo, then SqlMetal will create a file with a class
called DownloadPoints with an accessor named DownloadPointNo. I would
like the object to be able to pass me the string "DownloadPointNo".
For example, the code could be something like:

DownloadPoints dp = new DownloadPoints();
string colName = dp.DownloadPointNo.ColumnName;
First of all, if the column name isn't special, then you already know what
it is: it's exactly the same as the name of the property. So whatever you
use to determine the property is what you can use to determine the column name.

For the general case (where the column name might be something that can't be
represented unescaped, like "My Column"), retrieving the ColumnAttribute of
the property will do:

((ColumnAttribute)
typeof(DownloadPoints).GetProperty("DownloadPointN o").GetCustomAttribute(typeof(ColumnAttribute),
false)).Name;

This doesn't just look cumbersome, it actually is, since it uses reflection.
You should strongly consider a properly separated design where you do not
need to know the column name. Use of this knowledge should be restricted to
within your data access layer. SqlMetal generates partial classes, so you
can always add your own members to your DAL classes if you need more. While
such custom members can't be updated automatically in response to changes,
there's dubious value in using LINQ to SQL to begin with if your schema
changes frequently.

--
J.
Nov 17 '08 #2
I've had a similar problem in the past that is sitting and waiting for me to
get back to it. Getting meta data about the table such as column names is
important if you need to build a form dynamically.
"Jeroen Mostert" <jm******@xs4all.nlwrote in message
news:49*********************@news.xs4all.nl...
Big Daddy wrote:
>For example, if I have a DB table called DownloadPoints with a column
named DownloadPointNo, then SqlMetal will create a file with a class
called DownloadPoints with an accessor named DownloadPointNo. I would
like the object to be able to pass me the string "DownloadPointNo".
For example, the code could be something like:

DownloadPoints dp = new DownloadPoints();
string colName = dp.DownloadPointNo.ColumnName;
First of all, if the column name isn't special, then you already know what
it is: it's exactly the same as the name of the property. So whatever you
use to determine the property is what you can use to determine the column
name.

For the general case (where the column name might be something that can't
be represented unescaped, like "My Column"), retrieving the
ColumnAttribute of the property will do:

((ColumnAttribute)
typeof(DownloadPoints).GetProperty("DownloadPointN o").GetCustomAttribute(typeof(ColumnAttribute),
false)).Name;

This doesn't just look cumbersome, it actually is, since it uses
reflection. You should strongly consider a properly separated design where
you do not need to know the column name. Use of this knowledge should be
restricted to within your data access layer. SqlMetal generates partial
classes, so you can always add your own members to your DAL classes if you
need more. While such custom members can't be updated automatically in
response to changes, there's dubious value in using LINQ to SQL to begin
with if your schema changes frequently.

--
J.
Nov 18 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Webgour | last post by:
Hi, I'm tring to add a column to a datagrid with a linkbutton as header that can be used to sort the column. The column and the linkbutton are added programmatically (see below). However the...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
7
by: Andrus | last post by:
var db = new MyDb(connString); causes creation of all objects corresponding to all tables in database. Database contains 500 tables. Application accesses only few tables at a time. Creating...
5
by: =?Utf-8?B?Tmlrb2xheSBQb2Rrb2x6aW4=?= | last post by:
Good noon! Maybe anyone of you knows how generate LINQ Classes programmatically? Thanks in advance!
1
by: john | last post by:
LINQ works a lot like an Access DB Recordset in the way it functions, one of the things I could do in Access is refer to the Fields thru a Variable as below allowing me to Iterate over the...
0
by: Robson Felix | last post by:
Folks, I've been trying to put some legacy databases I have here through SqlMetal. Most of it goes well, but in some stored procedures, I get errors for temp tables that don't exist and so on. I...
3
by: Andy | last post by:
Hi, I use procs for my data access, instead of allowing direct inserts and updates to tables. How can I get SqlMetal to generate code that will use procs? I know the designer you can Configure...
2
by: Joey | last post by:
I am querying a DataSet with LINQ. I am running into a problem when trying to construct my query because in the "from" clause I do not know the table name (range variable) until runtime. Possible...
7
by: Mike P | last post by:
I am working my way through ScottGu's LINQ to SQL tutorials (http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-par t-1.aspx), but am having problems with inserting and deleting...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.