如何将类方法的值返回到调用它的各个页面

时间:2014-06-27 07:35:51

标签: c# mysql wpf

所以我的应用程序中有两页。 我还有一个MySQL类,包含要执行的连接参数和查询。 事实是,我需要两个页面在类中使用相同的方法,并将数据返回到触发它的表单。

这种方式适用于一个页面,我只是不知道如何使用它们中的两个或更多。

//MySQL Library
using MySql.Data.MySqlClient;
using MySql.Data;

namespace Masca.users.MySQL
{

    public class registrationSQL
    {
        // The SQL class now knows about the registration page
       public users.RegistrationPages.registrationForm Registration;

       // The SQL class now knows about the registration Form and datagrid page
       public users.RegistrationPages.registrationFormGrid RegistrationFormGrid;

这是我希望全球可访问的方法。

    // I guess this part is what I cant figure out
    // This is where you set where it's supposed to ruturn the strings right?
    // Well What I don't get is how you add another page to return strings to if it calls the method.
    public void foward (users.RegistrationPages.registrationForm RegistrationForm)
    {
        //Declarations
        string datasource = "localhost";
        string port = "3306";
        string username = "root";
        string password = "Password";

        //Connection
        string sqlcon = "datasource = " + datasource + ";" + "port=" + port + ";" + "username=" + username + ";" + "password=" + password + ";";
        //Query
        string query = "select * from temp.signup where tag = (select min(tag) from temp.signup where tag > '" + RegistrationForm.tag.Text + "' );";

        //MySQL declarations
        MySqlConnection con = new MySqlConnection(sqlcon);
        MySqlCommand cmd = new MySqlCommand(query, con);
        MySqlDataReader rdr;

        //Excecution
        try
        {
            //If the connection is Open
            con.Open();
            {
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    //String population
                    // GetString & GetColumnValueAsString are predefined functions
                    // They check if a field is null or an integer before returning it as a string
                    string sid = GetString(rdr, "id");
                    string smember = GetColumnValueAsString(rdr, "member");
                    string stag = GetColumnValueAsString(rdr, "tag");

                    string sfirst = GetString(rdr, "first");
                    string ssecond = GetString(rdr, "second");
                    string sthird = GetString(rdr, "third");
                    string sfourth = GetString(rdr, "surname");

                    string sdob = rdr.GetString("dob");
                    string ssex = rdr.GetString("sex");

                    string susername = GetString(rdr, "username");
                    string spassword = GetString(rdr, "password");
                    string ssecurity = GetString(rdr, "question");
                    string sanswer = GetString(rdr, "answer");

                    string spemail = GetString(rdr, "pemail");
                    string swemail = GetString(rdr, "wemail");

                    string sdoc = rdr.GetString("doc");

                    string scell = GetString(rdr, "cell");
                    string shome = GetString(rdr, "home");
                    string sext = GetColumnValueAsString(rdr, "ext");

                    string sstreet = GetString(rdr, "street");
                    string ssurbub = GetString(rdr, "surbub");
                    string scity = GetString(rdr, "city");
                    string sregion = GetString(rdr, "region");

                    string sdept = rdr.GetString("dept");

                    string sbank = GetString(rdr, "bank");
                    string sbranch = GetString(rdr, "branch");
                    string saccount = GetString(rdr, "account");

                    // Bindings
                    // This is another thing
                    // If another page calls this method besides the 'RegistrationForm'
                    // How am I to direct the strings to that one instead of 'RegistrationForm'

                    RegistrationForm.id.Text = sid;
                    RegistrationForm.member.Text = smember;
                    RegistrationForm.tag.Text = stag;

                    RegistrationForm.first.Text = sfirst;
                    RegistrationForm.second.Text = ssecond;
                    RegistrationForm.third.Text = sthird;
                    RegistrationForm.surname.Text = sfourth;

                    RegistrationForm.dob.Text = sdob;
                    RegistrationForm.sex.Text = ssex;

                    RegistrationForm.username.Text = susername;
                    RegistrationForm.password.Text = spassword;
                    RegistrationForm.question.Text = ssecurity;
                    RegistrationForm.answer.Text = sanswer;

                    RegistrationForm.pemail.Text =  spemail;
                    RegistrationForm.wemail.Text =  swemail;

                    RegistrationForm.doc.Text = sdoc;

                    RegistrationForm.cell.Text =  scell;
                    RegistrationForm.home.Text =  shome;
                    RegistrationForm.ext.Text = sext;

                    RegistrationForm.street.Text = sstreet;
                    RegistrationForm.surbub.Text = ssurbub;
                    RegistrationForm.city.Text =  scity;
                    RegistrationForm.region.Text = sregion;

                    RegistrationForm.dept.Text = sdept;

                    RegistrationForm.bank.Text = sbank;
                    RegistrationForm.branch.Text = sbranch;
                    RegistrationForm.account.Text = saccount;
                }

                // Close the connection
                con.Close();
            }
        }

        catch (Exception ex)
        {
            ModernDialog.ShowMessage(ex.Message , "SQL related error", MessageBoxButton.OK);
        }
    }

这就是我在'registrationForm'中调用此方法的方法

private void next_Click(object sender, RoutedEventArgs e)
    {
        RegistratonSql.foward(this);
    }

这基本上只允许使用页面中的文本框浏览记录。 关于如何解决这个问题的任何线索?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你说你已经有一个访问你数据的类,所以你的问题的一半已经排序了。你真正的问题是由于你在一堂课中混淆了你的顾虑而引起的。在开发过程中,我们的目标是让每个班级都有一个目的,而你当前的问题正是的原因。

您只需将数据访问代码拆分为一个类,并准备好在其他类的UI中显示数据。因此,您的RegistrationPages类应该只获取数据并使用名称为GetThisDataGetThatData的方法(显然不完全相同)。

您的其他类都需要此数据访问类的实例来访问其数据,然后将其打包以准备显示和/或编辑......可能是这样的:

RegistrationPages dataAccess = new RegistrationPages();
SomeCollectionType data = dataAccess.GetThisData();
ObservableCollection<SomeDataType> dataForUi = PrepareDataForUi(data);
SomeUiProperty = dataForUi;

虽然其他班级可以这样做:

RegistrationPages dataAccess = new RegistrationPages();
SomeOtherCollectionType data = dataAccess.GetThatData();
ObservableCollection<SomeOtherDataType> dataForUi = PrepareDataForUi(data);
SomeOtherUiProperty = dataForUi;