Centralized data in .NET?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 27
Reputation: Terry Robinson is an unknown quantity at this point 
Solved Threads: 1
Terry Robinson Terry Robinson is offline Offline
Light Poster

Centralized data in .NET?

 
0
  #1
Jun 27th, 2007
I've used Delphi for the past 12 years, but now I need to get up to speed in C#. Since both systems were designed by the same person, there are lots of similarities. However, C# seems to have a big gap when it comes to centralized data access. All of the examples I've seen show the connections, commands and datasets and so forth all being placed on individual forms with no interconnection.

The applications I work with have numerous primary and support tables with related queries and so on. If I have to redelcare these in every form where they are used, the code overhead will be huge and maintenance becomes a nightmare because I must make changes in multiple locations.

Delphi provides a container class called a 'data module' where connections and data access components can be installed during design. Data modules are also used to hold support code for events, business rules and so forth all in one place. Once defined, data aware controls in forms or other units can access those components as need be either during design or run time. This obviously reduces code and makes design and maintenace much easier.

I am assuming there is some way to do this, or at least achieve the same end, in .NET. However, I haven't come across an answer yet. Can anyone point me in the right direction? I don't want to do any serious work until I figure this problem out. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Centralized data in .NET?

 
0
  #2
Jun 28th, 2007
i ran into an issue such as this a while back and ended up just writing a library to handle all SQL connections so that all forms had a central method for running their methods.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 27
Reputation: Terry Robinson is an unknown quantity at this point 
Solved Threads: 1
Terry Robinson Terry Robinson is offline Offline
Light Poster

Re: Centralized data in .NET?

 
0
  #3
Jun 28th, 2007
Originally Posted by Killer_Typo View Post
i ran into an issue such as this a while back and ended up just writing a library to handle all SQL connections so that all forms had a central method for running their methods.
I was afraid that might be the answer.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Centralized data in .NET?

 
0
  #4
Jun 29th, 2007
I am also a convert from Delphi (started with D1, ended with D2005).

There are many ideas out there on how to centralize your data. The idea of a Data Module is not as foreign to C# as you might think. After all, a TDataModule is nothing more than a Class with functions, procedures, Properties and the ability to include public DataSets, public TADOConnection, etc. A TDataModule in Delphi is developed by the programmer with a specific task in mind. Lets say the data components allow for accessing payroll information. In Delphi you typically try to encapulate the data access and attributes of the payroll system within your TDataModule.

A C# class is all of that and more. You can create a new class for your application, and think of it just like a TDataModule. Add your public variables or components just like in Delphi. For instance, you can instanciate an SqlConnection in the same way you instanciate a TAdoConnection.

It takes some getting used to, and some things take more code than in Delphi, but I find that c# is more powerful, and getting a forward only dataset is much faster in dot-Net than ADO or BDE in Delphi.

BTW: I still work with Delphi everyday in support of our legacy product. I am also working on the new development projects in C#.

//Jerry
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 27
Reputation: Terry Robinson is an unknown quantity at this point 
Solved Threads: 1
Terry Robinson Terry Robinson is offline Offline
Light Poster

Re: Centralized data in .NET?

 
0
  #5
Jun 29th, 2007
That is a little more encouraging. However, you didn't say if you got to the point that you could link data objects to your forms and reports at design time, and if you were able to create reusable classes or had to custom code each 'data module'. I'm sure that's all possible if I want to delve deeply enough into NET (though far more difficult since I don't have the code). For the moment, I will do what I need in code alone, I guess.

I am working with data for natural resources which involves lots of tables with lots of fields and lots of interrelations, so hand-coding everything is quite a chore. I'm finding that the amount of code required in C# is a LOT larger than Delphi. NET may well be more powerful than VCL, but I find it much harder to handle as well.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Centralized data in .NET?

 
0
  #6
Jun 29th, 2007
when in the C# Editor add a new class,

build that class as you need.

then you can simply reference that class in any other class/form as needed.


i'll write you an example write now!

cheers! :-)

EDIT:

Also if you have any questions regarding what you are trying to acheive i may be able to point you in a direction that will reduce the ammount of code you are using.

One huge advantage of C# is that it can use libraries from most any languages as long as they are built correctly. I find myself most commonly referencing C++ libraries to handle tasks that would otherwise require more code than normal.
Last edited by Killer_Typo; Jun 29th, 2007 at 3:15 pm.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Centralized data in .NET?

 
0
  #7
Jun 29th, 2007
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data.Odbc;
  5. namespace ConnectionSharing
  6. {
  7. class SqlConnectionManager : IDisposable
  8. {
  9. public SqlConnectionManager() {
  10. if (ConnectionSharing.Properties.Settings.Default.OdbcSqlConnection == null)
  11. {
  12. //you may place any initializer code in here
  13. //create our connection string
  14. /*
  15.   * Driver: The built in Driver to use, for
  16.   * non standard databases this must
  17.   * be installed and setup prior to calling
  18.   * Server: The location of the server, mine
  19.   * happens to be on the local host
  20.   * Database: Not specified but is recommended
  21.   * if you wish to use for specific reasons
  22.   * Uid: The user ID of the person connecting
  23.   * specified by the global variables
  24.   * Pwd: The password of the person connecting
  25.   * specified by the global variables
  26.   */
  27. string openSqlConnection =
  28. @"Driver=(SQL Server);Server=Localhost;uid=" + ConnectionSharing.Properties.Settings.Default.Username +
  29. ";Pwd=" + ConnectionSharing.Properties.Settings.Default.Password + ";";
  30. //please note that the @ sign on the preceeding line means to interpret the string literally
  31. this.x_sqlConnection = new OdbcConnection(openSqlConnection);
  32. }
  33. }
  34. //now any methods you create have access to the SQL connection created
  35. private OdbcConnection x_sqlConnection = null;
  36.  
  37. //always supply a dispose method when working with disposable objects
  38. public void Dispose() {
  39. //cleanup / close the connection
  40. this.x_sqlConnection.Close();
  41. this.x_sqlConnection.Dispose();
  42. }
  43. }
  44. }

you can now reference this class from any other form/class you create and not have to re-write your code for common tasks.

one option would be to create another application setting which contained the OdbcConnection object this way each time the class was called from a new form it would simply check to see if the object existed and had an open connection, the SqlConnectionManager would simply reuse the pre-existing OdbConnection even though it the class was called as a new instance.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Centralized data in .NET?

 
0
  #8
Jul 5th, 2007
Hey guys

Dont reinvent the wheel the guys at Microsoft have developed a central data access layer and other useful stuff. It is in their applications blocks in Patterns and Practices and there are many useful blocks in there to use. All of them are proven and very useful in enterprise applications

http://msdn2.microsoft.com/en-us/pra.../bb190359.aspx is where to find them all
Last edited by f1 fan; Jul 5th, 2007 at 5:09 pm. Reason: cant spell :(
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 57
Reputation: fishsqzr is an unknown quantity at this point 
Solved Threads: 1
fishsqzr fishsqzr is offline Offline
Junior Poster in Training

Re: Centralized data in .NET?

 
0
  #9
Oct 28th, 2007
I finally got back to looking at this problem. From what I see in the replies, I can build a class to be used in various parts of a program. While that is useful, I see no way to create a single instance of that class to be used throughout the program. I am looking a a database with nearly 100 different tables (and probably nearly that many forms to handle them). Yeah, I can use the class in every place I need it, which saves a lot of code, but each time will be a new instance. That is pretty weak, as I can't make use of common states and so on.

Is there any way to instantiate a class outside of a form for use throughout a program?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Centralized data in .NET?

 
0
  #10
Oct 28th, 2007
Create a public static class in your main namespace, and you will have access to it from all objects / forms that use that namespace. You can store state-full information in static vars and collections within that class.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC