如何使用ASP.NET C#将数据从txt文件导入sqlserver? [英] How can I import data from txt file into sqlserver using ASP.NET C#?

查看:84
本文介绍了如何使用ASP.NET C#将数据从txt文件导入sqlserver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用asp.net c#webapplication从txt文件导入sql server中的数据,在txt文件列名中没有定义



这里是数据



31201805260758000100010002210001

31201805260758000100010003540001

31201805260759000100010001920001

31201805260805000100010000400001

31201805260808000100010002060001

31201805260808000100010001960001

31201805260809000100010001740001


说明将在SQL服务器中定义的字段不在Txt文件数据中。



31 20180526 0444 0002 000 2000091 0001



日期时间员工代码机器ID



我尝试了什么:



寻找编码,我试图从excel导入数据到SQL服务器现在寻找使用asp.net c#

i want to import data in sql server using asp.net c# webapplication from txt file,in txt file column name are not define

here is data

31201805260758000100010002210001
31201805260758000100010003540001
31201805260759000100010001920001
31201805260805000100010000400001
31201805260808000100010002060001
31201805260808000100010001960001
31201805260809000100010001740001

Explain Field which will define in SQL server not in Txt file data.

31 20180526 0444 0002 000 2000091 0001

Date Time OUt Employe Code Machine ID

What I have tried:

looking for coding ,i tried to import data from excel to sql server now looking for coding of txt import into sql server using asp.net c#

推荐答案

我们不为你做你的工作。

但这是我怎么做的:

1)将文件读入一个字符串数组:File.ReadAllLines将这样做。

2)设置一个带有相应列的DataTable

3)遍历每一行

3.1)使用string.Substring将行分成几部分。

3.2)使用int.TryParse和DateTime.TryParseExact将部件转换为正确的数据类型。

3.3)将正确值作为新行添加到DataTable。

4)使用DataAdapter将DataTable写入数据库。
We don't do your work for you.
But here's how I'd do it:
1) Read the file into an array of strings: File.ReadAllLines will do that.
2) Set up a DataTable with the appropriate columns
3) Loop through each line
3.1) Use string.Substring to break the line into parts.
3.2) Use int.TryParse and DateTime.TryParseExact to convert the parts to "proper" datatypes.
3.3) Add the "proper" values to the DataTable as a new row.
4) Use a DataAdapter to write the DataTable to the DB.


这篇关于如何使用ASP.NET C#将数据从txt文件导入sqlserver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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