我有这个代码将数据插入数据库。我想检查要插入的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();
}
}
答案 0 :(得分:2)
尝试删除查询中的单引号,如此
cmd = new SqlCommand("Select idnum from TableVotersInfo Where idnum=@idnum", sc);
并将变量_idnum
的类型从字符串更改为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();
}
}
答案 1 :(得分:1)
您可以通过将values
更改为select
并添加where
子句,而不是两个查询。完成此操作后,您只需检查ExecuteNonQuery()
的返回值,以查看是否更新了0
或1
行。
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();
}
}
答案 2 :(得分:0)
在再次初始化命令对象之前,在 else {} 中尝试 cmd.Dispose(); 。 这适用于您的情况。
答案 3 :(得分:0)
关闭else
中的datareader对象