none
请帮忙看看是哪里出错了,谢谢 RRS feed

  • 问题

  • 我将数据访问层的方法改成了静态方法,然后提示:<o:p></o:p>

    对象引用对于非静态的字段、方法或属性“DataAccess.ConnectionString”是必需的    <o:p></o:p>

    请问要如何修改?谢谢

    public class DataAccess
    {
    	public string ConnectionString = "server=192.168.2.19;User Id=root;password=12!;Database=project_status;Character Set=utf8;";            
    	public static MySqlDataReader GetDataReader(string sql)
    	{
    		MySqlDataReader resultReader = null;
    		MySqlConnection connection = new MySqlConnection(DataAccess.ConnectionString);//此处提示错误
    		MySqlCommand command = new MySqlCommand(sql, connection);
    		command.CommandType = CommandType.Text;
    		connection.Open();
    		resultReader = command.ExecuteReader(CommandBehavior.CloseConnection);
    		return resultReader;
    	}
    	
    }
    

    2017年9月14日 13:20

答案

  • 该层这样

    public static class DataAccess
    {
    	public  static string ConnectionString = "server=192.168.2.19;User Id=root;password=12!;Database=project_status;Character Set=utf8;";            
    	public static MySqlDataReader GetDataReader(string sql)
    	{
    		MySqlDataReader resultReader = null;
    		MySqlConnection connection = new MySqlConnection(DataAccess.ConnectionString);//此处提示错误
    		MySqlCommand command = new MySqlCommand(sql, connection);
    		command.CommandType = CommandType.Text;
    		connection.Open();
    		resultReader = command.ExecuteReader(CommandBehavior.CloseConnection);
    		return resultReader;
    	}
    	
    }


    please verify my account

    2017年9月15日 0:07