如何将记事本数据导入数据库 [英] How to import notepad data to database

查看:306
本文介绍了如何将记事本数据导入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

在C#.net Windows应用程序中,如何将明智地将数据从.txt文件复制到SQL Server数据库中,该数据库具有表并在网格视图中检索日期.请帮帮我.

在记事本文件中包含如下所示的字段

Dear All,

In C#.net windows application, How to copy the data filed wise from .txt file to SQL server data base, the database having a table and retrieve the date into grid view. Please help me.

In notepad file contain fields like in below

Packet ID : 48236W2	 	ID : 47565
Row No : 1 			Name : sudhakar
Depth : 6680.00 		Total Measured Depth :


谢谢&问候
T.sudhakara rao


Thanks & Regards
T.sudhakara rao

推荐答案

[ ^ ]可能会有所帮助.
This[^] might help.


List<string> lines = System.IO.File.ReadAllLines(@"yourpathtoTextfilet").ToList();
            string[] stringSeparators = new string[] {"\t"};
            string[] stringSeparatorsSub = new string[] { ":" };
            var result = (from t in lines
                         select t.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)).ToList();

            foreach (string[] s in result)
            {
                foreach (string sin in s)
                {
                    if (sin.IndexOf(":") > 0)
                    {
                     string[] pair =   sin.Split(stringSeparatorsSub, StringSplitOptions.None);
                     string s1 = pair[0].Trim();
                     string s2 = pair[1].Trim();

                     //write s1 and s2 to DB or store it


                    }
                }
            }




首先,如果可能的话,我更喜欢使用 CSV 文件.如果您没有使用文本文件的限制,请使用CSV.您可以轻松地从中读取信息.

而且,如果只想从上面的文本文件中读取文本,则需要使用C#的某些 String Manipulation函数.

您需要检查文本文件中的特定模式.在您的示例中说,您可以看到数据库列名称和值之间存在冒号(:).您可以使用拆分功能来拆分值.您可以一次读取一行,然后使用冒号和空格将其拆分.您将在字符串Array中获得列名"和值"替代.

然后,您可以将该数组传递给函数以进行下一阶段.下一步将是在与您的列名匹配之后在数据库中插入记录.您可以使用switch语句来匹配列名.

希望以上信息对您有帮助,

谢谢
-Amit Gajjar
Hi,

First of all i prefer to use CSV file if possible. If you do not have constraint to use Text file then go for the CSV. it would be easy for you to read information from it.

And In case you want to read your text from above text file only then you need to use some String Manipulation functions of C#.

You need to check particular pattern in the text file. say in your example you can see there is colon(:) between database column name and value. you can use Split function and split the value. You can read one line at a time and then split the line using colon and space. You will get Column name and value alternative in your string Array.

Then your can pass that array to the function for next stage. the next stage would be to insert record in database after matching your column name. you can use switch statement to match column name.

Hope above information helps you,

Thanks
-Amit Gajjar


这篇关于如何将记事本数据导入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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