从CSV或.txt文件填充数据集 [英] Populate Dataset from CSV or .txt files

查看:157
本文介绍了从CSV或.txt文件填充数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类型化数据集"
其列和数据类型为:

字段名称数据类型
primary_key int
emp_name varchar
emp_id varchar
部门varchar
位置varchar
日期date
time_in datetime
break_out datetime
break_in datetime
time_out datetime

并且以某种方式,我想使用.txt fil
中的这种记录来填充此数据集

I have this "Typed Dataset"
where its columns and data types are:

Field Name Data Type
primary_key int
emp_name varchar
emp_id varchar
department varchar
position varchar
date date
time_in datetime
break_out datetime
break_in datetime
time_out datetime

and in some way i would like to populate this dataset using this kind of records from a .txt fil

37,PETER L PARKER,1,LABORATORY,STAFF,5/1/2012,5/1/2012 08:00:15,5/1/2012 12:00:34,5/1/2012 13:30:55,5/1/2012 17:30:05
38,PETER L PARKER,1,LABORATORY,STAFF,5/2/2012,5/2/2012 08:20:24,5/2/2012 12:00:37,5/2/2012 13:01:21,5/2/2012 17:00:48
39,PETER L PARKER,1,LABORATORY,STAFF,5/3/2012,5/3/2012 08:30:01,,,5/3/2012 17:30:13




但是在我尝试期间仍然无法正常运行

我实际上尝试了这些代码@ http://social. msdn.microsoft.com/Forums/pl-PL/adodotnetdataproviders/thread/952ab4ca-97d4-4cc8-9db8-d5d562161461 [ 这意味着我实际上没有得到我想要实现的目标.
老实说,我在vb.net上的表现不是很好,但是我渴望学习,所以也许有人可以再次帮助我




however during my tries it still doesn''t work

I actually tried those codes @ http://social.msdn.microsoft.com/Forums/pl-PL/adodotnetdataproviders/thread/952ab4ca-97d4-4cc8-9db8-d5d562161461[^]
But still no success at all.

hope someone could share some knowledge and wisdom

Copied from "solution"
It means that i actually do not get what i want to achieve.
Honestly i am not that good in vb.net but i am eager to learn so maybe someone could help me again

推荐答案

不知道这是否是你需要的,但是读取基本文本文件是循环中的练习.这是我在编程生涯初期就开始使用的一些简单代码,很简单,但并不可靠.

这是代码的主要部分,向其发送一个数据表,您希望将其放入文本文件信息以及要读取的文件名.它将把每个用分号分隔的字符串放入数据表的列中. 重要提示:文件中的每个段发送的数据表必须至少具有1列.
Not sure if this is what you need but to read a basic text file is an exercise in looping. This is some simple code I started using early in my coding career, it is simple but not robust.

This is the main bit of code, you send it a datatable that you want the text file information put into and the name of the file you want to read. It will place each semicolon separated string into a column of the datatable. Important: the datatable sent must have at least 1 column for each segment in the file.
Private Sub ReadFile(sTable As DataTable, sFileName As String)
        Dim line As String

        ' Read the file line by line.
        Dim file As New System.IO.StreamReader(Application.StartupPath + "\" + sFileName)
        line = file.ReadLine()
        While (line) IsNot Nothing
            Dim tmpline As String = line
            Dim tmpStrings As String()

            Dim tmpRow As DataRow = sTable.NewRow()

            Dim i As Integer = 0
            While tmpline.Length > 0
                tmpStrings = NextLine(tmpline)
                tmpline = tmpStrings(1)
                tmpRow(i) = tmpStrings(0)
                i += 1
            End While

            sTable.Rows.Add(tmpRow)
            line = file.ReadLine()
        End While

        file.Close()
    End Sub



这是NextLine函数:



And here is the NextLine Function:

Private Function NextLine(line As String) As String()
	Dim tmpString As String() = New String(1) {}

	tmpString(0) = line.Substring(0, line.IndexOf(";"))
	tmpString(1) = line.Substring(line.IndexOf(";") + 1, line.Length -     (line.IndexOf(";") + 1))

	Return tmpString
End Function


这篇关于从CSV或.txt文件填充数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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