编译器错误消息:CS1519:类,结构或接口成员声明中的标记'='无效

时间:2013-06-07 06:14:56

标签: c# asp.net database

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

            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>

2 个答案:

答案 0 :(得分: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"] ?? "";... }

答案 1 :(得分:3)

问题是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>