C# 在我的代码中,当foreach DataRow位于DataTable中时,如何获得高性能?

C# 在我的代码中,当foreach DataRow位于DataTable中时,如何获得高性能?,c#,asp.net,performance,datatable,C#,Asp.net,Performance,Datatable,我从Oracle数据库中获得一个数据表(ora_dt),现在我需要在ora_dt中添加一列(colu-sql),但是我必须从一些Sqlserver数据库中获得colu-sql的值 这是我的密码: public void ProcessDataTable(DataSet _ds) { _ds.Tables[0].Columns.Add(new DataColumn("Descpition", typeof(string))); int countryId = -1; s

我从Oracle数据库中获得一个数据表(ora_dt),现在我需要在ora_dt中添加一列(colu-sql),但是我必须从一些Sqlserver数据库中获得colu-sql的值

这是我的密码:


public void ProcessDataTable(DataSet _ds)
{
    _ds.Tables[0].Columns.Add(new DataColumn("Descpition", typeof(string)));

    int countryId = -1;
    string des_ID = string.Empty;
    string geo = string.Empty;

    foreach (DataRow row in _ds.Tables[0].Rows)
    {
        if (row["des_ID"] != DBNull.Value)
            des_ID = row["des_ID"].ToString();

        if (!string.IsNullOrEmpty(des_ID))
        {
            if (countryId == 12 || countryId == 13)
                geo = "NA";
            else if ((countryId == 10 || countryId == 11))
                geo = "LA";
            else
                geo = "EMEA";
            row["Descpition"] = GetDes(geo, des_ID);
        }
        else { row["ExemptionDes"] = string.Empty; }
    }
}     
对于每个DataRow,为了获得row[“Descpition”]值,我必须检查它的geodes_id,并从另一个SqlserverDB中选择它们

如果DataTable中的行数非常大,那么当我访问DataTable时,我必须多次访问sqlserver db,这会使性能变差

实际上,我无法在Oracle中添加新的列描述。在我的代码中,当在DataTable中使用foreach DataRow时,如何获得高性能


私有字符串GetDes(字符串geo、字符串des_ID)
{
string description=string.Empty;
string query=“从地理位置选择描述,其中des_ID=“+des_ID;
Database DbSQL=DbSQLFactory.CreateDatabase(geo);
使用(DataReader dr=DbSQL.ExecuteReader(sqlCmd))
{
while(dr.Read())
{
if(dr[“description”]!=null)
description=dr[“description”].ToString();
}
Close博士();
}
返回说明;
}

我的建议是一次性从
geo\u豁免
获取
描述
desu ID
的所有记录,然后使用LINQ根据des\u ID筛选出记录。这样,您只需点击数据库一次。Rest所有操作都将发生在asp.net端

编辑:

public void ProcessDataTable(DataSet _ds)
{
    if (Session["GeoExpAllRec"] == null)
    {
        //Fetch all records here and add it to a datatable i.e. "select des_ID, description from geo_exemption"
        //Then Add the datatable to the session variable Session["GeoExpAllRec"]
    }

    _ds.Tables[0].Columns.Add(new DataColumn("Descpition", typeof(string)));

    int countryId = -1;
    string des_ID = string.Empty;
    string geo = string.Empty;

    foreach (DataRow row in _ds.Tables[0].Rows)
    {
        if (row["des_ID"] != DBNull.Value)
            des_ID = row["des_ID"].ToString();

        if (!string.IsNullOrEmpty(des_ID))
        {
            if (countryId == 12 || countryId == 13)
                geo = "NA";
            else if ((countryId == 10 || countryId == 11))
                geo = "LA";
            else
                geo = "EMEA";

            //Instead of calling "GetDes" function which will hit the database
            //Type-cast the session variable Session["GeoExpAllRec"] to datatable i.e. (Session["GeoExpAllRec"] as DataTable)
            //Fire a LINQ query on the datatable to get the desired Description like below

            //row["Descpition"] = GetDes(geo, des_ID);

            DataTable dt = (Session["GeoExpAllRec"] as DataTable);
            row["Descpition"] = dt.AsEnumerable().Where(r => r.Field<string>("des_ID") == des_ID).First()["description"];

        }
        else { row["ExemptionDes"] = string.Empty; }
    }
}
public void ProcessDataTable(数据集)
{
如果(会话[“GeoExpAllRec”]==null)
{
//在此处获取所有记录并将其添加到数据表中,即“选择des_ID,来自geo_的描述”
//然后将datatable添加到会话变量session[“GeoExpAllRec”]
}
_表[0].Columns.Add(新数据列(“Descpition”,typeof(string)));
int countryId=-1;
string des_ID=string.Empty;
string geo=string.Empty;
foreach(表[0].行中的DataRow行)
{
if(行[“des_ID”]!=DBNull.Value)
des_ID=行[“des_ID”]。ToString();
如果(!string.IsNullOrEmpty(des_ID))
{
如果(countryId==12 | | countryId==13)
geo=“NA”;
else如果((countryId==10 | | countryId==11))
geo=“LA”;
其他的
geo=“欧洲、中东和非洲”;
//而不是调用会命中数据库的“GetDes”函数
//键入将会话变量session[“GeoExpAllRec”]强制转换为datatable,即(session[“GeoExpAllRec”]作为datatable)
//在datatable上触发LINQ查询以获得所需的描述,如下所示
//行[“Descpition”]=GetDes(地理位置,des_ID);
DataTable dt=(会话[“GeoExpAllRec”]作为DataTable);
行[“Descpition”]=dt.AsEnumerable()。其中(r=>r.Field(“des_ID”)==des_ID)。First()[“description”];
}
else{row[“ExemptionDes”]=string.Empty;}
}
}

希望这有帮助。

Thx,但是我不知道如何从geo_获得所有记录。这里不仅有一个sqlserver数据库。不同的地理位置导致不同的sql数据库。我必须在_ds.Tables[0]行中检查geo,并使用geo获取sql db。这个问题让我不得不选择上述糟糕的解决方案。仅仅从查询geo_豁免的查询中删除where子句不会获取所有记录吗?您可以在页面加载时或在ProcessDataTable函数的开始处触发此查询,并将其放入会话变量中,然后将此代码置于会话变量的空检查中。添加要访问数据库的位置,只需将会话变量强制转换为datatable并启动linq查询。我已经更新了我的答案以适应这一点。我认为,不仅desu ID还需要geo来获取记录。e、 首先,我需要获取我应该使用的sql数据库。例如:geo=“NA”,Database NA=DbSQLFactory.CreateDatabase(geo),然后从NA获取记录。实际上,服务器上有三个sql db具有相同的表结构,如NADB、EMEADB和LADB。如果我从这三个DBs中获得了所有数十万条记录,我认为这将带来更糟糕的性能。为什么不从每次yu点击行时不进行字符串查找开始呢。dr[“description”]比dr[0]慢得多,当您控制SQL时,您应该知道序号-或者您可以向datareader请求序号并将其保存在变量中。在许多情况下,这是一个很大的差异。
public void ProcessDataTable(DataSet _ds)
{
    if (Session["GeoExpAllRec"] == null)
    {
        //Fetch all records here and add it to a datatable i.e. "select des_ID, description from geo_exemption"
        //Then Add the datatable to the session variable Session["GeoExpAllRec"]
    }

    _ds.Tables[0].Columns.Add(new DataColumn("Descpition", typeof(string)));

    int countryId = -1;
    string des_ID = string.Empty;
    string geo = string.Empty;

    foreach (DataRow row in _ds.Tables[0].Rows)
    {
        if (row["des_ID"] != DBNull.Value)
            des_ID = row["des_ID"].ToString();

        if (!string.IsNullOrEmpty(des_ID))
        {
            if (countryId == 12 || countryId == 13)
                geo = "NA";
            else if ((countryId == 10 || countryId == 11))
                geo = "LA";
            else
                geo = "EMEA";

            //Instead of calling "GetDes" function which will hit the database
            //Type-cast the session variable Session["GeoExpAllRec"] to datatable i.e. (Session["GeoExpAllRec"] as DataTable)
            //Fire a LINQ query on the datatable to get the desired Description like below

            //row["Descpition"] = GetDes(geo, des_ID);

            DataTable dt = (Session["GeoExpAllRec"] as DataTable);
            row["Descpition"] = dt.AsEnumerable().Where(r => r.Field<string>("des_ID") == des_ID).First()["description"];

        }
        else { row["ExemptionDes"] = string.Empty; }
    }
}