从文本文件中读取记录,并希望将这些记录插入我的数据库中 [英] read records from a text file and wants to insert those records into my database

查看:71
本文介绍了从文本文件中读取记录,并希望将这些记录插入我的数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图从文本文件中读取记录,并希望在通过列验证示例后将这些记录插入我的数据库表中.客户姓名首字母必须是字符等.
请任何人为此提供示例代码.
我有以下文本文件.

H |客户
D | 101112 |客户姓名首字母|客户姓氏|地址1 |地址2 |地址3 | 27123456789 |有效
D | 101113 |客户姓名首字母|客户姓氏|地址1 |地址2 |地址3 | 27123456790 |有效
D | 101114 |客户姓名首字母|客户姓氏|地址1 |地址2 |地址3 | 27123456791 |有效
D | 101115 |客户姓名首字母|客户姓氏|地址1 |地址2 |地址3 | 27123456792 |有效
D | 101116 |客户姓名首字母|客户姓氏|地址1 |地址2 |地址3 | 27123456793 |有效
D | 101117 |客户姓名缩写|客户姓氏|地址1 |地址2 |地址3 | 27123456794 |有效
D | 101118 |客户姓名首字母|客户姓氏|地址1 |地址2 |地址3 | 27123456795 |有效
D | 101119 |客户姓名首字母|客户姓氏|地址1 |地址2 |地址3 | 27123456796 |有效

在此先感谢

Hi There,

I trying to read records from a text file and wants to insert those records into my database table after passing the column validations example customer Initials must be characters etc.
Please can any one provide me a sample code for this.
I have the following text file.

H|customer
D|101112|customer Initials|customer Surname|Address 1|Addres 2|Address 3|27123456789|Active
D|101113|customer Initials|customer Surname|Address 1|Addres 2|Address 3|27123456790|Active
D|101114|customer Initials|customer Surname|Address 1|Addres 2|Address 3|27123456791|Active
D|101115|customer Initials|customer Surname|Address 1|Addres 2|Address 3|27123456792|Active
D|101116|customer Initials|customer Surname|Address 1|Addres 2|Address 3|27123456793|Active
D|101117|customer Initials|customer Surname|Address 1|Addres 2|Address 3|27123456794|Active
D|101118|customer Initials|customer Surname|Address 1|Addres 2|Address 3|27123456795|Active
D|101119|customer Initials|customer Surname|Address 1|Addres 2|Address 3|27123456796|Active

Thanks in advance

推荐答案

不,我们不能!我们不知道您正在使用哪个数据库,拥有哪些表,表中的哪些字段等.

我们可以建议:
1)读取文件:
No, we can''t! We don''t know what database you are using, what tables you have, what fields in the tables, etc..

What we can do is suggest:
1) Read the file:
string[] lines = File.ReadAllLines(path);


2)遍历每行,并将其处理为组件:


2) Loop through each line, and proccess it into components:

foreach (string line in lines)
   {
   string[] components = line.Split(''|'');
   ...
   }


3)构建一个数据库连接和一个命令,并编写您的片段.假设SQL Server:


3) Construct a database connection, and a command, and write your fragments. Assuming SQL Server:

SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand com = new SqlCommand("INSERT INTO myTable (myField1, myField2) VALUES (@MF1, @MF2)", con);
com.Parameters.AddWithValue("@MF1", components[0]);
com.Parameters.AddWithValue("@MF2", components[1]);
com.ExecuteNonQuery();


其余的取决于您!


The rest is up to you!


请参阅 ^ ]


这篇关于从文本文件中读取记录,并希望将这些记录插入我的数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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