我有一个名为Patients with Table Conditions的数据库,它包含以下字段:
ConditionID(PK) int(50) AI,
ID_No(PK)(FK) bigint,
Date date,
Diagnosis varchar(50),
Notes varchar(max)
我有一个名为Records的C#窗体应用程序应该读取表条件中的数据,并在记录表单的tabcontrol1中的选项卡条件下显示它。 我有另一个名为Misc.cs的类,它有连接字符串(因为我的项目有很多表单,我避免在每个表单上连接重复的代码)
以下是来自Misc.cs的代码,用于连接和读取数据库中的数据:
class Misc
{
SqlConnection con = new SqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=G:\BiometricSystem\BiometricSystem\Patients.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter a = new SqlDataAdapter();
SqlDataReader dr;
public static long Id;
public void connect()
{
cmd.Connection = con;
}
public void getConditions()
{
connect();
con.Open();
cmd.CommandText = "SELECT * FROM Condition WHERE ID_No = '" + Id + "'";
dr = cmd.ExecuteReader();
Records r = new Records();
if (dr.HasRows)
{
while (dr.Read())
{
r.setdiagnosis(dr[0].ToString());
r.setDate(dr[1].ToString());
r.setNotes(dr[2].ToString());
r.printConditions();
}
}
else
System.Windows.Forms.MessageBox.Show("No records to display");
closeConn();
}
}
此代码在Records.cs中调用,如下所示:
public partial class Records : Form
{
Misc misc = new Misc();
long patID = Misc.Id;
private string diagnosis, datum ,dinota;
public Records()
{
InitializeComponent();
diagnosis = "";
datum = "";
dinota = "";
}
public void setdiagnosis(string dx)
{
diagnosis = dx;
}
public string getdiagnosis()
{
return diagnosis;
}
public void setDate(string date)
{
datum = date;
}
public string getDate()
{
return datum;
}
public void setNotes(string notes)
{
dinota = notes;
}
public string getNotes()
{
return dinota;
}
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
switch (tabControl1.SelectedIndex)
{
case 0:
{
//Nothing to do here
}
break;
case 1://tab containing where I want to display the data
misc.getConditions();
//MessageBox.Show("Conditions have been selected");
break;
}
}
public void printConditions()
{
MessageBox.Show(getdiagnosis()+"\t"+getDate()+"\t"+getNotes());
listConditions.Items.Add(getdiagnosis() + "\t" + getDate() + "\t" + getNotes());
}
}
消息框显示请求的数据,但listbox listConditions没有,我不知道是什么问题。
PS:消息框只是为了证明我确实已经返回了所请求的数据。 PPS:您是否也可以选择指定不需要解决方案绝对路径的连接字符串。