3

我一直遇到以下编译错误:

            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>
4

2 回答 2

8

此错误是由于您的 MSBuild 版本造成的,旧版本的 MSBuild 只能编译 C# 版本 4,而您的代码以 C# 版本 6 格式编写。

使用 C# 版本 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"] ?? "";... }
于 2017-02-08T00:00:33.970 回答
6

问题是 ASP.NET 页面中的内联代码(与经典 ASP 不同)是在类的范围内编译的,而不是在函数的范围内(或松散的 ASP 脚本),因此您需要用方法包围代码宣言。ASP.NET 提供并自动连接一个Page_Load函数并在页面生命周期中调用它。

<script runat="server" language="C#">
public void Page_Load(object sender, EventArgs e)
{
    // put your existing code here
}
</script>
于 2013-06-07T06:39:13.780 回答