Declaring SqlConnection throughout one form

后端 未结 4 1656
感情败类
感情败类 2020-12-22 02:25

I\'m looking for some advise on C# - Please bare in mind that i\'m only a beginner with C# & Sql.

I am looking to design a small program that will Add/Edit/Dele

4条回答
  •  青春惊慌失措
    2020-12-22 02:55

    Of course, you dont need to have multiple SqlConnection instances. for most application, one connection is sufficient. sqlCommand on the other hand is another thing: You might step into trouble if reusing it, but this is more a design issue than a technical thing.

    I would suggest you move all your database access code to another class ("DataProvider"/"DatabaseController" or whatever name you think is sufficient) to properly encapsulate the code parts. There is no need for a form to directly handle SqlConnection or sqlCommand objects, let it indirectly happen via public methods of that controller type:

    public class DataController
    {
        private SQLConnection Connection {get; set;
        public void DataTable LoadDataFromDatabase()
        {
            ...
        }
        ...
     }
    

提交回复
热议问题