Thanks for your quick reply,
Reply: "you would want to store this in the ConnectionStrings section in app.config"
So when do i need to do this, when am establishing a connection between SQL and my windows application ?
when i make the changes to app.config file with new server details, then i need to make sure that all users have the newly updated configuration file in on their machine.
1. So in this case, is their any option for creating a patch.exe to be installed on the machine which will replace the old config file on users machine to newly updated config file. ?
2. And regarding the front end tool using windows application, my requirement is to have a 2 flavours of tool.
a) one installed to all the users (e.g: 50 machines) this will have only limited options to only enter data of their tasks.
b) another one is the Admin tool , with extra options and dialog boxes and buttons to pull daily or monthly data in form of excel from SQL server.
1. For the connection strings, take a look at Don't hard code your DataProviders[^], which explains how it should be done. I'm not sure how you deliver updates for an installed application, try searching for articles on the subject.
2. I don't see a problem, just create a separate tool for administrator use, or add some sort of password login to it.
will get back to you when am struck with something
I don't know if we'll be able to provide first aid if you're struck with a heavy object, but feel free to get back to us if you're stuck with something. We may be able to provide you a solution. Or at least a solvent.
i have my SQL Server Database on Machine1
I have installed my visual Studio on Machine2. I created windows forms on Machine1 and try to connect with Database on Machine1
View-->Server Explorer-->Add Connection-->Choose Microsoft SQL Server
In Add Connection window:
when i give Server name as
i could not find any database in the dropdown list. i get an error (Provider: SQL Network Interfaces, error:26 - Error Locating Server/Instance Specified)
But the Database is present on the Machine1.
(connecting to local database on same machine)
in dropdown list i get the datbase listed.
Issue: am not able to connect to database on other machines.
I'm a freelance programmer, and one of the major bugbears with working by myself is that I have no-one to bounce ideas off, so my code becomes very personal, if that's the right word, and I sometimes wonder if I'm doing things the right way. For instance, when I interrogate my database using Linq to Entities I use the following code (to return an instance of the purchase class).
internal static Purchase ReturnPurchaseItem(int PiD)
RegulusEntities RG = new RegulusEntities();
Purchase purchase = (from PU in RG.Purchases
where PU.PurchaseID == PiD
I would be very grateful if you could critique my code and maybe suggest more efficient ways of returning an instance of a class, or using the entity framework for that matter.
(If I've got this in the wrong forum, please forgive me, and transfer me to the right one!).
There simply isn't enough code here to make any real judgment of what you're doing. I can see some smallish things but nothing about any patterns you're using.
1) Your method is called "Return..." something. Personally, I always name methods that return something "Get...". Return is a bit more vague as to what it does. Does "Return" refer to retrieving a database object, or does it refer to some business rule method. It's the concept of "self documenting code". Method names should describe exactly what they do.
2) Entities (or DbContext) classes should be Disposed when you're done with them. It should be something like this:
internalstatic Purchase GetPurchaseItem(int purchaseId)
Purchase purchase = null;
using (var context = new RegulusEntities())
3) If you have a primary key Id for an object, you don't need an entire Linq query to find it. You just need to call the Find method to retrieve it. Find() will return the object with the specified key. If it's not found, it'll throw a InvalidOperationException. You can replace it with FirstOrDefault() if you want the "Get..." method to return a null of the specified type instead of throwing.
Other than that, a couple of rules I follow:
I don't have my database code return any ViewModel objects, not does it accept any. My database layer will accept and return Entity objects only. If these objects need to be translated into ViewModel objects, I can have another layer above this that does the translation, such as in an MVC Controller class or what I call a "Services" class.
I never expose my database layer directly to UI code.
Thanks for the reasoned and well thought out answer Dave. In showing you only a tiny patch of my code, I actually missed out the whole structure that this little method sits in.
This method, as with all my other calls to EF sits in a service class written to handle all the calls to a particular table. for example, all the calls to the purchase table are made from the clsPurchaseHandler class, thus separating it from the UI.
Your answer is exactly what I wanted. as I explained I am a freelance programmer running my own little operation, and I haven't got the luxury of being able to discuss code with anyone, and there's always the niggle at the back of my mind that 'I know it works, but is it the most efficient'. Your code detailing the disposal of entities after use and the find method is invaluable.
As for my naming of the method "ReturnPurchaseItem", that's my usual practise, as that's what the method does, I take your point that it may sound vague, but I've got a lot of code out in the wild that uses this notation, so for now, I'll stick with it.
Sorry to be a pest Dave, but the 'find' method you suggest isn't available to my version of EF. I've read up on it and it appears to come in after EF 4.0. I appear to be running EF 6.0, but I still can't see it. What am I doing wrong?