简体   繁体   中英

How can I pass my getters and setters parameters to connection class?

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

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

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();
......

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

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(); }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM