C#Web开发者Web服务

C#Web开发者Web服务,c#,asp.net,C#,Asp.net,因此,我正在制作一个网站,作为c#web developer大学项目的一部分,我收到了这个错误,这是web服务,它已连接到数据库,我似乎找不到错误: **Error 1 Type or namespace definition, or end-of-file expected Source Error: Line 278: } Line 279: } Line 280:}** 现在我能从这里走到哪里?删除它会弄乱整个站点,添加另一个括号也无济于事 using Sy

因此,我正在制作一个网站,作为c#web developer大学项目的一部分,我收到了这个错误,这是web服务,它已连接到数据库,我似乎找不到错误:

**Error 1   Type or namespace definition, or end-of-file expected   
Source Error:
Line 278:        }
Line 279:    }
Line 280:}**
现在我能从这里走到哪里?删除它会弄乱整个站点,添加另一个括号也无济于事

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Web.Services.Protocols;
using System.Xml.Linq;

    [WebService(Namespace = "http://tempuri.org/")]

public class WebService : System.Web.Services.WebService
    {

    // Connection is initialized 
        OleDbConnection conn;
        OleDbDataReader dbReader;

    private void ConnectToDatabase()
    {
        // Creates a connection to the database
        conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Server.MapPath("App_Data\\Sports Car Auction Database.accdb"));

        // Opens the connection
        conn.Open();
    }

    private void DisconnectDatabase()
    {
        // The connection is closed
        conn.Close();
    }

    [WebMethod]
    public string Login(string userName)
    {
        // Connects to the database
        ConnectToDatabase();

        try
        {
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = ("Select Password FROM [Buyer Information]  WHERE UserName = '" + userName + "'");

            dbReader = cmd.ExecuteReader();
            dbReader.Read();

            // The result is read from the datareader and returned to the calling method
            string result = (string)dbReader["Password"];
            return result;
        }
        catch (OleDbException)
        {
            // Nothing is returned if there are exceptions
            return null;
        }
    }

     [WebMethod]
    public DataSet ForgetPass(string userName)
    {
        try
        {
            // Connect to database
            ConnectToDatabase();

            // Info from the database is selected via the data adapter
            OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT [Secret Question], [Answer] From [Buyer Information] Where [UserName] = '" + userName + "'", conn);

            // Dataset stores results
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            // Dataset is returned to calling method
            return ds;
        }
        catch
        {
            // Nothing is returned if there are exceptions
            return null;
        }
    }

    [WebMethod]
        // Method defines what will be recieved from ChangePassword.aspx
        public void ChangePass(string Pass, string userName)
        {
            // Connects to the database
            ConnectToDatabase();

            // Values in the database are updated
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = (@"UPDATE [Buyer Information] SET [Password] = '" + Pass + "' WHERE UserName = '" + userName + "'");
            cmd.ExecuteNonQuery();

            // The connection is closed
            DisconnectDatabase();
        }

        [WebMethod]
        // Method define what values will be recieved form register.aspx
        public void RegisterCustomer(string UserName, string Address, string Tel, string Email, string Ques, string Ans, string Pass)
        {
            // Connects to thedatabase
            ConnectToDatabase();

            // Values are inserted into the database
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = @"INSERT INTO [Buyer Information] ([UserName], [Address], [Telephone], [Email], [Password], [Secret Question], [Answer]) VALUES ('" + UserName + "', '" + Address + "', '" + Tel + "', '" + Email + "', '" + Pass + "', '" + Ques + "', '" + Ans + "')";

            cmd.ExecuteNonQuery();

            // The connection is closed
            DisconnectDatabase();
        }

        [WebMethod]
        public DataSet ViewDetails(string userName)
        {
            try
            {
                // Connects to the database
                ConnectToDatabase();

                // The correct data is selected from the database using the data adapter
                OleDbDataAdapter adapter = new OleDbDataAdapter(@" SELECT Address, Telephone, Email, [Secret Question], Answer FROM [Buyer Information] WHERE UserName = '" + userName + "'", conn);

                // The sesults are stored in the dataset
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                // Dataset is returned to the calling method
                return ds;
            }
            catch (OleDbException)
            {
                // Nothing is returned if there are exceptions
                return null;
            }
        }

        [WebMethod]
        // This defines what values will be recieved from Details.aspx
        public void UpdateCustomer(string userName, string Address, string Tel, string Email, string Ques, string Ans)
        {
            // Connects to the database
            ConnectToDatabase();

            // Updates the database
            OleDbCommand cmd = new OleDbCommand(@"UPDATE [Buyer Information] SET [UserName] = '" + userName + "', [Address] = '" + Address + "', [Telephone] = '" + Tel + "', [Email] = '" + Email + "', [Secret Question] = '" + Ques + "', [Answer] = '" + Ans + "' WHERE [UserName] = '" + userName + "'", conn);

            cmd.ExecuteNonQuery();

            // The connection is closed
            DisconnectDatabase();
        }

        [WebMethod]
        public DataSet SelectItem()
        {
            try
            {
                ConnectToDatabase();

                // Get the model values for the drop down list
                OleDbDataAdapter da = new OleDbDataAdapter("SELECT Model FROM Car", conn);

                DataSet ds = new DataSet();
                da.Fill(ds, "Model");

                return ds;
            }
            catch (OleDbException)
            {
                // Nothing is returned if there are exceptions
                return null;
            }
        }

        [WebMethod]
        public DataSet selectCarInfo(string model)
        {
            try
            {
                // Connects to the database
                ConnectToDatabase();

                // Info is selected from the database via the data adapter
                OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT [Car Information].carID, [Car Information].Make, [Car Information].Description, [Car Information].[Starting Bid], [Car Information].[Closing Date] FROM [Car Information] WHERE Model = '" + model + "'", conn);

                // The results are stored in dataset
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                // Dataset is returned to calling method
                return ds;
            }
            catch
            {
                // Nothing is returned if there are exceptions
                return null;
            }
        }

        [WebMethod]
        public decimal highestBidVal(int carID)
        {
            try
            {
                // Connects to the database
                ConnectToDatabase();

                // Selects highestBid to compare to Buyer Informations value
                OleDbCommand cm = conn.CreateCommand();
                cm.CommandText = ("SELECT [Bid Information].HighestBid  FROM [Bid Information] WHERE carID = " + carID + "");

                dbReader = cm.ExecuteReader();
                dbReader.Read();

                decimal highestBidValue = (decimal)dbReader["HighestBid"];
                return highestBidValue;
            }
            catch (OleDbException)
            {
                // Nothing is returned if there are exceptions
                return 0;
            }
        }

        [WebMethod]
        // Method that defines what will be recieved from AddNewItem.aspx
        public void AddNewCar(string Make, string Model, string Description, decimal StartingBid, DateTime closeDate, string owner)
        {
            ConnectToDatabase();

            OleDbCommand cmd = conn.CreateCommand();

            // Values are inserted into the database
            cmd.CommandText = (@" INSERT INTO [Car Information] ([Make], [Model], [Description], [Starting Bid], [Closing Date], [Owner]) VALUES ('" + Make + "', '" + Model + "', '" + Description + "', '" + StartingBid + "', '" + closeDate + "', '" + owner + "')");

            cmd.ExecuteNonQuery();

            // Closes the connection
            DisconnectDatabase();
        }

        [WebMethod]
        // Method that defines what will be recieved from PlaceBid.aspx
        public void AddNewBid(int carid, string userName, decimal bidValue,
            DateTime bidingDate)
        {
            ConnectToDatabase();

            // Values are updated in the database
            OleDbCommand cmd = new OleDbCommand(@"UPDATE [Bid Information] SET [carID] = '" + carid + "', [UserName] = '" + userName + "', [Highest Bid] = '" + bidValue + "', [Bid Date] = '" + bidingDate + "' WHERE [carID] = " + carid + "", conn);

            cmd.ExecuteNonQuery();

            // The connection is closed
            DisconnectDatabase();
        }

        [WebMethod]
        // Method that defines what values will be recieved from Placebid.aspx
        public void AddBid(int carid, string userName, decimal bidValue, DateTime bidingDate)
        {
            ConnectToDatabase();

            OleDbCommand cmd = conn.CreateCommand();
            // Values are inserted into the database
            cmd.CommandText = (@"INSERT INTO [Bid Information] ([carID], [UserName], [UserName], [HighestBid], [Biddate]) VALUES ('" + carid + "', '" + userName + "', '" + bidValue + "', '" + bidingDate + "')");

            cmd.ExecuteNonQuery();

            // Closes the connection
            DisconnectDatabase();
        }
    }
}

最后一个结束括号似乎是与名称空间匹配的括号。移除它应该可以做到这一点

您可以尝试使用control-k-d。如果有效,则括号(以及括号的数量)匹配


如果仍然出现错误,则表示代码中有更多错误。您可能确实缺少using指令,但这是另一个错误。您确实需要删除该右括号,因为它没有与右括号匹配的右括号。

您是否尝试缩进代码?文件末尾有多余的}。删除它。格式化代码热键真的很有帮助,然后我发现我的web服务命名不正确。现在工作起来很有魅力。:)