简体   繁体   English

管理表中记录的最佳方法

[英]The best way to manage records in a table

I am sorry that they ask this question has been asked many times but I still have not yet found the best answer. 对不起,他们问了这个问题很多次,但我仍然没有找到最佳答案。

I am worried applications take a long time to download the record or filter the records . 我担心应用程序需要很长时间才能下载记录过滤记录 Assuming I have a table called tbl_customer . 假设我有一个名为tbl_customer的表。 And records in tbl_customer more than 10,000 rows . 并在tbl_customer中记录了10,000多行

The first question , I am using Data Grid View to display the records . 第一个问题 ,我正在使用数据网格视图显示记录 Would be ideal if I download all the records up to 10,000 rows into the Data Grid View? 如果我将多达10,000行的所有记录下载到数据网格视图中,将是理想的选择吗? Or perhaps I had better put the record row limit? 还是我最好将记录行数限制为?

Second question , what is the best way to filter records in tbl_customer . 第二个问题 ,什么是在tbl_customer中 过滤记录的最佳方法 Do we just need to query using SQL? 我们是否只需要使用SQL查询? or using LINQ? 使用LINQ? or maybe there is a better way? 还是有更好的方法?

For now, I only use this way: 现在,我仅使用这种方式:

DataTable dtCustomer = new DataTable();

 using (SqlConnection conn = new SqlConnection(cs.connString))
        {
            string query = "SELECT customerName,customerAddress FROM tbl_customer WHERE customerAddress = '"+addressValue+"' ORDER BY customerName ASC;";

            using (SqlDataAdapter adap = new SqlDataAdapter(query, conn))
            {
                adap.Fill(dtCustomer);

            }
        }

dgvListCustomer.DataSource = dtCustomer

Then I learn about LINQ so i do like this 然后我了解了LINQ,所以我喜欢这样

    DataTable dtCustomer = new DataTable();

         using (SqlConnection conn = new SqlConnection(cs.connString))
                {
                    string query = "SELECT * FROM tbl_customer ORDER BY customerName ASC;";

                    using (SqlDataAdapter adap = new SqlDataAdapter(query, conn))
                    {
                        adap.Fill(dtCustomer);

                    }
                }

var resultCustomer = from row in dtCustomer.AsEnumerable()
                                         where row.Field<string>("customerAddress") == addressValue
                                         select new
                                         {
                                             customerName = row["customerName"].ToString(),
                                             customerAddress = row2["customerAddress"].ToString(),

                                         };

 dgvListCustomer.DataSource = resultCustomer;

Workflow SQL> DATATABLE> LINQ > DataGridView is suitable to filter records? 工作流SQL> DATATABLE> LINQ> DataGridView是否适合过滤记录? Or if there are better suggestions are most welcome. 或者,如果有更好的建议,则欢迎您。

Thanks you..:) 谢谢..:)

I am worried applications take a long time to download the record or filter the records. 我担心应用程序需要很长时间才能下载记录或过滤记录。

Welcome - you seem to live in a world like me where performance ms measured in milliseconds, and yes, on a low power server it will take likely more than a millisecond (0.001 seconds) to hot load and filter 10.000 rows. 欢迎使用-您似乎生活在像我这样的世界中,性能毫秒数以毫秒为单位,是的,在低功耗服务器上,热加载和过滤10.000行可能要花费超过一毫秒(0.001秒)的时间。

As such, my advice is not to put that database on a tablet or mobile phone but to use at least a decent desktop level compute r or VM for the database server. 因此,我的建议是不要将该数据库放在平板电脑或移动电话上,而应至少为数据库服务器使用一个不错的桌面级计算机或VM。

As a hint: I am regularly making queries on a billion row table and it is fast. 提示:我经常在十亿行表上进行查询,而且速度很快。 Anything below a million rows is a joke these days - in fact it was nothing worth mentioning when I started with databases more than 15 years ago. 如今,低于一百万行的内容是一个笑话-实际上,当我15年前开始使用数据库时,这没什么值得一提的。 You are the guy asking whether it is better to have a ferrari or a porsche becauese you are concerned whether any of those case goes more than 20km/h. 您是在询问是否有法拉利或保时捷beau beauese更好的人,您担心这些情况是否会超过每小时20公里。

Would be ideal if I download all the records up to 10,000 rows into the Data Grid View? 如果我将多达10,000行的所有记录下载到数据网格视图中,将是理想的选择吗?

In order to get fired? 为了被解雇? Yes. 是。 Old rule with databases: never load more data than you have to, especially when you have no clue. 数据库的旧规则:永远不要加载比您更多的数据,尤其是当您没有任何线索时。 Forget the SQL side - you will get UI problems with 10.000 rows and more, especially usability issues. 忘记SQL方面-您将遇到10.000行及更多行的UI问题,尤其是可用性问题。

Do we just need to query using SQL? 我们是否只需要使用SQL查询? or using LINQ? 或使用LINQ?

Hint: Linq is also using SQL under the hood. 提示:Linq也在后台使用SQL。 The question is more - how much time do you want to spend writing boring repetitive code for handwritten SQL like in your examples? 问题是更多的-您想花多少时间为示例编写无聊的,重复的手写SQL代码? Espeically given that you also do "smart" things like referencing fields by name, not ordinal, and asking for "select *" instead of a field list, bot obvious beginner mistakes. 通常,假设您还执行“智能”操作,例如按名称而不是按序引用字段,并要求“选择*”而不是字段列表,则bot显然是初学者的错误。

What you should definitely not do - but you do - is using a DataTable. 您绝对不应该(但是您可以)使用DataTable。 Get a decent book about programming databases. 获得有关编程数据库的不错的书。 RTFSM may help - both LINQ (which I am not sure what you mean - LINQ is a language for the compiler, you need an implementor, so that could be NHibernate, Entity Framework, Linq2Sql, BlToolkit, to name just a FEW tha t go from a LINQ query to a sql statement). RTFSM可能会有所帮助-两者都是LINQ(我不确定您的意思是什么-LINQ是用于编译器的语言,您需要一个实现程序,因此可以是NHibernate,Entity Framework,Linq2Sql,BlToolkit,仅此而已从LINQ查询到sql语句)。

Workflow SQL> DATATABLE> LINQ > DataGridView is suitable to filter records? 工作流SQL> DATATABLE> LINQ> DataGridView是否适合过滤记录?

A Ferrari is also suitable to transport 20 tons of coal from A to B - just the worst possible car for it. 法拉利还适合将20吨煤从A运到B-可能是最糟糕的汽车。 GSour stack is likely the worst I have seen, but it is suuitable in that you CAN do it - slow, lots f mmemoory use, but you will get a result and hopefully fired. GSour堆栈可能是我所见过的最糟糕的情况,但可以做到这一点,这是可以接受的-速度很慢,可以大量使用内存,但是您将获得结果并有望被解雇。 You pull the data from a high performance database into a data table, then use a non integrating technology (LINQ) to filter (not using the indices in the data table) to go into yet another layer. 您将数据从高性能数据库中提取到数据表中,然后使用非集成技术(LINQ)进行过滤(不使用数据表中的索引)以进入另一层。

Just to give you an idea - this would get you removed from quite some "beginning programming" courses. 只是给您一个想法-这会让您从很多“入门编程”课程中脱身。

What about: 关于什么:

LINQ LINQ

Point. 点。

Pulls a collection of business objects that go to the UI. 提取要转到UI的业务对象的集合。 Period. 期。

Read at least some of the sample code for the technologies you use. 阅读有关您所使用技术的至少一些示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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