在将值插入数据库时​​更新进度条值 [英] Updating Progress bar value while values are inserted into database

查看:88
本文介绍了在将值插入数据库时​​更新进度条值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个c#中的应用程序,用于从文本文件中提取数据。将这些值插入数据库。要插入的数据量太大(> 100000)&当数据被插入时,应用程序显示没有响应...



我的要求

1)我如何避免没有回复消息

2)我想在数据插入数据库时​​更新进度条,以显示进度



这里是我用来存储到数据库中的代码

i am developing an application in c# that extracts data from a text file & inserts those values into the database. the amount of data to be inserted is way too huge(>100000) & while the data is being inserted, the application shows "Not responding"...

my requirements
1) How do i avoid the "Not Responding" Message
2) i want to update the progress bar as the data is inserted into the database, to show the progress

here is the code that i am using to store into the db

string str;
            List<data> gd = new List<data>();
            try
            {
                using (StreamReader sr = new StreamReader("C:\\Windows\\Temp\\op.txt"))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] arr = line.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);
                        while (sr.Peek() > 0)
                        {

                            string[] s = new string[] { " " };
                            char[] c = new char[] { ' ' };
                            str = sr.ReadLine();
                            arr = str.Split(c);

                            data d = new data();
                            d.date = arr[0];
                            d.time = arr[1];
                            d.lat = Convert.ToDouble(arr[3]);
                            d.lon = Convert.ToDouble(arr[4]);
                            if (arr[6] == "")
                            {
                                d.depth = arr[7];
                            }
                            else
                            {
                                d.depth = arr[6];
                            }
                            if (arr[8] == "")
                            {
                                d.mag = arr[9];
                            }
                            else
                            {
                                d.mag = arr[8];
                            }
                            //MessageBox.Show(d.depth);
                            OleDbConnection cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "/earthDB.mdb;Persist Security Info=True");
                            OleDbCommand cmd = new OleDbCommand();
                            cn.Open();
            cmd.Connection = cn;
           
            cmd.CommandText = "insert into Nass values(@DateTime,@Latitude,@Longitude,@Depth,@Magnitude)";

            cmd.Parameters.AddWithValue("@DateTime",d.date +" "+d.time);
            cmd.Parameters.AddWithValue("@Latitude",d.lat);
            cmd.Parameters.AddWithValue("@Longitude",d.lon);
            cmd.Parameters.AddWithValue("@Depth",d.depth);
            cmd.Parameters.AddWithValue("@Magnitude",d.mag);

            try
            {
                cmd.ExecuteNonQuery();
                cn.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                cn.Close();
            }
            finally
            {
                cn.Close();
            }



plzz帮助..


plzz help..

推荐答案

首先,请阅读我的评论关于这个问题。



正如我在过去的回答中所提到的:使用C#将数组元素存储到Access数据库中 [ ^ ] - 而是将文本文件加载到数组中,直接读取此文件使用JET 4.0进入Access数据库。 OLEDB驱动程序。相信我,这是一个很好的建议。这是最简单的,最简单的......



如果要显示进度条以向用户显示操作正在进行中,您需要阅读有关线程的信息。我建议你使用推荐的方法(OLEDB)和不确定进度条来显示用于显示操作正在进行的动画。

线程教程 [ ^ ]

线程化(C#编程指南) [ ^ ]



更多关于:

进度条 [ ^ ]

进度条,线程,Windows窗体和你 [ ^ ]

使用C#进度条 [ ^ ]

显示用户使用进度条插入记录的进度 [ ^ ]
First of all, please, read my comment to the question.

As i had mentioned in my past answer: Storing Array Elements into Access Database in C#[^] - Instead loading text file into array, read this file direct into Access database using JET 4.0. OLEDB drivers. Belive me, it's good advice. It's simplest, it's easiest and...

If you want to show progressbar to show the user that operation is in progress, you need to read about threading. I would suggest you to use recommended method (OLEDB) and Indeterminate progress bars to display animation which is used to show that an operation is ongoing.
Threading Tutorial[^]
Threading (C# Programming Guide)[^]

More about:
Progress Bars[^]
Progress Bars, Threads, Windows Forms, and You[^]
Progress Bar using C#[^]
Show User Progress Of Inserting Records By Using Progressbar[^]


这篇关于在将值插入数据库时​​更新进度条值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆