【发布时间】:2021-03-17 03:07:23
【问题描述】:
我对 C# 相当陌生,所以我怀疑我遗漏了一些明显的东西,但我有一个看起来非常简单的案例,我在 C# 中定义了一个数据表并将一列添加到数据表的 DataColumnCollection。我在智能感知中收到一条错误消息:
名称
mytable在当前上下文中不存在。
我还收到如下所示的编译器错误,我将其解释为试图理解无法识别的 mytable 的结果。
1>\Controllers\OutputControllerdemo.cs(14,28,14,29):错误 CS1519: 类、记录、结构或接口成员中的无效标记 '(' 声明
1>\Controllers\OutputControllerdemo.cs(14,36,14,37):错误 CS8124: 元组必须至少包含两个元素。
1> \Controllers\OutputControllerdemo.cs(14,37,14,38):错误 CS1519: 令牌无效 ';'在类、记录、结构或接口成员中 声明
欣赏您可能拥有的任何见解
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data;
namespace VPopWeb.Controllers
{
public class OutputControllerdemo : Controller
{
DataTable mytable = new DataTable("Results");
DataColumn column1 = new DataColumn("Date", System.Type.GetType("DateTime"));
mytable.Columns.Add(column1); //Error Here: The name mytable does not exist in the current context.
public ViewResult Chart() => View("Chart");
}
}
【问题讨论】:
-
该代码(特别是添加列的第三行)需要在方法中。所有过程代码都必须有一个方法作为它的主目录。
标签: c# asp.net-mvc