Furkan Gözükara
Furkan Gözükara

Reputation: 23850

Performance way of working on SQL Server data tables at C# code behind

Alright now the question may come to you vague so I will try to explain as much as possible.

For example I have 10 columns and 5000 rows in a data table called tblNpc.

So I will make a lot of filtering on this table during the work. I am using this approach here, but is that the best way of handling it?

Here a small example

    string srQuery = "select * from tblNpc";
    DataSet dsNPC = DbConnection.db_Select_Query(srQuery);

    DataView dvNPC = new DataView(dsNPC.Tables[0]);

    dvNPC.RowFilter = "npcId=32";
    string srExampleData = dvNPC[0]["npcName"].ToString();

C# 4.0, SQL Server 2008 R2

Important: I will use entire table and will do filtering equal to entire table row count

Upvotes: 0

Views: 261

Answers (1)

Kaf
Kaf

Reputation: 33849

As per @Andormar's comments, it would be quicker if you hand over the filtering job to the SQL Server, which is designed to do the job, in the first place and bring over only what you needed.

If you need to work with the DataTable in the code behind, then LINQ would be quicker I think. On top of that you could do complex queries with your DataTable with LINQ rather than RowFilter.

For example your query can be done as

var results = (from r in dsNPC.Tables[0].AsEnumerable()
               where r.Field<int>("npcId")) ==32
               select new { npcName = r.Field<string>("npcName")})
              .FirstOrDefault();

Upvotes: 2

Related Questions