简体   繁体   中英

More efficient way to display big amount of data on c# from sql server?

Currently this is our method to display data table from sql server, the table stored in server have several columns including ProjectFile and Culture, we only want to display the data with user specified projectfile and culture:

private void showData(string[] selectedProject, string[] selectedCulure)
 {
        //the sql procedure will select data of specific culture and projectfile from table,it has two parameters:@culture and @projectfile
        SqlCommand com = new SqlCommand("PROCEDURE_GETData", m_conn);
        com.CommandType = CommandType.StoredProcedure;
        for (int i = 0; i < selectedProject.Length; i++)
        {
            com.Parameters.Add("@ProjectFile", SqlDbType.NVarChar).Value =   selectedProject[i];
            for (int m = 0; m < selectedCulure.Length; m++)
            {
                com.Parameters.Add("@Culture", SqlDbType.NVarChar).Value = selectedCulure[m];

                try
                {
                    m_conn.Open();
                    dataView.DataSource = com.ExecuteReader();
                    dataView.DataBind();
                    if (dataView.Rows.Count == 0)
                    {
                        dataView.EmptyDataText = "No data found, please select another project or culture. ";
                        dataView.DataBind();
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("Error Occur: " + ex.ToString());
                }
                finally
                {
                    m_conn.Close();
                    m_conn.Dispose();
                }
            }
        }
    }

The problem of this method is it is very slow, and it cannot successfully create table when user chooses multiple projectfile or culture, what's the better way to display data in this case?

The failure is happening for 2 (maybe 3) reasons:

  1. Don't use an instance member as the SQL connection. Wrap all your code in a

     using(SqlConnection conn = new SqlConnection(myConnectionString) 
  2. Reconstruct the SqlCommand inside the innermost loop every time.

  3. It looks like you're rebinding each dataset from multiple calls to the same dataview. I don't have much experience with those. I typically use reader["myField"] and construct local objects, so this may not be an issue.

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