I have a function to update a database table with 40 columns. currently I
created the function with 40 parameters which is very ugly, any best
practice to handle this situation? 8 1364
Nick,
I would recommend creating a structure which exposes public fields which
you can set to the values of each of the columns. The fields would be named
after the columns, of course. Then, you just pass that in.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"nick" <nb**************@hotmail.com> wrote in message
news:OD**************@TK2MSFTNGP12.phx.gbl... I have a function to update a database table with 40 columns. currently I created the function with 40 parameters which is very ugly, any best practice to handle this situation?
nick <nb**************@hotmail.com> wrote: I have a function to update a database table with 40 columns. currently I created the function with 40 parameters which is very ugly, any best practice to handle this situation?
Create a type which encapsulates a row of the database, then pass an
instance of that type (or more likely, a reference to an instance of
that type).
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
To go one step further even, when it's this many fields at a time, you may
want to make a class, the represents one row of your data, and have a public
property for each field. The difference between this and a struct is that
you can do additional handling (like checking for field lengths and ensuring
bounds, etc) on each of them.. Because when you have this much data to
update, just doing this field validation can be a handful, so it might be
easier to just save all of that, for this class. for example:
public string LastName
{
set
{
if ( value.Length > 40 )
throw new Exception("LastName must be less than 40 characters");
m_LastName = value;
}
}
HTH
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eH**************@tk2msftngp13.phx.gbl... Nick,
I would recommend creating a structure which exposes public fields which you can set to the values of each of the columns. The fields would be named after the columns, of course. Then, you just pass that in.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"nick" <nb**************@hotmail.com> wrote in message news:OD**************@TK2MSFTNGP12.phx.gbl...I have a function to update a database table with 40 columns. currently I created the function with 40 parameters which is very ugly, any best practice to handle this situation?
Hi,
The cleaner solution is to use a struct. Others solutions may be using an
ArrayList .
Are you keeping those 40 columns in independent variables in your program?
Probably you have them grouped under either a class, struct or even a
DAtaset, you could just that in this function..
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"nick" <nb**************@hotmail.com> wrote in message
news:OD**************@TK2MSFTNGP12.phx.gbl... I have a function to update a database table with 40 columns. currently I created the function with 40 parameters which is very ugly, any best practice to handle this situation?
Thanks, in a multiple tier system, in which layer should the type be
defined? or a common module?
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... nick <nb**************@hotmail.com> wrote: I have a function to update a database table with 40 columns. currently I created the function with 40 parameters which is very ugly, any best practice to handle this situation?
Create a type which encapsulates a row of the database, then pass an instance of that type (or more likely, a reference to an instance of that type).
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Sounds good to add additional check.
However, if I've defined a class with 40 columns and validations code, how
to handle the situation that I need only a couple of the columns (say, the
Primary Key columns) in some other functions? handle the validation
separately? or define a class for each column (sounds too much)?
"RCS" <rs****@gmail.com> wrote in message
news:pW***************@newssvr33.news.prodigy.com. .. To go one step further even, when it's this many fields at a time, you may want to make a class, the represents one row of your data, and have a public property for each field. The difference between this and a struct is that you can do additional handling (like checking for field lengths and ensuring bounds, etc) on each of them.. Because when you have this much data to update, just doing this field validation can be a handful, so it might be easier to just save all of that, for this class. for example:
public string LastName { set { if ( value.Length > 40 ) throw new Exception("LastName must be less than 40 characters"); m_LastName = value; } }
HTH
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:eH**************@tk2msftngp13.phx.gbl... Nick,
I would recommend creating a structure which exposes public fields which you can set to the values of each of the columns. The fields would be named after the columns, of course. Then, you just pass that in.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"nick" <nb**************@hotmail.com> wrote in message news:OD**************@TK2MSFTNGP12.phx.gbl...I have a function to update a database table with 40 columns. currently I created the function with 40 parameters which is very ugly, any best practice to handle this situation?
nick <nb**************@hotmail.com> wrote: Thanks, in a multiple tier system, in which layer should the type be defined? or a common module?
I wouldn't like to say for sure - it depends on the rest of your
system. The question to ask yourself is "Does it represent a valid
business object, or is it database-specific?"
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
I assume you mean like if you are JUST updating an email address for
example, and you just need a PK and the email address??
I'd say still use this option, but just use the pieces that you need:
UserRow objUR = new UserRow();
objUR.PrimaryKey = 143;
objUR.EmailAddress = jd**@somedomain.com;
UpdateEmail(objUR);
and inside UpdateEmail, it will just pick off what it needs, those two
fields. That way you could still use the same logic.. I think that's
reasonable? Off the top of my head, that doesn't strike me as horrible and I
think any badness from doing it that way, is countered by the fact that you
can have all of your field logic in one place and you are reusing it (for
inserts, updates and field-specific updates)..
For what it's worth.. HTH
"nick" <nb**************@hotmail.com> wrote in message
news:uK****************@TK2MSFTNGP09.phx.gbl... Sounds good to add additional check.
However, if I've defined a class with 40 columns and validations code, how to handle the situation that I need only a couple of the columns (say, the Primary Key columns) in some other functions? handle the validation separately? or define a class for each column (sounds too much)?
"RCS" <rs****@gmail.com> wrote in message news:pW***************@newssvr33.news.prodigy.com. .. To go one step further even, when it's this many fields at a time, you may want to make a class, the represents one row of your data, and have a public property for each field. The difference between this and a struct is that you can do additional handling (like checking for field lengths and ensuring bounds, etc) on each of them.. Because when you have this much data to update, just doing this field validation can be a handful, so it might be easier to just save all of that, for this class. for example:
public string LastName { set { if ( value.Length > 40 ) throw new Exception("LastName must be less than 40 characters"); m_LastName = value; } }
HTH
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:eH**************@tk2msftngp13.phx.gbl... Nick,
I would recommend creating a structure which exposes public fields which you can set to the values of each of the columns. The fields would be named after the columns, of course. Then, you just pass that in.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"nick" <nb**************@hotmail.com> wrote in message news:OD**************@TK2MSFTNGP12.phx.gbl... I have a function to update a database table with 40 columns. currently I created the function with 40 parameters which is very ugly, any best practice to handle this situation?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Zlatko Matić |
last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL
Server) and that we want to execute some parameterized query as a
pass.through query. How can we pass parameters to the...
|
by: Zlatko Matić |
last post by:
Hi everybody!
Recently I was struggling with client/server issues in MS Access/PostgreSQL
combination.
Although Access is intuitive and easy to use desktop database solution, many
problems...
|
by: kinaxx |
last post by:
Hello,
now I'm learning progamming language in university.
but i have some question.
in textbook. says there are four passing Mechanism
1) pass by value (inother words : call by value)
2)...
|
by: gumby |
last post by:
I would like to call this stored procedure, but I am unable to pass
parameters to the @Start and @End.
Is thier a way to pass parameters to a pass through query from MS
Access?
SELECT ...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
| |
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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...
|
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...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |