使用SQL数据库处理文本文件-Visual Basic [英] Text File Handling with SQL Database - Visual Basic

查看:88
本文介绍了使用SQL数据库处理文本文件-Visual Basic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含分隔记录的文本文件.

I have a text file contains delimited records.

1243;jhhf';982u4k;9u2349;huf8
kij;9238u;98ur23;jfwf;03i24

我需要将每个记录的第4部分的值替换为从SQL数据库(Select X from T where C='4Th part from the flatfile')返回的值.

I need to replace the value of 4th part of every record with the value returned from SQL database (Select X from T where C='4Th part from the flatfile').

关于,
桑索什.

Regards,
SAnthosh.

推荐答案

尝试一下:

Dim newLines As List(Of String) = New List(Of String)
Dim sqlConn As New SqlConnection(connectionString)
Dim SQLCmd As New SqlCommand()
SQLCmd.Connection = sqlConn
Dim lines As String() = File.ReadAllLines(filename)
sqlConn.Open()
For Each line As String In lines
    Dim parts As String() = line.Split(";")
    SQLCmd.CommandText = "Select X from T where C=""" & parts(3) & """"
    Dim dr As SqlDataReader = SQLCmd.ExecuteReader
    While dr.Read()
        parts(3) = dr("X")
    End While
    newLines.Add(String.Join(";", parts))
Next
File.WriteAllLines(filename, newLines.ToArray())
sqlConn.Close()

这篇关于使用SQL数据库处理文本文件-Visual Basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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