c# - Best practice to use ADO.NET with design patterns? -
i have following
... public datatable getlist() { using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); // select table } } public void insert() { using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); // insert table } } ...
is there way define connection string global variable, , dispose after operations?
// connection open ... public datatable getlist() { // utilize global connection } public void insert() { // utilize global connection } ... // connection close
how can this? design pattern can use?
it's possible
public class myclass:idisposable { sqlconnection mycon; public myclass() { mycon = new sqlconnection(connectionstring); } public datatable getlist() { // utilize global connection } public void insert() { // utilize global connection } public void dispose() { mycon.close(); } }
using:
using (myclass myclass = new myclass()) { datatable dt = myclass.getlist(); }
c# ado.net
No comments:
Post a Comment