最近存储的数据无法通过其他方法读取

时间:2013-09-13 02:27:29

标签: c# database ms-access

我已将数据添加为:

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()中单独调用它们也没有用。它只是“患者未注册..................”。请提出任何建议

更新:获取医疗保健的设置方法。

public void SetMedicare(int pMedicare)
{
    if (pMedicare > 0)
    {
        medicareNo = pMedicare;
    }
    else
    {
        Console.WriteLine("Medicare Number not valid");
    }
}
public int getMedicare()
{
    return medicareNo;
}

2 个答案:

答案 0 :(得分:0)

您的SELECT语句是问题:

"SELECT * FROM patientinfo WHERE medicareNo = " + "" + medicareNo + ""

它将medicareNo作为字符串进行比较,而不是int

将您的查询更改为:

"SELECT * FROM patientinfo WHERE medicareNo = " + medicareNo

答案 1 :(得分:0)

使用以下

更改displayResult(medicareNo);
displayResult(p.getMedicare());

我也会改变你的方法如下

    public static void insert()
    {
        try
        {
            string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\test.accdb;Persist Security Info=False";
            string commandText = "INSERT INTO patientinfo (medicareNo, title, fName, lName, gender, height, weight, age )" +
                                " VALUES(?, ?, ?, ?, ?, ?, ?, ?)";

            using (OleDbConnection con = new OleDbConnection(connStr))
            using (OleDbCommand cmd = new OleDbCommand(commandText, con))
            {
                cmd.Parameters.AddWithValue("@medicareNo", p.getMedicare());
                cmd.Parameters.AddWithValue("@title", p.getTitle());
                cmd.Parameters.AddWithValue("@fName", p.getfName());
                cmd.Parameters.AddWithValue("@lName", p.getlName());
                cmd.Parameters.AddWithValue("@gender", p.getGender());
                cmd.Parameters.AddWithValue("@height", p.getheight());
                cmd.Parameters.AddWithValue("@weight", p.getweight());
                cmd.Parameters.AddWithValue("@age", p.getAge());
                con.Open();
                int ret = cmd.ExecuteNonQuery();
                if(ret ==1)
                    Console.WriteLine("Insert Successful");
            }

            displayResult(p.getMedicare());
        }
        catch (OleDbException exp)
        {
            Console.WriteLine("Error");
        }

    }

    public static void displayResult(int medicareNo)
    {
        try
        {
            string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\test.accdb;Persist Security Info=False";
            string commandText = "SELECT * FROM patientinfo WHERE medicareNo = ?";
            using (OleDbConnection con = new OleDbConnection(connStr))
            using (OleDbCommand cmd = new OleDbCommand(commandText, con))
            {
                cmd.Parameters.AddWithValue("@medicareNo", medicareNo);
                con.Open();
                using (OleDbDataReader rdr = cmd.ExecuteReader())
                {
                    if (rdr.HasRows)
                    {
                        foreach (DataRow row in rdr.GetSchemaTable().Rows)
                        {
                            Console.Write(row["ColumnName"].ToString() + " ");
                        }
                        Console.WriteLine(" ");
                        while (rdr.Read())
                        {
                            string str = string.Format("{0} {1} {2} {3} {4} {5} {6} {7}", 
                                rdr["medicareNo"], rdr["title"], rdr["fName"], rdr["lName"],
                                rdr["gender"], rdr["height"], rdr["weight"], rdr["age"]);

                            Console.WriteLine(str);
                        }
                        Console.WriteLine("Patient registered. Information retrieved. ");
                    }
                    else
                    {
                        Console.WriteLine("Patient not registered. Add Patient information for registration.");
                    }
                }
            }
        }
        catch (OleDbException exp)
        {
            Console.WriteLine("error.");
        }
    }