[英]Convert Access Query to VB.net SQL Statement
我的Access數據庫中有一個名為Historical_Stock_Prices
的表,該表充滿了各個公司的歷史股價。 我需要運行一個查詢,將原始數據(股票價格)轉換為季度增長率,並在DataGridView
顯示季度增長率。
我已經在Access數據庫的SQL視圖中編寫了以下查詢,該查詢在Access中有效。
SELECT MinMaxYrQtrDates.YrQtr, MinMaxYrQtrDates.Ticker, MinMaxYrQtrDates.MaxDate, [Historical Prices].Close, MinMaxYrQtrDates.MinDate, [Historical Prices_1].Open, ([Historical Prices].[Close]/[Historical Prices_1].[Open]-1)*100 AS GrowthRate FROM [Historical Prices] AS [Historical Prices_1] INNER JOIN ([Historical Prices] INNER JOIN [SELECT Year([Date]) & "-" & DatePart("q",[Date]) AS YrQtr, [Historical Prices].Ticker, Max([Historical Prices].Date) AS MaxDate, Min([Historical Prices].Date) AS MinDate FROM [Historical Prices] GROUP BY Year([Date]) & "-" & DatePart("q",[Date]), [Historical Prices].Ticker]. AS MinMaxYrQtrDates ON ([Historical Prices].Date = MinMaxYrQtrDates.MaxDate) AND ([Historical Prices].Ticker = MinMaxYrQtrDates.Ticker)) ON ([Historical Prices_1].Ticker = MinMaxYrQtrDates.Ticker) AND ([Historical Prices_1].Date = MinMaxYrQtrDates.MinDate);
我需要能夠從程序中調用它並將結果顯示在DataGridView
。 我試圖從Access復制SQL語句並將其用作代碼中的SQL語句,但是它不起作用。 我沒有任何錯誤, DataGridView
只是空白。 到目前為止,這是我的代碼:
Imports System.IO
Imports System.Data.OleDb
Public Class Historical_Growth_Rates_Annual
Public tblName As String = "Historical_Stock_Prices"
Private Sub Historical_Growth_Rates_Annual_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If (File.Exists(Nordeen_Investing_3.databaseName)) Then
Nordeen_Investing_3.con.Open()
Dim restrictions(3) As String
restrictions(2) = tblName
Dim dbTbl As DataTable = Nordeen_Investing_3.con.GetSchema("Tables", restrictions)
If dbTbl.Rows.Count = 0 Then
MessageBox.Show("Historical Stock Prices tables does not exist in the database. Please Update")
Else
Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT MinMaxYrQtrDates.YrQtr, MinMaxYrQtrDates.Ticker, MinMaxYrQtrDates.MaxDate, [Historical_Stock_Prices].Close1, MinMaxYrQtrDates.MinDate, [Historical_Stock_Prices_1].Open1, ([Historical_Stock_Prices].[Close1]/[Historical_Stock_Prices_1].[Open1]-1)*100 AS GrowthRate FROM [Historical_Stock_Prices] AS [Historical_Stock_Prices_1] INNER JOIN ([Historical_Stock_Prices] INNER JOIN [SELECT Year([Date1]) & " - " & DatePart('q',[Date1]) AS YrQtr, [Historical_Stock_Prices].Ticker, Max([Historical_Stock_Prices].Date) AS MaxDate, Min([Historical_Stock_Prices].Date) AS MinDate FROM [Historical_Stock_Prices] GROUP BY Year([Date1]) & " - " & DatePart('q',[Date1]), [Historical_Stock_Prices].Ticker]. AS MinMaxYrQtrDates ON ([Historical_Stock_Prices].Date = MinMaxYrQtrDates.MaxDate) AND ([Historical_Stock_Prices].Ticker = MinMaxYrQtrDates.Ticker)) ON ([Historical_Stock_Prices_1].Ticker = MinMaxYrQtrDates.Ticker) AND ([Historical_Stock_Prices_1].Date = MinMaxYrQtrDates.MinDate);", Nordeen_Investing_3.con)
'create a new dataset
Dim ds As New DataSet()
'fill the datset
da.Fill(ds)
'attach dataset to the datagrid
DataGridView1.DataSource = ds.Tables(0)
ds = Nothing
da = Nothing
Nordeen_Investing_3.con.Close()
End If
Else
MessageBox.Show("Database does not exist. Please update.")
End If
End Sub
End Class
我真的很困,可以尋求幫助! 謝謝!
您希望VB.Net代碼重新創建可在Access中使用的相同SELECT
語句。 但是,看看Vim突出顯示的語法,我認為您可能實際上正在創建其他東西。 (這可能就像創建一個字符串來區別其他兩個字符串: "string 1" - "string 2"
)。
但是,無論我是否猜對了,都使用字符串變量來保存您的SELECT
語句。 然后將該字符串打印到控制台或將其寫入文本文件,以便您可以檢查要提供給db引擎的實際語句。
或將工作查詢保存為Access中的命名查詢對象,並使用您的VB.Net代碼中的查詢名稱---絕對可以保證使用與確認在Access中工作相同的SQL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.