消除数据库中的重复项

时间:2012-12-25 23:45:07

标签: c# ms-access oledb

我有以下代码用于从文件中读取并填充我的数据库。除了所有记录都插入两次外,一切正常。

OleDbCommand c = new OleDbCommand();
c.Connection = GetConnection();
c.CommandText =
    @"CREATE TABLE patients (
        patientid AUTOINCREMENT PRIMARY KEY,
        firstlastname CHAR(50),
        birthdate CHAR(50),
        birthplace CHAR(50),
        gender CHAR(1),
        bloodtype CHAR(50),
        telnum CHAR(50),
        address CHAR(255)
    )";
c.ExecuteNonQuery();

string info = "";
string name = "";
string date = "";
string place = "";
string blood = "";
string num = "";
string address = "";
//Read from file and insert values to patient table
StreamReader myReader = new StreamReader("myPath/patient.txt");
while (true)
{
    info = myReader.ReadLine();
    if (info == null) break;

    if (info.Trim() != String.Empty)
    {
        string[] words = info.Split(',');
        if (words.Length == 6)
        {
            name = words[0].Trim();
            date = words[1].Trim();
            place = words[2];
            blood = words[3];
            num = words[4];
            address = words[5];
        }
    }

    string cmdText = "INSERT INTO patients (" +
      "firstlastname, birthdate, birthplace, bloodtype, telnum, address) " +
      "VALUES (?,?,?,?,?,?)";

    using (OleDbConnection cn = GetConnection())
    {

        using (OleDbCommand cmd = new OleDbCommand(cmdText, cn))
        {
            cmd.Parameters.AddWithValue("name", name);
            cmd.Parameters.AddWithValue("date", date);
            cmd.Parameters.AddWithValue("place", place);
            cmd.Parameters.AddWithValue("blood", blood);
            cmd.Parameters.AddWithValue("num", num);
            cmd.Parameters.AddWithValue("address", address);
            cmd.ExecuteNonQuery();
        }
    }


}

我注意到了那条线 if (info.Trim() != String.Empty)

每隔一段时间返回“false”,因此记录被插入两次,但不知道如何解决这个问题。

我如何消除这些重复? 感谢

编辑:文件内容如下所示,显示姓名,日期,城市,血型,手机号码和地址:

Mehtap Selim,1985年6月11日,Kyrenia,FBRh +,05617584509,Yasemin St. No:48 Edremit Kyrenia KKTC

1 个答案:

答案 0 :(得分:2)

它看起来像是你的代码结构仍然允许写入你的数据库,即使条件语句是假的,并且因为数据仍然在你之前写入的变量中,所以它再次写入它们。尝试将数据库写入相同的条件。至于为什么每隔一段时间它就会出现错误,我们需要查看你文件的内容。

if (info.Trim() != String.Empty)
{
    string[] words = info.Split(',');
    if (words.Length == 6)
    {
        name = words[0].Trim();
        date = words[1].Trim();
        place = words[2];
        blood = words[3];
        num = words[4];
        address = words[5];
    }


    string cmdText = "INSERT INTO patients (" +
       "firstlastname, birthdate, birthplace, bloodtype, telnum, address) " +
       "VALUES (?,?,?,?,?,?)";

    using (OleDbConnection cn = GetConnection())
    {
        using (OleDbCommand cmd = new OleDbCommand(cmdText, cn))
        {
            cmd.Parameters.AddWithValue("name", name);
            cmd.Parameters.AddWithValue("date", date);
            cmd.Parameters.AddWithValue("place", place);
            cmd.Parameters.AddWithValue("blood", blood);
            cmd.Parameters.AddWithValue("num", num);
            cmd.Parameters.AddWithValue("address", address);
            cmd.ExecuteNonQuery();
        }
    }
}