可访问性不一致 - 开发Web服务

时间:2013-10-24 18:01:35

标签: c# asp.net web-services

我正在尝试创建一个用于更新简单数据库表的Web服务。我有一个更新方法,它接受一个类型为Employee的对象的参数。我在Employee类所属的名称空间中包含了一个引用。由于我无法理解的原因,我收到以下错误: 可访问性不一致:参数类型“EmployeeDBApplication.Employee”的方法不如方法'EmployeeStoreWS.EmployeeStoreService.update(EmployeeDBApplication.Employee)'

class Employee
{

    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    private double salary;

    public double Salary
    {
        get { return salary; }
        set { salary = value; }
    }
    private string address;

    public string Address
    {
        get { return address; }
        set { address = value; }
    }
    private string firstname;

    public string Firstname
    {
        get { return firstname; }
        set { firstname = value; }
    }
    private string lastname;

    public string Lastname
    {
        get { return lastname; }
        set { lastname = value; }
    }


    public override string ToString() {
        string x;
        x = "Employee ID:" + this.id + "\tName:" + this.firstname + "\t" + this.lastname + "\n\tSalary:" + this.salary + "\t Address:" + this.address; 

        return x;
    }

}

网络服务:

public class EmployeeStoreService : System.Web.Services.WebService
{
    //id    int(11) NO  PRI 0   
    //firstname varchar(255)    YES         
    //lastname  varchar(255)    YES         
    //address   varchar(255)    YES         
    //salary    double  YES         

    [WebMethod]
    public MySqlConnection getConnection()
    {
        return new MySqlConnection("Database=sakila;Data Source=localhost;User Id=root;Password=george 01");
    }

    [WebMethod]
    public void update(Employee employee)
    {
        MySqlConnection connection = null;
        try
        {
            connection = getConnection();
            connection.Open();

            MySqlCommand myCommand = new MySqlCommand("UPDATE employee SET (?id,?firstname,?lastname,?address,?salary) WHERE employee.id = ?id");
            myCommand.Prepare();

            myCommand.Parameters.AddWithValue("?id", employee.Id);
            myCommand.Parameters.AddWithValue("?firstname", employee.Firstname);
            myCommand.Parameters.AddWithValue("?lastname", employee.Lastname);
            myCommand.Parameters.AddWithValue("?address", employee.Address);
            myCommand.Parameters.AddWithValue("?salary", employee.Salary);

            myCommand.ExecuteNonQuery();

            connection.Close();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            if (connection != null)
                connection.Close();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

尝试更改此

class Employee
{

    private int id;
    //...

到此

public class Employee
{

    private int id;
    //...

除非您有特殊原因,否则请将Employee定义为public

答案 1 :(得分:1)

你需要公开你的课程。

public class Employee

它抱怨你的一些班级成员是公开的,而班级本身则没有。默认情况下,类是内部的

答案 2 :(得分:0)

您的更新方法是公开的,因为您的班级是内部的。因此消息。如果您未指定访问修饰符,则内部。