【问题标题】:C# Call function from another form (radform)C# 从另一种形式调用函数 (radform)
【发布时间】:2017-11-06 05:17:06
【问题描述】:

首先,我想指出,我一直在寻找解决这个问题的方法,但没有运气。

所以基本上我在 Radform1 中有一个 Mysql 函数,我想从 CustomAppointmentEditForm 调用

Radform1

  public RadForm1()
    {
        InitializeComponent();

    }

CustomAppointmentEditForm

        public CustomAppointmentEditForm()
    {
        InitializeComponent();

    }

我想从 Radform1 调用的函数

private void Update_LeadInfo()
    {
        try
        {
            using (MySqlConnection cn = new MySqlConnection(ConString))
            {
                string UniqueID = textBoxDescription.Text;

                string CVR = txtCVR.Text;
                string Firma = txtFirma.Text;
                string Nummer = txtNummer.Text;
                string Addresse = txtAddresse.Text;
                string Postnr = txtPostnr.Text;
                string By = txtBy.Text;
                string Noter = txtNoter.Text;
                string Email = txtEmail.Text;
                string StartDato = dateStart.Text + " " + timeStart.Text;
                string SlutDato = dateEnd.Text + " " + timeEnd.Text;
                string Afholdt = radCheckBox1.CheckState.ToString();
                if(Afholdt == "Checked")
                {
                    Afholdt = "1";
                }
                else
                {
                    Afholdt = "0";
                }
                if(chkAllDay.CheckState == CheckState.Checked)
                {
                    StartDato = dateStart.Text + " " + "00:00";
                    SlutDato = dateEnd.Text + " " + "00:00";
                }
                cn.Open();

                Console.WriteLine(UniqueID);
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = "UPDATE Leads SET CVR = ('" + CVR + "'), Firma = ('" + Firma + "'), Nummer = ('" + Nummer + "'), Addresse = ('" + Addresse + "'), Postnr = ('" + Postnr + "'), Bynavn = ('" + By + "'), Noter = ('" + Noter + "'), Afholdt = ('" + Afholdt + "'), Email = ('" + Email + "'), Slut_Dato = ('" + SlutDato + "')  WHERE UniqueID = ('" + UniqueID + "');";
                cmd.ExecuteNonQuery();

                cmd.Dispose();
                cn.Close();
            }
        }
        catch (Exception Fejl)
        {
            Console.WriteLine(Fejl);
        }
    }

我知道有一个简单的解决方案,在此先感谢

【问题讨论】:

  • 您需要在您的CustomAppointmentEditForm 中引用Radform1。这已经被问了四万亿次了。
  • 你能举个例子吗?如前所述,我正在使用 Radforms,有一点不同
  • Radform1 formObj = new Radform1(); 然后像这样调用 show 方法formObj.Update_LeadInfo();
  • @Taco2 完全没有区别。 OOP 保持 OOP 独立于您正在使用的任何框架、库等。
  • 我认为@un-lucky 的意思是formObj.Update_LeadInfo()

标签: c# mysql methods call


【解决方案1】:

编辑: 我不会在那里使用这种方法。

Update_LeadInfo 除了那些TextBox.Text 对象外,似乎没有使用来自RadForm1 的任何方法。

创建另一个具有访问数据库的参数的类,例如

public static class DatabaseAccess{

    private static string ConString = "<SOMECONSTRING>";
    public static void Update_LeadInfo(LeadInfo infoObj){ //Don't mix CamelCase and _ Case Notation
        using (MySqlConnection cn = new MySqlConnection(ConString))
        {
           MySqlCommand cmd = new MySqlCommand();
           cmd.Connection = cn;
           cmd.CommandText = "UPDATE Leads SET CVR = ('" + infoObj.CVR + "'), Firma = ('" + infoObj.Firma + "'), Nummer = ('" + infoObj.Nummer + "'), Addresse = ('" + infoObjAddresse + "'), Postnr = ('" + infoObj.Postnr + "'), Bynavn = ('" + infoObj.By + "'), Noter = ('" + infoObj.Noter + "'), Afholdt = ('" + infoObj.Afholdt + "'), Email = ('" + infoObj.Email + "'), Slut_Dato = ('" + infoObj.SlutDato + "')  WHERE UniqueID = ('" + infoObj.UniqueID + "');";
           cmd.ExecuteNonQuery();

           cmd.Dispose();
           cn.Close();
        }
    }
}

LeadInfo班级:

 public class LeadInfo{
     public string CVR {get;set}
     ...
     public string Afholdt {get;set;}
 }

并使用参数化查询

而不是cmd.CommandText = "UPDATE Leads SET CVR = ('" + infoObj.CVR + "'), Firma = ('" + infoObj.Firma + "'), Nummer = ('" + infoObj.Nummer + "'), Addresse = ('" + infoObjAddresse + "'), Postnr = ('" + infoObj.Postnr + "'), Bynavn = ('" + infoObj.By + "'), Noter = ('" + infoObj.Noter + "'), Afholdt = ('" + infoObj.Afholdt + "'), Email = ('" + infoObj.Email + "'), Slut_Dato = ('" + infoObj.SlutDato + "') WHERE UniqueID = ('" + infoObj.UniqueID + "');";

用途:

string updateCommand = "UPDATE Leads SET CVR = @paramCVR, ..., WHERE UniqueID = @paramUniqueID;"
MySqlCommand m = new MySqlCommand(updateCommand);
m.Parameters.AddWithValue("@paramCVR", infoObj.CVR);
...
m.Parameters.AddWithValue("@paramUniqueID", infoObj.UniqueID);

要访问另一个公共非静态方法,您必须在要使用它的类中引用它。

因此,当您现在初始化 CustomAppointmentEditForm 时,您必须将 RadForm1 作为参数传递。

如果您想像以前一样调用CustomAppointmentEditForm,则构建为不带参数的第二个构造函数。

以此为例:

RadForm1 radObj;

public CustomAppointmentEditForm(RadForm1 radObj)
{
    InitializeComponent();
    this.radObj = radObj;
}

private void SomeMethod()
{
    radObj.Update_LeadInfo();
}

这是您的电话来自RadForm1

CustomAppointmentEditForm custForm = new CustomAppointmentEditForm(this);

【讨论】:

    【解决方案2】:

    首先,Update_LeadInfo() 看起来像是业务逻辑,你不应该在 UI 层有业务逻辑。如果将其移至单独的数据访问项目,则可以像这样重构代码:

    DataAccessLayer:

    class DataAdapter
    {
        public void UpdateLeads(LeadsInfo info)
        {
            using (MySqlConnection cn = new MySqlConnection(ConString))
            {
                cn.Open();
    
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = "UPDATE Leads SET CVR = ('" + info.CVR + "'), Firma = ('" + info.Firma + "')," /* ... */;
                cmd.ExecuteNonQuery();    
    
                cmd.Dispose();
                cn.Close();
            } 
        }
    }
    

    UI 层:

    class RadForm1
    {
        public RadForm1(DataAdapter adapter)
        {
            if (adapter == null)
                throw new ArgumentNullException("adapter");
    
            InitializeComponent();
            this.Adapter = adapter
        }
    
        private void Update_LeadInfo()
        {
            this.Adapter.UpdateLeads(new LeadsInfo(
                              CVR: txtCVR.Text,
                              Firma: txtFirma.Text,
                              /* ... */));
        }
    }
    
    class CustomAppointmentForm
    {
        public CustomAppointmentForm(DataAdapter adapter)
        {
            if (adapter == null)
                throw new ArgumentNullException("adapter");
    
            InitializeComponent();
            this.Adapter = adapter
        }
    
        private void Update_LeadInfo()
        {
            this.Adapter.UpdateLeads(new LeadsInfo(
                              CVR: txtCVR.Text,
                              Firma: txtFirma.Text,
                              /* ... */));
        }
    }
    

    【讨论】:

    • 是的,我也想提一下。但你更快+1
    猜你喜欢
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    相关资源
    最近更新 更多