0

我有两个 Microsoft Access 数据库表。它们被命名为Historical_Stock_PricesBalance_Sheets。我需要组合这些表中的每一个表中的数据来创建一个名为Daily. 我需要取字段Ticker,[Date][Close]from Historical_Stock_Prices,我需要取字段Common_Stocksfrom Balance_Sheets

我不会从Historical_Stock_Pricesand中取出每一行Balance_Sheets。我只会采用在DateTimePicker命名中选择的日期上或之前的行dtpDateSelection

现在我遇到的主要问题是Historical_Stock_Prices每天都包含一行。虽然Balance_Sheets每个季度包含一行。因此,对于四分之一中的每一天,来自的数字Balance_Sheets将是相同的。

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

Dim Date1 As Date = dtpDateSelection.Value

Try
Dim cmd As OleDbCommand = New OleDbCommand("CREATE PROC Daily AS SELECT [H].[Ticker], [H].[Date], [H].[Close], [B].[Common_Stocks] FROM [Historical_Stock_Prices] AS [H] INNER JOIN [Balance_Sheets] AS [B] ON Int(Year([H].[Date])) = Int([B].[Year]) AND Int(Format([H].[Date],'Q')) = Int([B].[Period]) AND CStr([H].[Ticker]) = CStr([B].[Ticker]) WHERE CDate([H].[Date]) = #" & CDate(Date1) & "#", con)
cmd.ExecuteNonQuery()
Catch ex As Exception
Finally
Dim cmda As OleDbCommand = New OleDbCommand("DROP PROCEDURE Daily", con)
cmda.ExecuteNonQuery()
Dim cmdb As OleDbCommand = New OleDbCommand("CREATE PROC Daily AS SELECT [H].[Ticker], [H].[Date], [H].[Close], [B].[Common_Stocks] FROM [Historical_Stock_Prices] AS [H] INNER JOIN [Balance_Sheets] AS [B] ON Int(Year([H].[Date])) = Int([B].[Year]) AND Int(Format([H].[Date],'Q')) = Int([B].[Period]) AND CStr([H].[Ticker]) = CStr([B].[Ticker]) WHERE CDate([H].[Date]) = #" & CDate(Date1) & "#", con)
cmdb.ExecuteNonQuery()
End Try

此代码在该表中创建表和Daily字段Ticker[Date]和。但是代码不会将任何数据加载到表的行中。有什么想法为什么不呢?[Close]Common_Stocks

4

3 回答 3

1

我不确定您的代码是否符合您的要求。我没有看到CREATE TABLE创建所需表的INSERT INTO命令或将记录插入表的命令。

于 2013-10-03T18:34:44.687 回答
1

看起来您正在创建一个存储过程,实际上并没有将INSERT任何数据放入Daily您提到的表中。

您可能希望将第一部分更改CREATE PROC Daily ASINSERT INTO dbo.Daily. 这样,数据将附加到表中。

于 2013-10-03T18:35:35.373 回答
1

您的代码尝试创建视图的等效项。它本身不返回任何数据。您可以从 SELECT 语句返回数据。不确定语法是否正确,但您可以直接使用 MS-Access 尝试 cmdText 并验证结果

Dim cmdText = "SELECT [H].[Ticker], [H].[Date], [H].[Close], [B].[Common_Stocks] " & _
              "FROM [Historical_Stock_Prices] AS [H] INNER JOIN [Balance_Sheets] AS [B] " & _
              "ON Year([H].[Date]) = [B].[Year] AND " & _
              "Format([H].[Date],'Q') = [B].[Period] AND " & _
              "[H].[Ticker] = [B].[Ticker] "
              "WHERE [H].[Date] = ?"
Using cmdb = New OleDbCommand(cmdText, con)
    cmdb.Parameters.AddWithValue("@1", CDate(Date1))
    Using da = new OleDbDataAdapter(cmdb)
        Dim dt = new DataTable("Daily")
        da.Fill(dt)
        ' now you have an IN MEMORY DataTable named Daiyly filled with the data '
        ' returned by the SELECT query '
    End Using
End Using
于 2013-10-03T18:35:35.553 回答