0

我有以下包含股票代码的数组:Public Shared tickerArray() As String = {"GOOG", "AAPL", "V", "MSFT"}. 我需要使用这个循环:For Each tickerValue In Form1.tickerArray将每个股票代码的 csv 文件加载到 Microsoft Access 中的一个大表中。csv 文件位于"http://ichart.yahoo.com/table.csv?s=" & tickerValue. 我还需要将相应的股票代码加载到从 csv 文件导入的表格的每一行中,以使每一行都是唯一的。所以数据库中的列应该是:“Ticker, Date, Open, High, Low, Close, Volumne & Adj Close”。

我找到了有关将本地 csv 文件加载到 Access 中的信息,但我似乎无法弄清楚如何通过 vb.net 将 csv 文件从 Internet 加载到 Access 中。

另外,我需要经常更新这个表,所以我只需要从 csv 文件中插入新的唯一行。没有重复。

有什么建议么?谢谢!

更新: 这是我到目前为止的代码。

Imports System.Data
Imports System.Data.OleDb
Imports System.Net
Imports System.IO

Public Class Form1

Public Shared tickerArray() As String = {"GOOG", "AAPL", "V", "MSFT"}

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    Dim myConnection As OleDbConnection
    Dim DBpath As String = 
    Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBpath & ";Persist Security Info=True"
    myConnection = New OleDbConnection(sConnectionString)
    myConnection.Open()

    Dim strURL As String
    Dim strBuffer As String
    For Each tickerValue In Form1.tickerArray
        'Creates the request URL for Yahoo.
        strURL = "http://ichart.yahoo.com/table.csv?s=" & tickerValue
        strBuffer = RequestWebData(strURL)
        'Create array.
        Dim sReader As New StringReader(strBuffer)
        Dim List As New List(Of String)
        Do While sReader.Peek >= 0
            List.Add(sReader.ReadLine)
        Loop
        Dim lines As String() = List.ToArray
        sReader.Close()
        For Each Line In lines.Skip(1)
            MsgBox(Line)
            Dim myInsert As String = TextToInsert(Line, tickerValue)
            Dim cmd As New OleDbCommand(myInsert, myConnection)
            cmd.ExecuteNonQuery()
        Next
    Next
End Sub

Function TextToInsert(ByVal inputString As String, ByVal ticker As String)
    Dim Dt As String = inputString.Split(",")(0).Trim
    Dim Open As String = inputString.Split(",")(1).Trim
    Dim High As String = inputString.Split(",")(1).Trim
    Dim Low As String = inputString.Split(",")(1).Trim
    Dim Close As String = inputString.Split(",")(1).Trim
    Dim Volume As String = inputString.Split(",")(1).Trim
    Dim Adj_Close As String = inputString.Split(",")(1).Trim
    Dim SQLstr As String
    SQLstr = "INSERT INTO Historical (Ticker, Date, Open, High, Low, Close, Volume, Adj Close) " & "VALUES (" & "'" & ticker & "','" & Dt & "','" & Open & "','" & High & "'," & "'" & Low & "','" & Close & "','" & Volume & "','" & Adj_Close & "'" & ")"
    Return SQLstr
End Function

Private Function RequestWebData(ByVal pstrURL As String) As String
    Dim objWReq As WebRequest
    Dim objWResp As WebResponse
    Dim strBuffer As String
    'Contact the website
    objWReq = HttpWebRequest.Create(pstrURL)
    objWResp = objWReq.GetResponse()
    'Read the answer from the Web site and store it into a stream
    Dim objSR As StreamReader
    objSR = New StreamReader(objWResp.GetResponseStream)
    strBuffer = objSR.ReadToEnd
    objSR.Close()

    objWResp.Close()

    Return strBuffer
End Function

End Class

我收到错误代码“OleDBException 未处理。INSERT INTO 语句中的语法错误。” 在线cmd.ExecuteNonQuery()请帮忙!

4

3 回答 3

2

您可以使用 System.Data.OleDb 命名空间来定义函数以插入数据库。像(以非常粗略的方式):

Dim myConnection As OleDbConnection

Dim sConnectionString  As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBpath & ";Persist Security Info=True"

myConnection = New OleDbConnection(sConnectionString)

myConnection.Open()

然后在csv中的每一行上做一个循环

Dim myInsert as String= functionTextToInsert(inputString)

Dim cmd As New OleDbCommand(myInsert, myConnection)

cmd.ExecuteNonQuery()

functionTextToInsert(ByVal inputString as string) 是一个函数,它在插入字符串中从 csv 转换一行:"max;min;vol" -> "Insert into MYTABLE (Column_01,Column_02,Column_03) VALUES (max,min,vol );"

于 2013-03-01T22:15:13.993 回答
0

尝试这个:

在 .NET/C# 中将大量记录(批量插入)写入 Access

我从未与 DAO 合作过,所以在这里我帮不上什么忙,但是...

如果此 MSSQL 语法适用于 MS ACCESS,请使用命令对象并将包含您的 csv 作为 varchar(max) 或类似访问数据类型的参数传递给它,这将来自您的应用程序的 CSV 内容:

BULK
INSERT mytable
FROM @myCSVstring
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

有两件事,SO 帖子指出 DAO 快得多,而且你的终结者可能不同,但我会从这些开始。

于 2013-03-01T17:13:52.080 回答
0

我认为以前的答案是不必要的复杂。您只需发送XMLHTTP请求即可。这会将 CSV 数据作为字符串获取,然后可以将其解析为表。

下面是执行此操作的 VBA 子例程。我在哪里找到这些技术的文档在代码注释中。我已经在 Access 2019 中运行了它,它可以工作。

可以在将数据插入表之前或之后对问题中指示的数据进行修改。

Sub pImportOnlineCsvAsTable(sURL As String, sTableName As String, Optional sKey As String)

' Download a CSV file from the Internet and parse it into an Access table (all strings).

' Adapted from "www.stackoverflow.com/questions/52142757/vba-download-csv-from-a-link/#52175815"
'   This shows how to download an online CSV file into an ADO stream, then save it as a local file.
' and "www.stackoverflow.com/questions/33860833/convert-adodb-binary-stream-to-string-vba/#33883789"
'   This shows I don't need the stream, but can use the CSV data as a string and parse it into a table.

' Documentation on "XMLHTTP" object:
'   "https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms760305(v=vs.85)"

Dim oWinHttpReq As Object
Dim oDB As Database
Dim sSQL As String, sCSVdata As String, _
    asCSVrows As Variant, asCSVfields As Variant, _
    nCSVrows As Long, nCSVfields As Integer, _
    nCountRows As Long, nCountFields As Integer

Set oDB = CurrentDb()
Set oWinHttpReq = CreateObject("Microsoft.XMLHTTP")
oWinHttpReq.Open "get", sURL, False
oWinHttpReq.send
If oWinHttpReq.status = 200 Then
    sCSVdata = oWinHttpReq.ResponseText
    asCSVrows = Split(sCSVdata, Chr(10))
    nCSVrows = UBound(asCSVrows) + 1
    asCSVfields = Split(asCSVrows(0), ",")
    nCSVfields = UBound(asCSVfields) + 1
    sSQL = ""
    For nCountFields = 0 To (nCSVfields - 1)
        sSQL = sSQL & "[" & asCSVfields(nCountFields) & "] varchar(255)" & _
                    IIf(nCountFields = nCSVfields - 1, "", ", ")
      Next
    sSQL = "create table [" & sTableName & "] (" & sSQL & ");"
    oDB.Execute (sSQL)
    For nCountRows = 1 To (nCSVrows - 1)
        asCSVfields = Split(asCSVrows(nCountRows), ",")
        sSQL = ""
        For nCountFields = 0 To (nCSVfields - 1)
            sSQL = sSQL & """" & asCSVfields(nCountFields) & """" & _
                        IIf(nCountFields = nCSVfields - 1, "", ", ")
          Next
        sSQL = "insert into [" & sTableName & "] values (" & sSQL & ");"
        oDB.Execute (sSQL)
      Next
    If sKey <> "" Then _
        oDB.Execute "alter table [" & sTableName & "] add primary key ([" & sKey & "]);"
  End If

Set oWinHttpReq = Nothing
Set oDB = Nothing

End Sub     ' pImportOnlineCsvAsTable()
于 2021-04-26T07:17:28.327 回答