在私有bool中使用来自public static void的字符串C#

时间:2014-06-20 17:41:12

标签: c# mysql

所以我试图验证从MySQL数据库收到的字符串,但由于某种原因无法访问。我确信这是一个愚蠢的问题,但任何人都可以帮助我吗?我觉得它可能与公共与私人和静态相关,但在玩了我能想到的每个组合来传递变量之后,它仍然一直给我错误。有什么想法吗?

string failReason = "";
int valid = 0;

public static void getNewRow()
    {

        try
        {
            string getNewRow = "SELECT * FROM appointments WHERE valid IS NULL";

            DbConnection mysqlDB = new DbConnection();

            MySqlCommand mysqlCommand = new MySqlCommand(getNewRow, mysqlDB.GetLocalMySQLConnection());

            MySqlDataReader reader = mysqlCommand.ExecuteReader();

            while (reader.Read())
            {


                    int id = reader.GetInt32("id");
                    string accountID = reader.GetString("accountID");
                    string appDate = reader.GetString("appDate");
                    string appTime = reader.GetString("apptime");
                    string patientName = reader.GetString("patientName");
                    string appPhone = reader.GetString("appPhone");
                    string appPhone2 = reader.GetString("appPhone2");
                    string smsPhone = reader.GetString("smsPhone");
                    string special = reader.GetString("special");
                    string email = reader.GetString("email");
                    string provider = reader.GetString("provider");
                    string location = reader.GetString("location");
                    string other = reader.GetString("other");



                    Console.WriteLine("ID: " + id);
                    Console.WriteLine("AccountID: " + accountID);
                    Console.WriteLine("Appointment Date: " + appDate);
                    Console.WriteLine("Appointment Time: " + appTime);
                    Console.WriteLine("Patient Name: " + patientName);
                    Console.WriteLine("Phone 1: " + appPhone);
                    Console.WriteLine("Phone 2: " + appPhone2);
                    Console.WriteLine("SMS Phone: " + smsPhone);
                    Console.WriteLine("Special: " + special);
                    Console.WriteLine("Email: " + email);
                    Console.WriteLine("Provider: " + provider);
                    Console.WriteLine("Location: " + location);
                    Console.WriteLine("Other: " + other);







            }




        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.InnerException);
        }
    }

    private bool validName()
    {
        if (patientName.Length < 20)
        {
            failReason = "Bad Name";
            return false;
        }
        else
        {
            return true;
        }
    }

private bool validName()
    {
        if (patientName.Length < 20)
        {
            failReason = "Bad Name";
            return false;
        }
        else
        {
            return true;
        }
    }

3 个答案:

答案 0 :(得分:0)

string patientName = reader.GetString("patientName");

是当地的     public static void getNewRow()

这样做:

static string failReason = "";
static int valid = 0;
static string patientName = string.Empty;

public static void getNewRow()
{

    try
    {
        string getNewRow = "SELECT * FROM appointments WHERE valid IS NULL";

        DbConnection mysqlDB = new DbConnection();

        MySqlCommand mysqlCommand = new MySqlCommand(getNewRow, mysqlDB.GetLocalMySQLConnection());

        MySqlDataReader reader = mysqlCommand.ExecuteReader();

        while (reader.Read())
        {


                int id = reader.GetInt32("id");
                string accountID = reader.GetString("accountID");
                string appDate = reader.GetString("appDate");
                string appTime = reader.GetString("apptime");
                patientName = reader.GetString("patientName");
                string appPhone = reader.GetString("appPhone");
                string appPhone2 = reader.GetString("appPhone2");
                string smsPhone = reader.GetString("smsPhone");
                string special = reader.GetString("special");
                string email = reader.GetString("email");
                string provider = reader.GetString("provider");
                string location = reader.GetString("location");
                string other = reader.GetString("other");



                Console.WriteLine("ID: " + id);
                Console.WriteLine("AccountID: " + accountID);
                Console.WriteLine("Appointment Date: " + appDate);
                Console.WriteLine("Appointment Time: " + appTime);
                Console.WriteLine("Patient Name: " + patientName);
                Console.WriteLine("Phone 1: " + appPhone);
                Console.WriteLine("Phone 2: " + appPhone2);
                Console.WriteLine("SMS Phone: " + smsPhone);
                Console.WriteLine("Special: " + special);
                Console.WriteLine("Email: " + email);
                Console.WriteLine("Provider: " + provider);
                Console.WriteLine("Location: " + location);
                Console.WriteLine("Other: " + other);







        }




    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.InnerException);
    }
}

private static bool validName()
{
    if (patientName.Length < 20)
    {
        failReason = "Bad Name";
        return false;
    }
    else
    {
        return true;
    }
}

private static bool validName()
{
    if (patientName.Length < 20)
    {
        failReason = "Bad Name";
        return false;
    }
    else
    {
        return true;
    }
}

答案 1 :(得分:0)

patientName仅存在于getNewRow的范围内。如果你想在failReason之外的任何地方使用它们,你需要将它作为类范围的变量(就像你使用getNewRow一样。)你也可以将它们作为参数传递,但是我没有看到你的方法有参数开始。

答案 2 :(得分:-2)

你应该决定让你的所有方法都是静态的。您不能从静态方法调用非静态方法。