static class for database access?
-
This is a quick question. Is it possible to do ADO.NET access from a static class? I'm guessing that it's not, because if I were to create a static class (say DBController) and then attempt to use it to access my database (eg with the method DBController.getNewClients) then the method (getNewClients) would have to create non-static instances of objects (ie the connection object). From Java I know that if you try to reference non-static methods from static methods you get an error. Does this hold true for C# as well? Or would I be able to create a static DBController class that I could then use on any of my forms to access my database? If that's not possible, then is it possible to pass a reference to my DBController to every form that's going to need access to it? How would I do that? These may be dumb questions, but I'm the only tech guy in my company and my degree is in mathematics. Any help would be EXTREMELY appreciated. It's lonely, oh so lonely, being the only tech guy in my company.
-
This is a quick question. Is it possible to do ADO.NET access from a static class? I'm guessing that it's not, because if I were to create a static class (say DBController) and then attempt to use it to access my database (eg with the method DBController.getNewClients) then the method (getNewClients) would have to create non-static instances of objects (ie the connection object). From Java I know that if you try to reference non-static methods from static methods you get an error. Does this hold true for C# as well? Or would I be able to create a static DBController class that I could then use on any of my forms to access my database? If that's not possible, then is it possible to pass a reference to my DBController to every form that's going to need access to it? How would I do that? These may be dumb questions, but I'm the only tech guy in my company and my degree is in mathematics. Any help would be EXTREMELY appreciated. It's lonely, oh so lonely, being the only tech guy in my company.
-
This is a quick question. Is it possible to do ADO.NET access from a static class? I'm guessing that it's not, because if I were to create a static class (say DBController) and then attempt to use it to access my database (eg with the method DBController.getNewClients) then the method (getNewClients) would have to create non-static instances of objects (ie the connection object). From Java I know that if you try to reference non-static methods from static methods you get an error. Does this hold true for C# as well? Or would I be able to create a static DBController class that I could then use on any of my forms to access my database? If that's not possible, then is it possible to pass a reference to my DBController to every form that's going to need access to it? How would I do that? These may be dumb questions, but I'm the only tech guy in my company and my degree is in mathematics. Any help would be EXTREMELY appreciated. It's lonely, oh so lonely, being the only tech guy in my company.
theStorminMormon wrote: if I were to create a static class (say DBController) and then attempt to use it to access my database (eg with the method DBController.getNewClients) then the method (getNewClients) would have to create non-static instances of objects (ie the connection object). You can create instances of objects in static methods. The isntance with belong to the class and not to any instance. Like this:
class DBController { static DBController() { conn = new SqlConnection(....); } public static Customer GetCustomer(int id) { conn.Open(); SqlCommand cmd = new SqlCommand("...", conn); SqlDataReader rdr = cmd.ExecuteReader(); rdr.Read(); Customer customer = new Customer() customer.Name = (string) rdr["Name"]; rdr.Close(); conn.Close(); return customer; } private static SqlConnection conn; }
While is not precisely the prettiest code, it shows two things: A class can have static objects (in this case a connection that belongs to the class itslef, and not to objects of this class); and a static method can create and return instances of this or any other class. I hope this helps! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
Yes, it's possible. As the connection has to be local to every thread that uses it, you would have to declare the reference to it as ThreadStatic. --- b { font-weight: normal; }
Thanks for the response. I don't know exatly what a thread is. Could you give me a code snippet (just by example) so I can see what you're talking about? Or just a more specific explanation? One is the loneliest number...
-
theStorminMormon wrote: if I were to create a static class (say DBController) and then attempt to use it to access my database (eg with the method DBController.getNewClients) then the method (getNewClients) would have to create non-static instances of objects (ie the connection object). You can create instances of objects in static methods. The isntance with belong to the class and not to any instance. Like this:
class DBController { static DBController() { conn = new SqlConnection(....); } public static Customer GetCustomer(int id) { conn.Open(); SqlCommand cmd = new SqlCommand("...", conn); SqlDataReader rdr = cmd.ExecuteReader(); rdr.Read(); Customer customer = new Customer() customer.Name = (string) rdr["Name"]; rdr.Close(); conn.Close(); return customer; } private static SqlConnection conn; }
While is not precisely the prettiest code, it shows two things: A class can have static objects (in this case a connection that belongs to the class itslef, and not to objects of this class); and a static method can create and return instances of this or any other class. I hope this helps! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
Thanks! That really does help! I think I can make my plan work from here. Is this the method you would recommend if I want to be able to access my database methods from various forms? Or would you pass by reference an instantiated DBController object? Or something else? Thanks for the help! The ends can never justify the means. It is the means that determine the ends.
-
Thanks! That really does help! I think I can make my plan work from here. Is this the method you would recommend if I want to be able to access my database methods from various forms? Or would you pass by reference an instantiated DBController object? Or something else? Thanks for the help! The ends can never justify the means. It is the means that determine the ends.
Yes, I would recommend a class with static methods over an instantiated object. It dependes on how complex is your application, because sometimes I've written specific classes for each table in the database. Other times just the simple static methods. -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
Thanks for the response. I don't know exatly what a thread is. Could you give me a code snippet (just by example) so I can see what you're talking about? Or just a more specific explanation? One is the loneliest number...
A thread is like a line of execution of code. You can have several threads running simultaneously in your process, for example one to manage the UI and respond to events, while other is processing data. Multithreading is not that easy, since you can have problems when two threads try to access the same object at the same time. Then you have thread synchronization, so that while one thread is accessing certain object, other threads wait on it. But then if you have thread A holding object 1 and waiting for object 2, and thread B is holding object 2 and waiting for object 1, you have a deadlock. Debugging a deadlock can be very difficult, but using multiple threads can improve the performance of your application. Search google for multithreading programming, I'm sure there are several articles on it. -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
Thanks for the response. I don't know exatly what a thread is. Could you give me a code snippet (just by example) so I can see what you're talking about? Or just a more specific explanation? One is the loneliest number...
If you use the class in asp.net, or from more than one program running at the same time, there will be more than one thread using the same class. If you have a static reference in the class that is not thread static, it will be shared by every thread that uses the class. That means that when one program connects to a database, and then another program connects to a second database, both programs will be using the second database. Also when one program closes the connection, the other program will stop working. --- b { font-weight: normal; }