학생들이 성과 이름, 도시, 주 등의 일반적인 정보를 입력하는 텍스트 상자 형식이 있습니다. 이전에 양식을 작성하면 ms 액세스 데이터베이스에 중복 항목이 생깁니다. 이상적으로는 삽입 전에 동일한 레코드에서 일치하는 이름과 성을 먼저 ms 액세스 데이터베이스에서 검색하도록 코드를 사용하고 싶습니다. 입력 한 이름과 성 필드 모두에서 일치하는 레코드가 있으면 스크립트가 실행되어 "일치하는 레코드가 이미 있습니다. 계속 하시겠습니까?"라고 말합니다. "예"를 클릭하면 새 행에 레코드가 입력되고 "취소"를 클릭하면 레코드가 데이터베이스에 전혀 입력되지 않습니다.삽입 전에 중복 입력 확인 (C# .net)
나는이 코드를 시작했지만 그것이 올바른 방향인지 확실하지는 않습니다. 어떤 지침도 감사할만한 것입니다. 감사합니다.
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);
}
}
테이블의 기본 키는 무엇입니까? 확인할 수있는'StudentID' 필드가 있습니까? 그렇다면 StudentID에 입력하라는 메시지를 표시하고 존재 여부를 확인하기위한 쿼리를 실행하고 필요하다면 수행하십시오. –