0

我有这个将数据插入数据库的代码。我想检查要插入的 ID 是否已经在数据库中。但如果它不在数据库中,则应将其插入。但是有一些错误,你能帮我解决一下吗?

    public void Add()
    {
        sc.Open();
        try
        {
            cmd = new SqlCommand("Select idnum from TableVotersInfo Where idnum=@idnum", sc);

            cmd.Parameters.AddWithValue("@idnum", _idnum);

            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.Read() == true)
            {
                MessageBox.Show("ID number already exist!");
                rd.Close();
            }
            else
            {
                cmd = new SqlCommand("INSERT INTO TableVotersInfo (Education, idnum, FirstName, MiddleName, LastName, SchoolYear, ControlNum, VResult) VALUES (@ed, @idnum, @firstname, @middlename, @lastname, @schoolyear, @controlnum, 'Not Voted');", sc);
                cmd.Parameters.AddWithValue("@id", _id);
                cmd.Parameters.AddWithValue("@ed", _ed);
                cmd.Parameters.AddWithValue("@idnum", _idnum);
                cmd.Parameters.AddWithValue("@firstname", _firstname);
                cmd.Parameters.AddWithValue("@middlename", _middlename);
                cmd.Parameters.AddWithValue("@lastname", _lastname);
                cmd.Parameters.AddWithValue("@schoolyear", _schoolyear);
                cmd.Parameters.AddWithValue("@controlnum", _controlnum);

                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Stored Successfully!");
                FAddVoters._cleardata = cleardata;
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }
    }

编辑 我现在的错误是There is already an open DataReader associated with this command which must be closed first..

更新

    public void Update()
    {
        sc.Open();

        try
        {
            cmd = new SqlCommand("UPDATE TableVotersInfo SET Education=@ed, idnum=@idnum, FirstName=@firstname, MiddleName=@middlename, LastName=@lastname, SchoolYear=@schoolyear, ControlNum=@controlnum WHERE id=@id", sc);
            cmd.Parameters.AddWithValue("@id", _id);
                cmd.Parameters.AddWithValue("@ed", _ed);
                cmd.Parameters.AddWithValue("@idnum", _idnum);
                cmd.Parameters.AddWithValue("@firstname", _firstname);
                cmd.Parameters.AddWithValue("@middlename", _middlename);
                cmd.Parameters.AddWithValue("@lastname", _lastname);
                cmd.Parameters.AddWithValue("@schoolyear", _schoolyear);
                cmd.Parameters.AddWithValue("@controlnum", _controlnum);

            int res = cmd.ExecuteNonQuery();
            if (res > 0)
            {
                MessageBox.Show("Successfully Updated!");
                FAddVoters._cleardata = cleardata;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }
    }
4

4 回答 4

2

尝试像这样删除查询中的单引号

cmd = new SqlCommand("Select idnum from TableVotersInfo Where idnum=@idnum", sc);

并将变量类型_idnum从 string 更改为 int

要解决插入错误,请尝试此代码

public void Add()
{
    sc.Open();
    try
    {
        cmd = new SqlCommand("Select idnum from TableVotersInfo Where idnum=@idnum", sc);
        cmd.Parameters.AddWithValue("@idnum", _idnum);

        if (cmd.ExecuteScalar() != null)
        {
            MessageBox.Show("ID number already exist!");
        }
        else
        {
            cmd = new SqlCommand("INSERT INTO TableVotersInfo (Education, idnum, FirstName, MiddleName, LastName, SchoolYear, ControlNum, VResult) VALUES (@ed, @idnum, @firstname, @middlename, @lastname, @schoolyear, @controlnum, 'Not Voted');", sc);
            cmd.Parameters.AddWithValue("@id", _id);
            cmd.Parameters.AddWithValue("@ed", _ed);
            cmd.Parameters.AddWithValue("@idnum", _idnum);
            cmd.Parameters.AddWithValue("@firstname", _firstname);
            cmd.Parameters.AddWithValue("@middlename", _middlename);
            cmd.Parameters.AddWithValue("@lastname", _lastname);
            cmd.Parameters.AddWithValue("@schoolyear", _schoolyear);
            cmd.Parameters.AddWithValue("@controlnum", _controlnum);

            cmd.ExecuteNonQuery();
            MessageBox.Show("Data Stored Successfully!");
            FAddVoters._cleardata = cleardata;
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        sc.Close();
    }
}
于 2013-10-30T06:10:11.580 回答
1

您可以通过更改values为 aselect并添加一个where子句,而不是两个查询。之后,您只需要检查返回的值ExecuteNonQuery()以查看01行是否已更新。

public void Add()
{
    sc.Open();
    try
    {
        using(cmd = new SqlCommand(@"INSERT INTO TableVotersInfo (Education, idnum, FirstName, MiddleName, LastName, SchoolYear, ControlNum, VResult)
                                        SELECT @ed, @idnum, @firstname, @middlename, @lastname, @schoolyear, @controlnum, 'Not Voted' 
                                        WHERE @idNum NOT IN (SELECT idNum FROM TableVotersInfo);", sc))
        {
            cmd.Parameters.AddWithValue("@id", _id);
            cmd.Parameters.AddWithValue("@ed", _ed);
            cmd.Parameters.AddWithValue("@idnum", _idnum);
            cmd.Parameters.AddWithValue("@firstname", _firstname);
            cmd.Parameters.AddWithValue("@middlename", _middlename);
            cmd.Parameters.AddWithValue("@lastname", _lastname);
            cmd.Parameters.AddWithValue("@schoolyear", _schoolyear);
            cmd.Parameters.AddWithValue("@controlnum", _controlnum);

            var rowsAffected = cmd.ExecuteNonQuery();
            if(rowsAffected == 0)
            {
                MessageBox.Show("ID number already exist!");
            }
            else
            {
                MessageBox.Show("Data Stored Successfully!");
                FAddVoters._cleardata = cleardata;
            }
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        sc.Close();
    }
}
于 2013-10-30T06:32:55.613 回答
0

试试cmd.Dispose(); else {}再次初始化命令对象之前。这应该适用于您的情况。

于 2013-10-30T06:24:20.210 回答
0

else 也关闭 datareader 对象

于 2013-10-30T06:26:48.413 回答