2017-02-02 2 views
2

데이터베이스에서 두 개의 값을 선택하고 사용자가 제공 한 텍스트 상자 값과 비교하는 클래스입니다. 아래는 내 수업입니다.Oracle.DataAccess.Client.OracleException C#

 public void Userlogin(TextBox username, TextBox pwd) 
    { 
     int _failedAttempt = 0; 

     OpenConnection(); 

     command = new OracleCommand(); 
     command.CommandText = "SELECT username, user_pwd FROM dinein_system_users WHERE username:usrname AND user_pwd:pwd"; 
     command.Connection = dbconnect; 
     command.BindByName = true; 

     try 
     { 
      command.Parameters.Add("usrname", username.Text); 
      command.Parameters.Add("pwd", pwd.Text); 
     } 
     catch (NullReferenceException NRE) 
     { 
      MessageBox.Show("Please contact your developer about this error. Thank you " + NRE); 
     } 

     _reader = command.ExecuteReader(); 
     if (_reader.Read() != true) 
     { 
      _failedAttempt = _failedAttempt + 1; 
      while (_failedAttempt < 3) 
      { 
       MessageBox.Show("Incorrect Username or Password. Please try again " + "Attempts: " + _failedAttempt); 
       username.ResetText(); 
       pwd.ResetText(); 
      } 
     } 
     else 
     { 
      MessageBox.Show("Welcome"); 
     } 
    } 

내 연결 문자열 프로그램이 내가이 오류를

An unhandled exception of type 'Oracle.DataAccess.Client.OracleException' occurred in Oracle.DataAccess.dll 

추가 정보를 얻고 실행 그래서

this._connectionString = "Data Source=xe;Max Pool Size=50;Min Pool Size=1;Connection Lifetime=120;Enlist=true;User Id=hr;Password=hr"; 

: 외부 구성 요소가 예외를 던졌다. 나는 지난 1 시간 동안이 일을 도와주었습니다.

업데이트

연결 열기 방법

public void OpenConnection() 
    { 
     try 
     { 
      if (dbconnect == null) 
      { 
       dbconnect = new OracleConnection(this._connectionString); 
       dbconnect.Open(); 
       return; 
      } 

      switch (dbconnect.State) 
      { 
       case ConnectionState.Closed: 
       case ConnectionState.Broken: 
        dbconnect.Close(); 
        dbconnect.Dispose(); 
        dbconnect = new OracleConnection(this._connectionString); 
        dbconnect.Open(); 
        return; 
      } 
     } 
     catch (OracleException oracleException) 
     { 
      MessageBox.Show("Database connectionString is null. Contact your developer! " + oracleException); 
     } 

    } 

예외 스택 추적은

at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck) 
    at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, String procedure, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, Boolean bCheck) 
    at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior) 
    at Oracle.DataAccess.Client.OracleCommand.ExecuteReader() 
    at DINEIN.OracleDB_Connection.Userlogin(TextBox username, TextBox pwd) in f:\My Documents\Projects\DINEIN\DINEIN\OracleDB_Connection.cs:line 92 
    at DINEIN.Login.btn_login_Click(Object sender, EventArgs e) in f:\My Documents\Projects\DINEIN\DINEIN\Login.cs:line 31 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.PerformClick() 
    at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData) 
    at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) 
    at System.Windows.Forms.Control.PreProcessMessage(Message& msg) 
    at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) 
    at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg) 
     at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.Run(Form mainForm) 
    at DINEIN.Program.Main() in f:\My Documents\Projects\DINEIN\DINEIN\Program.cs:line 19 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 
+0

'대해서 openConnection() '메서드에 대한 코드를 보여주십시오. 또한 예외가 발생하기 전에 마지막으로 실행되는 코드는 무엇입니까? 예외 스택 추적에서 찾아서 제공하십시오. – STLDeveloper

+0

@STLDeveloper OpenConnection() 메서드와 예외 스택 추적으로 내 질문을 업데이트했습니다. 지금 볼 수 있니? 고마워요 –

+0

고마워요. 위의'Userlogin()'메소드의 코드 중 92 번째 줄은 어느 라인입니까? – STLDeveloper

답변

0

오히려 별도의 OpenConnection() 방법을 사용하지 않고, 코딩 고려 using() 성명 그것은 당신의 연결이 다른 데이터베이스 관련 객체가 항상 처분되도록합니다. 예를 들어

:

int _failedAttempt = 0; 

public void Userlogin(TextBox username, TextBox pwd) 
{ 
    try 
    { 
    using (var connection = new OracleConnection(_connectionString)) 
    { 
     connection.Open(); 

     using (var command = new OracleCommand()) 
     { 
     command.CommandText = "SELECT username, user_pwd FROM dinein_system_users WHERE username= :usrname AND user_pwd= :pwd"; 
     command.Connection = connection; 
     command.BindByName = true; 

     command.Parameters.Add("usrname", username.Text); 
     command.Parameters.Add("pwd", pwd.Text); 

     using (var reader = command.ExecuteReader()) 
     { 
      if (reader.Read() != true) 
      { 
      _failedAttempt += 1; 
      if (_failedAttempt < 3) 
      { 
       MessageBox.Show("Incorrect Username or Password. " + 
           "Please try again. " + 
           $"Attempts: {_failedAttempt}"); 
       username.ResetText(); 
       pwd.ResetText(); 
      } 
      else 
      { 
       // 3 failed attempts 
      } 
      } 
      else 
      { 
      _failedAttempt = 0; 
      MessageBox.Show("Welcome"); 
      } 
     } 
     } 
    } 
    } 
    catch(OracleException ex) 
    { 
    MessageBox.Show("Error: {ex}"); 
    } 
} 
+0

이 코드를 시도해도 여전히 var reader = command.ExecuteReader()에서 같은 오류가 발생합니다. –

+0

try-catch 블록을 추가하십시오. OracleException 유형의 예외를 포착하고 있습니다. 특정 유형의 예외를 살펴볼 때 여기에서 무슨 일이 벌어지고 있는지 판단하는 데 도움이되는 유용한 정보를 얻게 될 것이라고 생각합니다. – STLDeveloper

+0

예'처리되지 않은 'System.NullReferenceException 형식의 예외'가 DINEIN.exe에서 발생했습니다. 추가 정보 : CloseConnection() 메서드의 개체 참조가 개체 인스턴스로 설정되지 않았습니다. –