0

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

1 回答 1

2

您的方法 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(); }
}
于 2013-09-02T21:33:52.483 回答