我怎样才能把我的getter和setter参数来连接类?(How can I pass my get

2019-11-02 10:21发布

我分配文本框输入getter和setter方法也创造了一个连接类。 我怎样才能把我的getter和setter方法参数的连接类,所以我可以用它在我的MainForm内。

成员类

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

 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

        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                              
                        }
                    }
                }

Answer 1:

你getConnection方法需要三个参数。 你需要将它们传递给方法。

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像这样的超载

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属性移动这个逻辑,你可以停下来担心这个元件的正确的格式,当您尝试读回

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


文章来源: How can I pass my getters and setters parameters to connection class?