C# 错误消息:需要类型或命名空间定义,或文件结尾

C# 错误消息:需要类型或命名空间定义,或文件结尾,c#,c#-4.0,C#,C# 4.0,我在互联网上搜索过,以前的答案都说缺少分号。这不是问题所在。还有什么会导致此错误?在第三个站点中,有几个人问了这个问题,但情况不同 错误为>类型或命名空间定义,或预期文件结尾 public partial class SeenSMS : System.Web.UI.UserControl { //temp sql connection //public SqlConnection mycon; SqlConnection mycon = new SqlConne

我在互联网上搜索过,以前的答案都说缺少分号。这不是问题所在。还有什么会导致此错误?在第三个站点中,有几个人问了这个问题,但情况不同

错误为>类型或命名空间定义,或预期文件结尾

 public partial class SeenSMS : System.Web.UI.UserControl
    {
    //temp sql connection
    //public SqlConnection mycon;
    SqlConnection mycon = new SqlConnection(@"Data Source=ASOFT20\MAMUT;Initial   Catalog=ViltraNew;UserID=sa;Password=sa123");


    protected void Page_Load(object sender, EventArgs e)
    {
        string[] msg_arr = Request.QueryString["arr"].Split('|');

        if (!IsPostBack)
        {
            string Moose = Request.QueryString[1];
        }

        if (msg_arr != null)
        {
            if ((msg_arr.Length == 3) && (msg_arr[1].ToLower() == "slett"))

            {
                int Hours = Convert.ToInt32(msg_arr[2]);

                if (Hours > 0)
                {   
                    string username = msg_arr[0];

                    SqlCommand com = new SqlCommand("SELECT count(*) as count  FROM Animal Where Hours=@Hours", mycon);
                    com.Parameters.AddWithValue("@Hours",Hours);
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        if(reader.HasRows)
                        {
                        while (reader.Read())
                        {
                             // int number = Convert.ToInt32(con.ExecuteReader());
                            int number = Convert.ToInt32(reader["count"]);

                        }
                        }
                        else{

                        }
                    }
                }
             //   if( number == 0)
               // {
                   // Response.Write("Improper Plain Summaries.");
              //  }
             //   else
                {

                }


                }
            }


        }

  public  bool number { get; set; }
  public  object Hours { get; set; }}



   // public System.Collections.Specialized.NameValueCollection Moose { get; set; }



  //  public string Value { get; set; }

  //  public object msg_arr { get; set; }
}
 }
这一行:

public  object Hours { get; set; }}
你有一个多余的
}
在末尾

  • 确保已引用System.Web
  • 最后把这两个}去掉

  • 您在小时属性中有额外的括号

    public  object Hours { get; set; }}
    

    乍一看,你的“}”太多了。我被大括号的数量弄糊涂了,难怪编译器也会这么做。这样的事情在正确的代码格式下是不会发生的。这个问题显然对一些未来的访问者有所帮助——甚至有人喜欢它。很难预测什么会对新手(或者不是很新手)有所帮助在未来。我只是有这个错误,并通过我所有的{}和分号,找不到任何错误。这让我发疯。结果是VS代码只是混乱。我关闭它,然后重新打开,问题神秘地消失了。