如何将我的getters和setters参数传递给连接类? [英] How can I pass my getters and setters parameters to connection class?

查看:92
本文介绍了如何将我的getters和setters参数传递给连接类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将文本框输入分配给getter,setter也创建了一个连接类.如何将我的getters和setters参数传递给连接类,以便可以在我的主窗体中使用它.

I assigned the textbox inputs to getters and setters also created one connection class. How can I pass my getters and setters parameters to connection class so I can use it inside my mainform.

会员等级

MemberClass

private string srDatabase = "";
private string srID = "";
private string srPass = "";

public string SDB
{
    get
    {
        return srDatabase;
    }
    set
    {
        srDatabase= value;
    }
}
public string SID
{
    get
    {
        return srID ;
    }
    set
    {
        srID = value;
    }
}
public string SPassword
{
    get
    {
        return srPass ;
    }
    set
    {
        srPass = value;
    }
}

ConnectionClass

ConnectionClass

 class Connection
    {
        public static OracleConnection GetConnection(string dataSource, string userName, string password)
        {
            OracleConnection con = null;
            if(!string.IsNullOrWhiteSpace(dataSource) && !string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password))
                {
                    con = new OracleConnection("Data Source=" + dataSource + ";User Id=" + userName.ToUpper() + ";Password=" + password + ";");
                    return con;
                }

            return con;
        }
    }

MainForm

MainForm

        UserMembers  = new UserMembers();

        txtSrcUserDatabase.Text = src.srDatabase ;
        txtSrcUserID.Text=src.srID.ToUpper();
        txtSrcUserPassword.Text = src.srPass;



               OracleConnection conn1 = Connection.GetConnection() // **here error**
               conn1.Open();

                using (OracleCommand Names = new OracleCommand("SELECT TABLE_NAME FROM USER_TABLES ORDER BY TABLE_NAME", conn1))
                {
                    using (OracleDataReader reader = Names.ExecuteReader())
                    {                            
                        while (reader.Read())
                        {                                    
                            //Do something                              
                        }
                    }
                }

推荐答案

您的方法GetConnection需要三个参数.您需要将它们传递给方法.

Your method GetConnection requires three parameters. You need to pass them to the method.

UserMembers  src = new UserMembers();

src.srDatabase =txtSrcUserDatabase.Text;
src.srID = txtSrcUserID.Text.ToUpper();
src.srPass = txtSrcUserPassword.Text;
OracleConnection conn1 = Connection.GetConnection(src.srDatabase, src.srID, src.srPass) 
conn1.Open();
......

或者您可以将UserMembers的实例传递给GetConnection方法,从而像这样创建GetConnection的重载

Or you could pass the instance of UserMembers to the GetConnection method creating an overload of GetConnection like this

class Connection
{
    // the first overload that takes 3 string parameters
    public static OracleConnection GetConnection(string dataSource, string userName, string password)
    {
        .... 
    }

    // The second overload that takes an instance of UserMembers
    public static OracleConnection GetConnection(UserMembers src )
    {
        OracleConnection con = null;
        if(!string.IsNullOrWhiteSpace(sr.srDatabase) && !string.IsNullOrWhiteSpace(sr.srID) && !string.IsNullOrWhiteSpace(sr.srPass))
        {
                con = new OracleConnection("Data Source=" + sr.srDatabase + ";User Id=" + sr.srID.ToUpper() + ";Password=" + sr.Pass + ";");
        }
        return con;
    }
}

作为旁注.如果您需要srID成员始终使用大写字母,则可以在setter属性中移动此逻辑,当您尝试回读该成员时,可以不必担心该成员的格式正确

As a side note. If you need the srID member to be always in upper case then move this logic in the setter property, and you could stop to worry about the proper formatting of this member when you try to read it back

public string SID
{
    get  { return srID ; }
    set  { srID = value.ToUpper(); }
}

这篇关于如何将我的getters和setters参数传递给连接类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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