2

所以我正在创建一个网站作为 c# web 开发人员的大学项目的一部分,我收到了这个错误,这是 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();
        }
    }
}
4

1 回答 1

2

最后一个右括号似乎是与命名空间匹配的那个。删除它应该这样做。

您可以尝试使用 control-kd格式化您的代码。如果它有效,那么你的括号(以及它们的数量)匹配。

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

于 2013-01-14T14:04:55.790 回答