I have some existing WCF code that accesses SQL Server 2005, but honestly I’ve come to mistrust that developer’s methods, so I want to know how this should be done correctly and professionally. I need to be able to pass an SQL statement to a method (within the WCF service, not from the client) that returns the resultant dataset (to the method in WCF that called it, not the client). I’m not interested in entity frameworks or other abstraction layers. I need to run SQL, DML, and hopefully DDL too.
I also want to know how to manage the connections.
Please point out your thoughts on better alternatives if you feel like it. I’m prepared to listen.
Always use
usingwhen dealing with sql connections.Using (sqlConn = new SqlConnection(ConnString)) { }Make your own POCOs to return the result to the client, you can either make them in seperate assembly and share it in both projects (WCF and client) or you can just add them to the WCF and when you create the proxy at the client side you will have access to them.
Here is a sample layout:
UPDATE
Here is an example of how to return a ‘DataSet` to the client, I do not recommend this but it will work: