2014-06-20 29 views
-1

所以我想驗證一個字符串,我從MySQL數據庫收到但由於某種原因無法訪問。我相信這是一個愚蠢的問題,但任何人都可以幫助我?我覺得它可能與公共與私人以及靜態有關,但是在我想到通過變量傳遞每個組合後,仍然不斷給我提供錯誤。有任何想法嗎?使用字符串從公共靜態無效私人布爾C#

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; 
     } 
    } 
+0

你能指出具體的錯誤和位置,這是問題? – BradleyDotNET

+0

是「私人布爾()「兩次或是一個錯字? – alykins

+0

對不起,只有一個私人布爾va​​lidName() – trehm07

回答

0

patientName僅存的getNewRow範圍之內。你需要創建一個類變量(如果你想在getNewRow之外的任何地方使用它們,你可以使用failReason。你也可以將它們作爲參數傳遞,但是我沒有看到你的方法有參數。

+0

我曾嘗試讓它們成爲全局類的範圍,但是然後在getRow()中獲得了非靜態字段,方法或屬性所需的對象引用中的錯誤 – trehm07

+0

@ trehm07顯示帶有此更改的代碼。這很好,你已經包含了一個實際的錯誤信息,但你仍然需要指出它發生在哪裏。我們不是心靈的。 – tnw

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

是本地 公共靜態無效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; 
    } 
} 
-2

你應該決定把所有的方法靜態或沒有。你不能從一個靜態方法調用非靜態方法。

+1

這不是OP得到的錯誤。雖然這是一個真實的陳述,但我沒有看到OP在做這件事的代碼中的任何地方。 -1 – tnw

相關問題