나는 두 개의 레코드가 내 데이터베이스에 복사되는 것을 막으려하고 있지만 코드를 입력 했음에도 불구하고 여전히 도움이 될만한 일이 일어나고 있습니다!누구든지이 데이터의 복사본을 입력 할 수있는 이유를 설명 할 수 있습니까?
여기 내 코드입니다 :
if (Page.IsPostBack == true)
{
SqlConnection conn = new SqlConnection("Data Source=TOSHIBA0007\\TESTSERVER;Initial Catalog=users;Integrated Security=True");
conn.Open();
string checkcompany = "select count(*) from Company where CompanyName = @CompanyName";
SqlCommand cc = new SqlCommand(checkcompany, conn);
cc.Parameters.AddWithValue("@CompanyName", InputCompany.Text);
int temp = Convert.ToInt32(cc.ExecuteScalar().ToString());
conn.Close();
if (temp == 1)
{
Response.Write("Company is already registered! if this is incorrect please email us at [email protected]");
}
}
}
편집
같은 이름if (temp == 1)
이 실패합니다 다음이 줄을 두 개 이상의 기업이있는 경우
protected void Button1_Click(object sender, EventArgs e)
{
Guid newGUID = Guid.NewGuid();
SqlConnection Comp = new SqlConnection("Data Source=TOSHIBA0007\\TESTSERVER;Initial Catalog=users;Integrated Security=True");
{
SqlCommand xp = new SqlCommand("insert into Company(GUID, CompanyName, Password, AddressLine1, AddressLine2, AddressLine3, City, PostCode, County, Country, Email, Telephonenumber, Faxnumber)Values(@ID, @CompanyName, @Password, @AddressLine1, @AddressLine2, @AddressLine3, @City, @PostCode, @County, @Country, @Email, @TelephoneNumber, @Faxnumber)",Comp);
xp.Parameters.AddWithValue("@ID", newGUID.ToString());
xp.Parameters.AddWithValue("@CompanyName", InputCompany.Text);
xp.Parameters.AddWithValue("@Password", InputPassword.Text);
xp.Parameters.AddWithValue("@AddressLine1", InputAddress1.Text);
xp.Parameters.AddWithValue("@AddressLine2", InputAddress2.Text);
xp.Parameters.AddWithValue("@AddressLine3", InputAddress3.Text);
xp.Parameters.AddWithValue("@City", InputCity.Text);
xp.Parameters.AddWithValue("@PostCode", InputPostcode.Text);
xp.Parameters.AddWithValue("@County", InputCounty.Text);
xp.Parameters.AddWithValue("@Country", InputCountry.Text);
xp.Parameters.AddWithValue("@Email", InputEmail.Text);
xp.Parameters.AddWithValue("@TelephoneNumber", InputTelephone.Text);
xp.Parameters.AddWithValue("@Faxnumber", InputFax.Text);
Comp.Open();
xp.ExecuteNonQuery();
Comp.Close();
if (IsPostBack)
{
Response.Redirect("Registration.aspx");
}
}
}
이 코드 블록 다음에 오는 코드는 무엇입니까? – CodeCaster
경쟁 조건을 피하기 위해 'CompanyName'에 고유 제한을 두어 고유 키 위반을 포착하는 것이 가장 좋습니다. –
해당 회사 이름이 이미 하나 이상 있는지 확인하십시오. 당신은 count == 1을 체크하고있다. – user995219