c# - C#.net中的数据表

标签 c# .net database datatable

我有一个与 .net 中的数据表相关的问题。 我正在尝试在 page_load 上填充数据表。那部分没问题,数据表正确填充了数据。但我注意到,当我单击页面上的按钮时......它再次尝试填充数据表,这只是一项耗时的任务......我希望数据表应该只填充一次,当,当网站在浏览器中打开时......不是每次点击...因为我在表中有大量数据所以加载数据表需要时间.. 请帮帮我... 这是我的代码:

protectd void Page_Load(object sender, EventArgs e)
{
        cnn.ConnectionString= ConfigurationManager.ConnectionStrings["con"].ConnectionString;

      //  if (this.IsPostBack == true)
        {
            dr1 = TableUtoP(); 
            dtWordsList.Load(dr1);
       }
}

OleDbDataReader TableUtoPunj(String ArrWord)
    {
        if (cnn.State == ConnectionState.Open)
        {
            cnn.Close();
        }
        cnn.Open();
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "SELECT U, P from UtoP";
        cmd.Connection = cnn;
        OleDbDataReader dr = cmd.ExecuteReader();
        return dr;
    }

最佳答案

您需要检查页面是否正在回发,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    cnn.ConnectionString= ConfigurationManager.ConnectionStrings["con"].ConnectionString;

    if(!IsPostBack)
    {
        // This will only happen when the page is first loaded, as the first time is not considered a post back, but all others are
        dr1 = TableUtoP(); 
        dtWordsList.Load(dr1);
    }
}

关于c# - C#.net中的数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18155294/

相关文章:

c# - 将 Azure.AsyncPageable<BlobItem> 转换为 List<BlobItem>

c# - 如何使用 Azure Iot 获取设备的当前位置

.net - 如何知道哪个应用程序正在使用 GAC 中的程序集?

java - Oracle + dbunit 获取 AmbiguousTableNameException

sql - 如何在 PostgreSQL 中更改 View

c# - 关于对 Windows 窗体应用程序进行单元测试的建议

c# - 安装 CORS 后无法加载文件

c# - 在 C# 中重载 =operator 或在赋值前后触发事件的替代方法?

c# - DataGridView 列自动调整宽度和可调整大小

database - 甲骨文 express : Getting ORA-12154 error when trying to connect using SQL Plus