使用后我是否必须将DataTable设置为null?

时间:2016-08-22 14:29:06

标签: c# sql

我是C#的新手,我使用的是Windows窗体。

我的C#项目中有大约50个user controls,每个user control从本地SQL服务器读取数据。 SQL查询位于user control's构造函数中,如以下代码所示:

 public partial class BurgersUC : UserControl
{
    SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
    SqlCommand MyCommand = new SqlCommand();
    DataTable DataTable = new DataTable();
    SqlDataAdapter Sql_Data_Adapter = new SqlDataAdapter();



 public BurgersUC()
     {

        InitializeComponent();

        try
        {


            DataTable.Rows.Clear();
            DataTable.Columns.Clear();

            MyConnection.Open();
            MyCommand.CommandText = "SELECT * FROM BurgerTable";
            MyCommand.Connection = MyConnection;
            Sql_Data_Adapter.SelectCommand = MyCommand;
            Sql_Data_Adapter.Fill(DataTable);

             button1.Text = Convert.ToString(DataTable.Rows[0]["Burger_Type"]);

             .

             .

             .


             button30.Text = Convert.ToString(DataTable.Rows[29]["Burger_Type"]);


           // DataTable = null;  

            MyCommand.Parameters.Clear();
            Sql_Data_Adapter.Dispose();
            MyConnection.Close();



        }

        catch (System.Exception excep)
        {

            MessageBox.Show(excep.Message);

        }


  }


}

我的问题是:

在保存一些内存方面我是否必须在使用后将DataTable设置为NULL?谢谢

0 个答案:

没有答案