我有一个文本框表格,学生可以填写他们的一般信息,例如名字和姓氏、城市、州等。有时学生不记得他们以前是否填写过表格,这会导致重复输入在 ms-access 数据库中。理想情况下,我希望代码在插入之前首先在 ms-access 数据库中搜索同一记录上匹配的名字和姓氏。如果有一条记录与输入的名字和姓氏字段都匹配,那么将运行一个脚本并说出类似“匹配的记录已经存在,你想继续吗?”之类的话。单击"is"会将记录输入新行,单击“取消”则根本不会将其输入数据库。
我开始了这段代码,但我不确定它是否是正确的方向,任何指导将不胜感激,谢谢。
using (OleDbConnection con = new OleDbConnection(constr))
using (OleDbCommand com = new OleDbCommand("SELECT COUNT(*) FROM StudentList WHERE [FName] = @FName AND [LName] = @LName", con))
{
con.Open();
using (OleDbDataReader myReader = com.ExecuteReader())
{
(This is where I am stuck)
}
}
下面是提交按钮的当前代码。
protected void btnSubmit_Click(object sender, EventArgs e)
{
{
//Preforms insert statement on click to allow additions to the database
DateTime CurrentDate;
CurrentDate = DateTime.Now;
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\sites\schoolinfo\students_dev\App_Data\Studentdb.mdb";
string cmdstr = "INSERT into StudentList(FName, LName, BDay, Gender, School, Grade, Address, APT, City, State, Zip, Email, Phone, CellPhone, ParentFName, ParentLName, ParentEmail) values(@FName, @LName, @BDay, @Gender, @School, @Grade, @Address, @APT, @City, @State, @Zip, @Email, @Phone, @CellPhone, @ParentFName, @ParentLName, @ParentEmail)";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand com = new OleDbCommand(cmdstr, con);
{
con.Open();
}
//The following fields are added from the student information to the corresponding database fields
com.Parameters.AddWithValue("@FName", txtFirstName.Text);
com.Parameters.AddWithValue("@LName", txtLastName.Text);
com.Parameters.AddWithValue("@BDay", txtBirthDate.Text);
com.Parameters.AddWithValue("@Gender", ddlGender.Text);
com.Parameters.AddWithValue("@School", txtSchool.Text);
com.Parameters.AddWithValue("@Grade", txtGrade.Text);
//The following fields are added from the contact information to the corresponding database fields
com.Parameters.AddWithValue("@Address", txtAddress.Text);
com.Parameters.AddWithValue("@APT", txtApt.Text);
com.Parameters.AddWithValue("@City", txtCity.Text);
com.Parameters.AddWithValue("@State", ddlState.Text);
com.Parameters.AddWithValue("@Zip", txtZip.Text);
com.Parameters.AddWithValue("@Email", txtEmail.Text);
com.Parameters.AddWithValue("@Phone", txtPhone.Text);
com.Parameters.AddWithValue("@CellPhone", txtCellPhone.Text);
com.Parameters.AddWithValue("@ParentFName", txtParentFName.Text);
com.Parameters.AddWithValue("@ParentLName", txtParentLName.Text);
com.Parameters.AddWithValue("@ParentEmail", txtParentEmail.Text);
com.ExecuteNonQuery();
con.Close();
//End database connection
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Student has been successfully added!')", true);
}
}
最佳答案
using (OleDbConnection con = new OleDbConnection(constr))
using (OleDbCommand com = new OleDbCommand("SELECT COUNT(*) FROM StudentList WHERE [FName] = @FName AND [LName] = @LName", con))
{
// Add your @Fname and @LName parameters here
com.Parameters.AddWithValue("@FName", firstName);
com.Parameters.AddWithValue("@LName", lastName);
con.Open();
using (OleDbDataReader myReader = com.ExecuteReader())
{
myReader.Read();
int count = myReader.GetInt32(0);
// return count > 0 or whatever to indicate that it exists
}
}
关于c# - 插入前检查重复项 (C#.net),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115045/