我已将数据添加为:
public static void insert()
{
try
{
string connStr =
(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\test.accdb;Persist Security Info=False");
OleDbConnection conn1 = new OleDbConnection();
conn1.ConnectionString = connStr;
OleDbCommand cmd = conn1.CreateCommand();
cmd.CommandText =
"INSERT INTO patientinfo (medicareNo, title, fName, lName, gender, height, weight, age )" +
" VALUES(" + p.getMedicare() + ",'" + p.getTitle() + "','" + p.getfName() + "','" + p.getlName() +
"','" + p.getGender() + "'," + p.getheight() + "," + p.getweight() + "," + p.getAge() + ");";
conn1.Open();
cmd.ExecuteNonQuery();
//displayResult(medicareNo);
}
catch (OleDbException exp)
{
Console.WriteLine("Error");
}
displayResult(medicareNo);
}
我还有另一种读取数据的方法
public static void displayResult(int medicareNo )
{
try
{
string connStr =
(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\test.accdb;Persist Security Info=False");
OleDbConnection conn1 = new OleDbConnection();
conn1.ConnectionString = connStr;
OleDbCommand cmd1 = conn1.CreateCommand();
cmd1.CommandText = "SELECT * FROM patientinfo WHERE medicareNo = " + "" + medicareNo + "";
conn1.Open();
OleDbDataReader rdr = null;
rdr = cmd1.ExecuteReader();
if (rdr.HasRows)
{
checkvalue = true;
foreach (DataRow row in rdr.GetSchemaTable().Rows)
{
Console.Write(row["ColumnName"].ToString() + " ");
}
Console.WriteLine(" ");
while (rdr.Read())
{
Console.Write(rdr["medicareNo"].ToString());
Console.Write(" ");
Console.Write(rdr["title"].ToString());
Console.Write(" ");
Console.Write(rdr["fName"].ToString());
Console.Write(" ");
Console.Write(rdr["lName"].ToString());
Console.Write(" ");
Console.Write(rdr["gender"].ToString());
Console.Write(" ");
Console.Write(rdr["height"].ToString());
Console.Write(" ");
Console.Write(rdr["weight"].ToString());
Console.Write(" ");
Console.WriteLine(rdr["age"].ToString());
Console.WriteLine(".......................");
}
Console.WriteLine("Patient registered. Information retrieved. ");
}
else
{
checkvalue = false;
Console.WriteLine("Patient not registered. Add Patient information for registration.");
}
}
catch (OleDbException exp)
{
Console.WriteLine("error.");
}
}
问题是 displayResult() 找不到最近添加的数据,所以我添加后无法立即显示。即使在 main() 中单独调用它们也没有用。它只是转到“患者未注册........”。请有任何建议
更新:获取 medicareno 的设置方法。
public void SetMedicare(int pMedicare)
{
if (pMedicare > 0)
{
medicareNo = pMedicare;
}
else
{
Console.WriteLine("Medicare Number not valid");
}
}
public int getMedicare()
{
return medicareNo;
}