【发布时间】:2025-12-18 22:15:01
【问题描述】:
我有一个表单(编辑),其中一些文本框控件作为私有成员,具有我想从另一个类(PatientService)访问的公共属性,但我不知道如何克服“当前文本框不存在我尝试访问的 8 个控件的上下文“错误。另外,通过构造函数传递这些值是一种很好的方法吗?除了与数据库交互的 PatientService 类之外,我的项目中不能有任何其他部分。谢谢,有问题的文本框以粗体显示。
public partial class Edit : XtraForm
{
private string patientID;
private string firstName;
private string lastName;
private string address;
private string city;
private string state;
private string zipCode;
private string phone;
public Edit(string PatientID, string FirstName, string LastName, string Address, string City, string State, string ZipCode, string Phone)
{
InitializeComponent();
patientID = txtPatientID.Text;
firstName = txtFirstName.Text;
lastName = txtLastName.Text;
address = txtAddress.Text;
city = txtCity.Text;
state = txtState.Text;
zipCode = txtZipCode.Text;
phone = txtPhone.Text;
}
public string PatientID
{
get { return patientID; }
set { patientID = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string City
{
get { return city; }
set { city = value; }
}
public string State
{
get { return state; }
set { state = value; }
}
public string ZipCode
{
get { return txtZipCode.Text; }
set { txtZipCode.Text = value; }
}
public string Phone
{
get { return phone; }
set { phone = value; }
}
public void CreatePatient()
{
//SAConnection conn = new SAConnection("dsn={SQL Anywhere 10};uid=dba;pwd=sql;databasefile=C:\\Users\\Kbaker1\\Desktop\\Training1.db;");
//SACommand cmd = new SACommand("INSERT INTO patient(patient_id, first_name, last_name, address, city, state, zipcode, phone) VALUES(); ");
using (SAConnection conn = new SAConnection())
{
conn.ConnectionString = "dsn={SQL Anywhere 10};uid=dba;pwd=sql;databasefile=C:\\Users\\Kbaker1\\Desktop\\Training1.db;";
conn.Open();
using (SACommand cmd = conn.CreateCommand())
{
cmd.CommandText =
"insert into patient(\n" +
" patient_id,\n" +
" first_name,\n" +
" last_name,\n" +
" address,\n" +
" city,\n" +
" state,\n" +
" zipcode,\n" +
" phone)\n" +
" values(\n" +
" @prm_patient_id,\n" +
" @prm_first_name,\n" +
" @prm_last_name,\n" +
" @prm_address,\n" +
" @prm_city,\n" +
" @prm_state,\n" +
" @prm_zipcode,\n" +
" @prm_phone)";
cmd.Parameters.Add("@prm_patient_id", SADbType.VarChar, 80).Value = **txtPatientID.Text**;
cmd.Parameters.Add("@prm_first_name", SADbType.VarChar, 80).Value = **txtFirstName.Text**;
cmd.Parameters.Add("@prm_last_name", SADbType.VarChar, 80).Value = **txtLastName.Text**;
cmd.Parameters.Add("@prm_address", SADbType.VarChar, 80).Value = **txtAddress.Text**;
cmd.Parameters.Add("@prm_city", SADbType.VarChar, 80).Value = **txtCity.Text**;
cmd.Parameters.Add("@prm_state", SADbType.VarChar, 80).Value = **txtState.Text**;
cmd.Parameters.Add("@prm_zipode", SADbType.VarChar, 80).Value = **txtZipCode.Text**;
cmd.Parameters.Add("@prm_phone", SADbType.VarChar, 80).Value = **txtPhone.Text**;
cmd.ExecuteNonQuery();
}
}
}
好的,所以我还是有点困惑。我创建了 Patient 类,并像这样在 Edit 表单中对其进行了实例化。
公开病人拍;
public Edit(Patient patient)
{
InitializeComponent();
pat = patient;
}
当我单击“确定”按钮时,我正试图让它通过 PatientService 类中的 CreatePatient 方法插入到数据库中。
下面是 Edit Form 中调用 PatientService 类中的 CreatePatient 方法的方法:
private void btnOK_Click(object sender, EventArgs e) {
PatientService ps = new PatientService();
ps.CreatePatient();
}
我的 Patient 类如下所示:
公开课病人 {
List<Patient> patList = new List<Patient>();
private string patientID;
private string firstName;
private string lastName;
private string address;
private string city;
private string state;
private string zipCode;
private string phone;
private int classificationID;
protected object Dispose;
public Patient(string PatientID, string FirstName, string LastName, string Address, string City, string State, string ZipCode, string Phone, int ClassificationID)
{
this.patientID = PatientID;
this.firstName = FirstName;
this.lastName = LastName;
this.address = Address;
this.city = City;
this.state = State;
this.zipCode = ZipCode;
this.phone = Phone;
this.classificationID = ClassificationID;
}
public string PatientId
{
get { return patientID; }
set { patientID = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string City
{
get { return city; }
set { city = value; }
}
public string State
{
get { return state; }
set { state = value; }
}
public string ZipCode
{
get { return zipCode; }
set { zipCode = value; }
}
public string Phone
{
get { return phone; }
set { phone = value; }
}
public int ClassificationID
{
get { return classificationID; }
set { classificationID = value; }
}
public Patient(string PatientID)
{
this.patientID = PatientID;
}
public Patient()
{
}
}
}
所以考虑到我不再像一开始那样通过 Edit 构造函数传递值,我将如何利用 Patient 类来获取发送到数据库的文本框值?
【问题讨论】:
标签: c#