我一直遇到以下编译错误:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1519: Invalid token '=' in class, struct, or interface member declaration
Source Error:
Line 22:
Line 23: //Assign a Connection String
Line 24: conn.ConnectionString = ConfigurationManager.ConnectionStrings["sauceatronConnString"].ConnectionString;
Line 25:
Line 26: //Connection Open
Source Line: 24
只是想用一般的编程新知识以及 ASP 和 C# 作为序言。我之前使用相同的代码连接到数据库并且它工作正常但现在我遇到了那个我不太熟悉如何解决的错误。下面是我的 aspx 页面和我的 web.config 的代码。
<%@Page Language="C#" MasterPageFile="MasterPage/AtronsSiteMaster.master"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.Common"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Collections.Generic"%>
<asp:Content ContentPlaceHolderID="titleContentPlaceHolder" ID="titleContent" runat="server">Products</asp:Content>
<asp:Content ContentPlaceHolderID="headContentPlaceHolder" ID="headContent" runat="server"></asp:Content>
<script runat="server" language="C#">
String provider = ConfigurationManager.ConnectionStrings["sauceatronConnString"].ProviderName;
DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
//Open a Connection
DbConnection conn = factory.CreateConnection();
//Assign a Connection String
conn.ConnectionString = ConfigurationManager.ConnectionStrings["sauceatronConnString"].ConnectionString;
//Connection Open
conn.Open();
//Initialize a Command
DbCommand comm = conn.CreateCommand();
//Tell the command which connection it will use
comm.Connection = conn;
//Give the command SQL to execute
comm.CommandText = "Select ProductName,ProductIssue,Writer,UnitPrice from Products order by ProductName, ProductIssue";
//Execute the command and get back the results via a reader
DbDataReader reader = comm.ExecuteReader();
//While we get results from the DB, add a row to the Table
while (reader.Read())
{
TableRow row = new TableRow();
TableCell cell;
cell = new TableCell();
cell.Text = reader["ProductName"].ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = reader["ProductIssue"].ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = reader["Writer"].ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = reader["UnitPrice"].ToString();
row.Cells.Add(cell);
}
//Free up the connection
conn.Close();
</script>
<asp:Content ContentPlaceHolderID="pageTitleContentPlaceHolder" ID="pageTitleContent" runat="server">Products</asp:Content>
<asp:Content ContentPlaceHolderID="mainContentPlaceHolder" ID="mainContent" runat="server">
<asp:Table ID="tblData" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell>Comic Book Name</asp:TableHeaderCell>
<asp:TableHeaderCell>Issue</asp:TableHeaderCell>
<asp:TableHeaderCell>Writer Name</asp:TableHeaderCell>
<asp:TableHeaderCell>Price</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</asp:Content>
<configuration>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true"/>
</system.web>
<connectionStrings>
<add name="databaseConnString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~\Database\database.accdb;Persist Security Info=False;" providerName="System.Data.OleDb"/>
<add name="studentConnString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~\Database\students.mdb;Persist Security Info=False;" providerName="System.Data.OleDb"/>
<add name="sauceatronConnString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~\finals\Database\SauceAtronsVault.accdb;Persist Security Info=False;" providerName="System.Data.OleDb"/>
</connectionStrings>
</configuration>
最佳答案
此错误是由于您的 MSBuild 版本问题而发生的,旧版本的 MSBuild 只能编译 C# 版本 4,而您的代码以 C# 版本 6 格式编写。
C# version 6 代码编写示例:
public static string HostName { get; set; } = ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";
要让 MSBuild 编译您的代码,您需要使用 C# 4 风格编写
public static string HostName { get; set; }
public SomeConstructor()
{
Host = ConfigurationManager.AppSettings["RabbitMQHostName"] ?? "";... }
关于c# - 编译器错误消息 : CS1519: Invalid token '=' in class, 结构或接口(interface)成员声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16977451/