473,758 Members | 5,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "DownloadPointN o".
For example, the code could be something like:

DownloadPoints dp = new DownloadPoints( );
string colName = dp.DownloadPoin tNo.ColumnName;

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

DownloadPoints dp = new DownloadPoints( );
string colName = HelperFunction( dp.DownloadPoin tNo);

Is there any way to do this?

thanks in advance,
John
Nov 17 '08 #1
2 10192
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 "DownloadPointN o".
For example, the code could be something like:

DownloadPoints dp = new DownloadPoints( );
string colName = dp.DownloadPoin tNo.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:

((ColumnAttribu te)
typeof(Download Points).GetProp erty("DownloadP ointNo").GetCus tomAttribute(ty peof(ColumnAttr ibute),
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******@xs4al l.nlwrote in message
news:49******** *************@n ews.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 "DownloadPointN o".
For example, the code could be something like:

DownloadPoints dp = new DownloadPoints( );
string colName = dp.DownloadPoin tNo.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:

((ColumnAttribu te)
typeof(Download Points).GetProp erty("DownloadP ointNo").GetCus tomAttribute(ty peof(ColumnAttr ibute),
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
8908
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 problem is that when you click the added column header it doesn't trigger the sort. The code : <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
1
4237
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
7
2651
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 large number of objects which are not used is bad design. How to force Linq-SQL to create table objects only when they are first
5
2607
by: =?Utf-8?B?Tmlrb2xheSBQb2Rrb2x6aW4=?= | last post by:
Good noon! Maybe anyone of you knows how generate LINQ Classes programmatically? Thanks in advance!
1
4550
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 Recordset with a test or slight change to field names. Is there a way to Replicate this Method in LINQ, I've tried setting the Variable as an Object "Dim tDayID AS Object = "tem.Day" & 1" but it did not work? IN ACCESS: Dim DaI As String DaI =...
0
1031
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 don't think I can simply go and change every single object of mine. My question is: is there a way to have SqlMetal generate a dataset for each stored procedure call rather than a customized result for each procedure? I understand that...
3
1960
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 Behavior to do this.. is there no equivolent in Sql Metal? Thanks
2
4809
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 table names might include "SomeTable1" or "SomeTable236" etc... Unfortunately when I try to use something like "from SomeTable + MyChangingNumber.ToString() in..." in my from clause it does not work. How can I set this up to use a range variable...
7
1488
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 data (3 : Insert a New Category and Two New Products into the Database) and (4 : Delete Products from the Database). Are the RemoveAll and Add methods now out of date? And if so, what has replaced them?
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.