简体   繁体   中英

first time running application slow from dll

I'm new to C#.

What I want to ask is I create a form and compile it into dll and try to call from it from another application (from company application).

What I want to ask is, when I try to open my form for the first time it takes a while (like 1-2 minute) and then I close the form ( not application ) and re-try to open the form again for the second time its much more faster than the first time.

but if I close the application completely and re-open my form for the first time it takes a while (like 1-2 minute).

for the dll itself, are selecting database. The code as below

public partial class Genre : Form
{
    SqlConnection myConnection = new SqlConnection(class.Conn);
    DataTable dt_main = new DataTable();

    Bitmap gbr_inf = new Bitmap(Properties.Resources.info_icon, 25, 25);
    Bitmap gbr_error = new Bitmap(Properties.Resources.close, 25, 25);

    RepositoryItemComboBox repositoryItemComboBox1 = new RepositoryItemComboBox();
    RepositoryItemComboBox repositoryItemComboBox2 = new RepositoryItemComboBox();


    public Genre()
    {            
        InitializeComponent();
        repositoryItemComboBox1.ButtonClick += RepositoryItemComboBox1_ButtonClick;
        repositoryItemComboBox2.ButtonClick += RepositoryItemComboBox2_ButtonClick;
    }

 private void Genre_Load(object sender, EventArgs e)
    {
        SDB();         
        fill_repo();
    }

 public void SDB()
    {
        SqlCommand command = new SqlCommand();
        SqlDataAdapter adapter = new SqlDataAdapter();
        try
        {
            dt_main.Clear();
            myConnection.Open();

            command.Connection = myConnection;
            command.CommandText = "Select * from Genre with (nolock) order by code";                
            adapter.SelectCommand = command;
            adapter.Fill(dt_main);
            gridControl1.DataSource = dt_main;               
        }
        catch (Exception ex)
        {
            MessageBox.Show("error" + ex);
        }
        finally
        {
            myConnection.Close();
        }
    }

  public void fill_repo()
    {
        DataTable dtrepo = new DataTable();
        dtrepo.Clear();
        dtrepo = dt_main.Copy();

        for (int i = 0; i < dtrepo.Rows.Count; i++)
        {                                
            string code = dtrepo.Rows[i]["code"].ToString();
            string genre = dtrepo.Rows[i]["genre"].ToString();

            if (!repositoryItemComboBox1.Items.Contains(code))
            {
                repositoryItemComboBox1.Items.Add(code);
            }
            if (!repositoryItemComboBox2.Items.Contains(genre))
            {
                repositoryItemComboBox2.Items.Add(genre);
            }

        }
    }

 private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
    {           
        if (e.Column.FieldName == "code" && view.IsFilterRow(e.RowHandle))
        {
            e.RepositoryItem = repositoryItemComboBox1;
        }
        if (e.Column.FieldName == "genre" && view.IsFilterRow(e.RowHandle))
        {
            e.RepositoryItem = repositoryItemComboBox2;
        }          
    }
}

what seems to be the problem ?

When you call SDB :

public void SDB()
{
    SqlCommand command = new SqlCommand();
    SqlDataAdapter adapter = new SqlDataAdapter();
    try
    {
        dt_main.Clear();
        myConnection.Open();

        command.Connection = myConnection;
        command.CommandText = "Select * from Genre with (nolock) order by code";                
        adapter.SelectCommand = command;
        adapter.Fill(dt_main);
        gridControl1.DataSource = dt_main;               
    }
    catch (Exception ex)
    {
        MessageBox.Show("error" + ex);
    }
    finally
    {
        myConnection.Close();
    }
}

You are copying all fields and all rows from the Genre table and putting them into the dt_main DataTable. This could be an expensive operation depending on how big the Genre table is.

If you want your application to perform well, you should generally avoid very large queries by selecting only the fields and rows you are interested in working with at any given time. In this case, you are only using the code and genre fields, so you could get much better performance by using

"Select code, genre with (nolock) order by code"

In addition, you are copying the entire table again just before you populate the dropdowns:

dtrepo = dt_main.Copy();

This is totally unnecessary.

Caching the data is also important so you don't hit the database on every form load. This you are doing (accidentally) but you really should have a better plan for doing so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM