【问题标题】:how to return a class method's values to the respective pages that called it如何将类方法的值返回到调用它的各个页面
【发布时间】:2014-06-27 07:35:50
【问题描述】:

所以我的应用程序中有两个页面。 我还有一个 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);
    }

这基本上是为了允许使用页面中的文本框浏览记录。 有什么线索可以解决这个问题吗?

【问题讨论】:

    标签: c# mysql wpf


    【解决方案1】:

    如果我理解正确,您说您已经有一个访问您的数据的类,所以您的问题的一半已经排序。你真正的问题是因为你把你的顾虑混在了一节课上。在开发中,我们的目标是让每个类都有一个目的,而您当前的问题正是原因。

    您只需将数据访问代码拆分为一个类,并准备好数据以在其他类的 UI 中显示。所以你的RegistrationPages 类应该只获取数据并拥有名称如GetThisData 和GetThatData 的方法(显然不完全一样)。

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

    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;
    

    【讨论】:

    • 好的,我明白你的解释了。你的代码对我来说有点新,但我试试这个并更新你。
    • 并不复杂。在您的RegistrationPages 中实现一个GetThisData 方法,该方法为您的一个页面返回数据,并一个GetThatData 方法为您的另一页面返回数据等。显然,您可以随意命名它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 2023-04-03
    • 1970-01-01
    • 2020-01-14
    • 2012-08-26
    • 1970-01-01
    • 2014-08-28
    相关资源
    最近更新 更多