如何确定C#中现有的oracle数据库连接? [英] How to determine an existing oracle database connection in C#?

查看:178
本文介绍了如何确定C#中现有的oracle数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用正确的凭证调用以下方法:

Assuming that I call the method below with the right credentials:

private bool Connect(string username, string password)
    {
        string CONNSTRING = "Provider = MSDAORA; Data Source = ISDQA; User ID = {0}; Password = {1};";
        OleDbConnection conn = new OleDbConnection();
        string strCon = string.Format(CONNSTRING, username, password);
        conn.ConnectionString = strCon;
        bool isConnected = false;

        try
        {
            conn.Open();

            if (conn.State.ToString() == "Open")
                isConnected = true;
        }//try
        catch (Exception ex)
        {
            lblErr.Text = "Connection error";
        }//catch
        finally
        {
            conn.Close();
        }//finally

        return isConnected;
    }

我已成功打开我的方法中的连接:

I have successfully open the connection in my method below:

private bool ValidateUserCode(string usercode)
{
    UserAccountDefine def = new UserAccountDefine();
    UserAccountService srvc = new UserAccountService();
    UserAccountObj obj = new UserAccountObj();

    bool returnVal = false;
    bool isValid = Connect(def.DB_DUMMY_USERCODE, def.DB_DUMMY_PASSWORD);
    if (isValid)
    {
        obj.SQLQuery = string.Format(def.SQL_LOGIN, usercode.ToLower(), DateTime.Now.ToString("MM/dd/yyy"));
        DataTable dt = srvc.Execute(obj, CRUD.READALL);
        if (dt.Rows.Count == 1)
        {
            returnVal = true;
        }
    }
    return returnVal;
}

问题是如何确定 ValidateUserCode()方法?

The question is how can I determine the connection status in ValidateUserCode() method? How can I close it afterwards?

注意:
我在 UserAccountDefine()中明确声明字符串变量; / code>

Note: I explicitly declare the string variables in UserAccountDefine(); so you don't have to worry about that.

我已经尝试声明一个新的 OleDbConnection conn ValidateUserCode()中,但 conn.State 总是返回

I already tried declaring a new OleDbConnection conn inside the ValidateUserCode() to but the conn.State always returning "Closed".

UPDATE

系统具有双层安全特性。第一是在应用程序和第二是在数据库。如果用户登录到应用程序,用户名和密码也用于将他/她登录到数据库。现在,情况是当用户忘记了他/她的密码时,我们无法确定 fullname 电子邮件 联系人(在数据库中维护)。我只知道他的 usercode 。要确定联系人详细信息,我必须使用 DUMMY_ACCOUNT 打开活动连接。

I have a system with 2-layer security feature. 1st is in application and 2nd is on database. If a user logs in to the application, the username and password is also used to log him/her in to the database. Now, the scenario is when a user forgot his/her password, we can't determine the fullname, email and contact (which are maintained in the database) of the user. I just know his usercode. To determine the contact details, I have to open an active connection using a DUMMY_ACCOUNT.

请注意,

推荐答案

首先,调用 Close() finally 块中,这意味着在第二个方法的任何时候,连接将被关闭。此外,即使你不 Close()它,因为 conn 是<$ c $中的局部变量c> Connect(),当您回到 ValidateUserCode()时,连接已经开始进行垃圾回收, c $ c> Dispose() d,它也会自动关闭。

First of all, you call Close() in your finally block, which means that at any point in your second method, the connection would be closed. Moreover, even if you don't Close() it,since conn is a local variable in Connect(), when you're back in ValidateUserCode(), the connection is already up for garbage collection, and when it's Dispose()d, it also closes automatically.

我建议您将其设为会员,参数,通过 Connect()方法返回它(并返回 null

I sugges you either make it a member, pass it as an out parameter, return it by the Connect() method (and return null for failure, or something, if you don't like exceptions)..or redesign the code.

private OleDbConnection Connect(string username, string password)
{
    string CONNSTRING = "Provider = MSDAORA; Data Source = ISDQA; User ID = {0}; Password = {1};";
    OleDbConnection conn = new OleDbConnection();
    string strCon = string.Format(CONNSTRING, username, password);
    conn.ConnectionString = strCon;

    try
    {
        conn.Open();

        if (conn.State.ToString() == "Open")
            return conn;
    }//try
    catch (Exception ex)
    {
        lblErr.Text = "Connection error";
    }//catch
    finally
    {
        //you don't want to close it here
        //conn.Close();
    }//finally

    return null;
}

这篇关于如何确定C#中现有的oracle数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆