首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Access查询转换为VB.net SQL语句

将Access查询转换为VB.net SQL语句
EN

Stack Overflow用户
提问于 2013-08-23 18:37:12
回答 1查看 2.1K关注 0票数 0

我的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只是空白。到目前为止,我的代码如下:

代码语言:javascript
复制
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

我真的被困住了,需要帮助!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-23 18:55:02

您希望VB.Net代码重新创建在Access中工作的相同的SELECT语句。但是,看看Vim的语法突出显示,我认为您可能实际上正在创建其他的东西。(这可能就像创建一个字符串,作为其他两个字符串的区别:"string 1" - "string 2")。

但是,不管我是否猜对了,使用字符串变量来保存您的SELECT语句。然后将该字符串打印到控制台或将其写入文本文件,以便您可以检查给db引擎的实际语句。

或者将Access中的工作查询保存为一个命名查询对象,并使用您的VB.Net代码中的查询名称--这将绝对保证使用相同的SQL,后者在Access中被确认可以工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18409654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档