2016-08-12 2 views
2

현재 asp.net 웹 사이트가 있습니다. 그러나 나는 등록 페이지에서 사용자가 동일한 사용자 이름을 사용하여 여러 번 등록 할 수 있음을 확인했습니다. 같은 사용자 이름을 사용하여 여러 번 등록하지 못하도록하는 방법이 있는지 궁금합니다.MySQL 데이터베이스에 중복 데이터 값이 나타나지 않도록하는 방법

데이터베이스에서 기본 키를 가질 수있는 한 개의 열을 설정할 수 있다는 것을 알고 있습니다 ...하지만 기본 키를 가질 열이 여러 개인 경우 어떻게해야합니까?

내 코드 삽입 ....

MySqlCommand command = mcon.CreateCommand(); 
    mcon.Open(); 
    command.CommandText = "insert into pointofcontact (FirstName, LastName,  EmailAddress, ContactNumber, BackupContactNumber, Address, Gender, Username, Password, Status, ProfilePic) values(?firstname, ?lastname, ?emailaddress, ?contactnumber, ?backupcontactnumber, ?address, ?gender, ?username, ?password, ?status, ?image)"; 
    command.Parameters.AddWithValue("?firstname", firstname); 
    command.Parameters.AddWithValue("?lastname", lastname); 
    command.Parameters.AddWithValue("?emailaddress", email); 
    command.Parameters.AddWithValue("?contactnumber", mobileNumber); 
    command.Parameters.AddWithValue("?backupcontactnumber", backupNumber); 
    command.Parameters.AddWithValue("?address", homeAddress); 
    command.Parameters.AddWithValue("?gender", gender); 
    command.Parameters.AddWithValue("?username", username); 
    command.Parameters.AddWithValue("?password", password); 
    command.Parameters.AddWithValue("?status", status); 
    command.Parameters.AddWithValue("?image", imageName); 
    command.ExecuteNonQuery(); 
    mcon.Close(); 

    MySqlDataReader reader = null; 
    mcon.Open(); 
    MySqlCommand command2 = mcon.CreateCommand(); 
    command2.CommandText = "select * from pointofcontact where Username = ?username"; 
    command2.Parameters.AddWithValue("?username", tbUsername.Text); 
    reader = command2.ExecuteReader(); 
    if (reader != null && reader.HasRows) 
    { 
     lblValidate.Text = "Username already exists."; 
    } 

    Response.Redirect("IndexAfterLogin1.aspx"); 

사용자 입력이 내 MySQL 데이터베이스에 삽입 할 수있는 데이터이지만, 입력 된 데이터는 사용자 이름 및 이메일 필드에 반복 될 수있다 다른 사용자를 위해, 나는 그런 일이 일어나기를 원하지 않습니다.

+0

의 사용 가능한 복제 [? 내가 MySQL을 여러 열에 대한 고유 제한 조건을 지정하려면 어떻게 (http://stackoverflow.com/questions/635937/how-do-i-specify-unique-constraint-for-multiple-columns-in-mysql) –

+1

최소한 데이터베이스에 제약 조건을 추가하면 중복 삽입을 시도 할 때 예외가 발생합니다. –

답변

0

이를 위해 당신은 그것을

0

가 기본 또는 고유 키와 같은 필드 이름을 확인의 삽입하기 전에 데이터베이스에서 확인 다른 테이블 create a functionunique key을 추가 할 수 있습니다. 다음과 같이 해당 필드

0

주문 코드 중복 항목을 허용하지 않습니다 :

try 
{ 
MySqlDataReader reader = null; 
mcon.Open(); 
MySqlCommand command2 = mcon.CreateCommand(); 
command2.CommandText = "select * from pointofcontact where Username = ?username"; 
command2.Parameters.AddWithValue("?username", tbUsername.Text); 
reader = command2.ExecuteReader(); 
if (reader != null && reader.HasRows) 
{ 
    lblValidate.Text = "Username already exists."; 
    **return;** 
} 
else 
{ 
    MySqlCommand command = mcon.CreateCommand(); 
    command.CommandText = "insert into pointofcontact (FirstName, LastName,  EmailAddress, ContactNumber, BackupContactNumber, Address, Gender, Username, Password, Status, ProfilePic) values(?firstname, ?lastname, ?emailaddress, ?contactnumber, ?backupcontactnumber, ?address, ?gender, ?username, ?password, ?status, ?image)"; 
    command.Parameters.AddWithValue("?firstname", firstname); 
    command.Parameters.AddWithValue("?lastname", lastname); 
    command.Parameters.AddWithValue("?emailaddress", email); 
    command.Parameters.AddWithValue("?contactnumber", mobileNumber); 
    command.Parameters.AddWithValue("?backupcontactnumber", backupNumber); 
    command.Parameters.AddWithValue("?address", homeAddress); 
    command.Parameters.AddWithValue("?gender", gender); 
    command.Parameters.AddWithValue("?username", username); 
    command.Parameters.AddWithValue("?password", password); 
    command.Parameters.AddWithValue("?status", status); 
    command.Parameters.AddWithValue("?image", imageName); 
    command.ExecuteNonQuery(); 
    } 
} 
catch 
{ 
    .... 
} 
finally 
{ 
mycon.Close(); 
} 
+0

코딩의 '캐치'부분에 무엇을 입력해야합니까? :) – MrStutterz

+0

캐치 시도 마지막 블록이 항상 선호됩니다. –

관련 문제