【问题标题】:C# using an Access View instead of a custom queryC# 使用访问视图而不是自定义查询
【发布时间】:2017-06-09 09:34:00
【问题描述】:

前言:关于 SO 有很多与“使用 SQL 视图的 C#”相关的问题,但我还没有找到任何接近我的问题的问题。

我正在使用 C# (Visual Studio 2015) 创建 Excel 2010 插件。在后端,我需要使用 Access 2010。我在 Access 中创建了一个视图,需要传递一个参数。

我不想在command.CommandText 中输入整个查询,而是只使用视图并绑定它需要运行的单个参数

任何帮助将不胜感激

private void getJobSetpoints(int xID, int xYear)
    {
        //string yearTable = "tbl_points_table_" + xYear.ToString();            
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;

            command.CommandText = "qry_setpoints";  //**This is wrong, I just don't know how to identify the VIEW.
            command.Parameters.Add("@ID", OleDbType.Integer).Value = xID;

            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                //Do stuff
            }
        }
        finally
        {
            connection.Close();
        }
    }

【问题讨论】:

  • command.CommandText = "select * from qry_setpoints"?

标签: c# sql oledb sql-view


【解决方案1】:

使用像表格一样的视图。

command.CommandText = "select qry_setpoints where ID = @ID";  
command.Parameters.Add("@ID", OleDbType.Integer).Value = xID;    

【讨论】:

  • 对 - 只有当它是一个存储过程而不是一个视图查询时,OP 代码才可以(除了它们会将 CommandType 设置为 adCmdStoredProc)。
  • 谢谢大家。这么简单的东西我就是看不到:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多