首先,我想指出,我没有运气就找到了解决这个问题的方法。
基本上我在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);
}
}
我知道有一个简单的解决方案,提前谢谢
答案 0 :(得分:2)
编辑: 我不会在那里使用这种方法。
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);
答案 1 :(得分:1)
首先,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,
/* ... */));
}
}