首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Oracle.DataAccess.Client.OracleException C#

Oracle.DataAccess.Client.OracleException C#
EN

Stack Overflow用户
提问于 2017-02-02 08:40:19
回答 1查看 4.7K关注 0票数 2

在这个类中,我从数据库中选择两个值,并将其与用户提供的textbox值进行比较。下面是我的班。

代码语言:javascript
复制
     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");
        }
    }

我的连接字符串

代码语言:javascript
复制
this._connectionString = "Data Source=xe;Max Pool Size=50;Min Pool Size=1;Connection Lifetime=120;Enlist=true;User Id=hr;Password=hr";

所以当程序执行时,我会得到这个错误。

代码语言:javascript
复制
An unhandled exception of type 'Oracle.DataAccess.Client.OracleException' occurred in Oracle.DataAccess.dll

附加信息:外部组件引发异常。在过去的一小时里,我一直在此工作,任何帮助都将不胜感激。

更新

开放连接方法

代码语言:javascript
复制
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);
        }

    }

异常堆栈跟踪

代码语言:javascript
复制
   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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-02 09:16:16

与其使用单独的OpenConnection()方法,不如考虑使用using()语句进行编码。它将确保您的连接(一个与数据库相关的其他对象)总是被释放.

例如:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41997598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档