943,769 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3427
  • C# RSS
Jun 27th, 2007
0

Centralized data in .NET?

Expand Post »
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.
Similar Threads
Reputation Points: 12
Solved Threads: 1
Light Poster
Terry Robinson is offline Offline
27 posts
since Apr 2007
Jun 28th, 2007
0

Re: Centralized data in .NET?

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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 28th, 2007
0

Re: Centralized data in .NET?

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.
Reputation Points: 12
Solved Threads: 1
Light Poster
Terry Robinson is offline Offline
27 posts
since Apr 2007
Jun 29th, 2007
0

Re: Centralized data in .NET?

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
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Jun 29th, 2007
0

Re: Centralized data in .NET?

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.
Reputation Points: 12
Solved Threads: 1
Light Poster
Terry Robinson is offline Offline
27 posts
since Apr 2007
Jun 29th, 2007
0

Re: Centralized data in .NET?

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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 29th, 2007
0

Re: Centralized data in .NET?

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jul 5th, 2007
0

Re: Centralized data in .NET?

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 :(
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Oct 28th, 2007
0

Re: Centralized data in .NET?

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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
fishsqzr is offline Offline
57 posts
since Aug 2007
Oct 28th, 2007
0

Re: Centralized data in .NET?

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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: small keylogger question
Next Thread in C# Forum Timeline: database connectivity





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC