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

所以基本上我想从CustomAppointmentEditForm调用Radform1女巫中的Mysql函数

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


我知道有一个简单的解决方案,谢谢

最佳答案

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

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

使用参数表创建另一个类以访问数据库,例如

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

关于c# - 来自其他形式的C#调用函数(radform),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44368827/

10-13 05:53