简体   繁体   English

Winforms C#.Add() 到 sql 数据库非常慢

[英]Winforms C# .Add() to sql database is VERY slow

I have a Windows Forms Application written in C#.我有一个用 C# 编写的 Windows Forms 应用程序。 One form is a data entry form, with a save button.一种形式是数据输入表,带有保存按钮。

Upon Clicking the Save Button, the code first converts all textboxes to Caps.单击保存按钮后,代码首先将所有文本框转换为大写字母。 Then it declares all the variables for the SQL table fields - to the identifying text boxes然后它将 SQL 表字段的所有变量声明到识别文本框

ex (there are several of these types of binds) ex(有几种类型的绑定)

var vlast = textBox_V_lname.Text;

All of this works super fast, as expected (I have used break points to find the hold up).正如预期的那样,所有这些都超级快(我已经使用断点来找到延迟)。 And here, everything hangs.在这里,一切都挂起。 It hangs for about 30 seconds (that's a long long time, when you only have 900 records in the table).它挂起大约 30 秒(这是很长的时间,当表中只有 900 条记录时)。

var badger_History = new Badger_History() { v_noaccess_flag = myacess, v_lastname = vlast, v_firstname = vfirst, v_child = vchild, v_company = vcompany, v_issue_date = vdate, v_location = mylocation, comments = mycomments, h_lastname = hlast, h_firstname = hfirst, h_middle_init = hmiddle, h_title = htitle, h_phone = hphone, h_org = horg, h_work_location = hlocation, pass_type = type, pending_flag = pending };
            oBadger_History.Add(badger_History);

It hangs right here: oBadger_History.Add(badger_History);它挂在这里:oBadger_History.Add(badger_History);

badger_History being the model for the table insert. badger_History 是表插入的 model。 I can post the model itself, but that looks pretty normal.我可以发布 model 本身,但这看起来很正常。

I have certainly tried manually adding a record with SQL Manager, and that worked instantly, as expected.我当然尝试过使用 SQL Manager 手动添加记录,并且按预期立即工作。

I can't figure out what is causing the hang.我无法弄清楚是什么导致了挂起。 When I hover over the 'oBadger_History.Add' and go to Definition, all I see is this line当我 hover 超过 'oBadger_History.Add' 和 go 到定义时,我看到的只是这条线

public virtual void Add(EntityType entity);

Note: this is an application another staff member developed and has since retired.注意:这是另一位工作人员开发的应用程序,现已退休。 I usually don't work with Winforms applications and so - I am in over my head here.我通常不使用 Winforms 应用程序,所以 - 我在这里不知所措。

Any insight or nudge in the right direction is appreciated.任何朝着正确方向的洞察力或推动都将受到赞赏。

I can add as much more code as needed - just wasn't sure if it was necessary to post more我可以根据需要添加更多代码 - 只是不确定是否有必要发布更多

Many thanks to all those who responded.非常感谢所有回复的人。 It did give me some insight on what to look harder at.它确实让我对更难看的东西有了一些了解。

Also - my apologies for taking so long to follow up.另外 - 我很抱歉花了这么长时间来跟进。 I am being handed failing apps left and right, to fix.我被交给左右失败的应用程序来修复。 And a different one had a high priority, but I finally got back to this one.另一个有很高的优先级,但我终于回到了这个。

After looking at the responses and using that information to start looking at samples of other works online.在查看回复并使用该信息开始查看其他在线作品的样本后。 . . This is what actually ended up working for me:这实际上最终为我工作:

public partial class frmBadgeCreate : daBaseForm
{

    private boBadger_History oBadger_History;
  public frmBadgeCreate()
    {
        if (daAppDesktop.IsRunning)
        {
            oBadger_History = new boBadger_History();
            oBadger_History.AutoGenerateContextIndex = true;
            oBadger_History.ReleaseContextonClose = true;
          
            oBadger_History.GetAll();

         }
            InitializeComponent();
         }

And then, the save (below) works perfect (about 2 seconds tops).然后,保存(下)完美(大约 2 秒)。

 var badger_History = new Badger_History() { v_noaccess_flag = myacess, v_lastname = vlast, v_firstname = vfirst, v_child = vchild, v_company = vcompany, v_issue_date = vdate, v_location = mylocation, comments = mycomments, h_lastname = hlast, h_firstname = hfirst, h_middle_init = hmiddle, h_title = htitle, h_phone = hphone, h_org = horg, h_work_location = hlocation, pass_type = type, pending_flag = pending, child_out_flag = childOutflag };
           
            oBadger_History.Add(badger_History);
  
            oBadger_History.Save();

Thank you to all those who offered some advice and assistance.感谢所有提供建议和帮助的人。 It really did help me find my way here.它确实帮助我找到了这里的路。 I am not a winforms developer and totally out of my lane here - so thank you all so much!我不是winforms开发人员,完全不在我的行列 - 所以非常感谢你们!

Happy New Year Stackoverflow !新年快乐!

Instead of using the Add method, use the AddRange method.不要使用Add方法,而是使用AddRange方法。 The latter is internally calculated based on dichotomy, and its performance is n * log (n), which is better than the former n * n.后者内部基于二分法计算,其性能为n*log(n),优于前者n*n。

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

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