|
 |
|
มี ต.ย. โค้ด ที่ไม่ให้เพิ่มข้อมูลซ้ำชื่อ - นามสกุล (ชื่อเหมือน แต่นามสกุลไม่เหมือน เพิ่มได้ แต่ถ้าเหมือน ทั้ง 2 จะไม่ให้เพิ่ม) |
|
 |
|
|
 |
 |
|
มี ต.ย. โค้ด ที่ไม่ให้เพิ่มข้อมูลซ้ำชื่อ - นามสกุล (ชื่อเหมือน แต่นามสกุลไม่เหมือน เพิ่มได้ แต่ถ้าเหมือน ทั้ง 2 จะไม่ให้เพิ่ม)
นี่โค้ดผมควรจะเพิ่มตรงไหน
Code (C#)
private void AddCustomer()
{
string ConnStr = Properties.Settings.Default.DatabaseCarserviceConnectionString;
OleDbConnection MyConn = new OleDbConnection(ConnStr);
MyConn.Open();
int intNumRows = 0;
string strSQL = "SELECT COUNT(*) FROM Customer WHERE Cus_Email = '" + this.txtAddEmail.Text + "' ";
OleDbCommand Cmd = new OleDbCommand(strSQL, MyConn);
intNumRows = Convert.ToInt32(Cmd.ExecuteScalar());
if (intNumRows > 0)
{
MessageBox.Show("Email นี้มีผู้ใช้แล้ว");
txtAddEmail.Text = "";
}
else
{
string insertSQL = "INSERT INTO Customer (Cus_ID,Cus_Name,Cus_Lastname,Cus_Address,Cus_Tel,Cus_Email) values('" + txtAddID.Text + "','"
+ txtAddName.Text + "','" + txtAddLastname.Text + "','" + txtAddAddress.Text + "','" + txtAddEmail.Text + "','" + txtAddTel.Text + "')";
try
{
OleDbCommand cmd = new OleDbCommand(insertSQL, MyConn);
MyConn.Open();
cmd.ExecuteNonQuery();
MyConn.Close();
string strCmd = "Select * from Customer ORDER BY Cus_ID";
OleDbDataAdapter da = new OleDbDataAdapter(strCmd, ConnStr);
DataSet ds = new DataSet();
MyConn.Open();
da.Fill(ds);
gridControl1.DataSource = ds.Tables[0];
MyConn.Close();
{
MessageBox.Show("เพิ่มข้อมูลเรียบร้อย", "Add", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
txtAddName.Text = "";
txtAddID.Text = "";
txtAddAddress.Text = "";
txtAddLastname.Text = "";
txtAddTel.Text = "";
txtAddEmail.Text = "";
}
}
catch
{
MessageBox.Show("ID นี้มีอยู่ในตารางแล้ว", "Add", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
txtAddID.Text = "";
}
}
}
Tag : .NET, Ms Access, Win (Windows App), C#, Windows

|
ประวัติการแก้ไข 2012-08-22 00:54:13
|
 |
 |
 |
 |
Date :
2012-08-22 00:49:33 |
By :
peteyothin |
View :
1165 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมไม่เก่งนะพอเดาโค๊ดถูก
string strSQL = "SELECT COUNT(*) FROM Customer WHERE Cus_Email = '" + this.txtAddEmail.Text + "' ";
SELECT * FROM Customer
ตรง where Cus_Email ให้เปลี่ยนเป็น Cus_Name and Cus_Lastname ตามที่ต้องการ
|
ประวัติการแก้ไข 2012-08-22 02:08:52
 |
 |
 |
 |
Date :
2012-08-22 02:07:20 |
By :
weaned |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนแรกจะทำแบบที่คุณบอก แต่ผมคิดว่ามันน่าจะเป็น ซ้ำอันไหน มันก็เตือนอะสิ เด๋วผมขอลองก่อนนะครับ ขอขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2012-08-22 02:09:42 |
By :
peteyothin |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ได้เลยครับ ขอบคุณครับ ว่าแต่ว่า ผมจะเพิ่มให้ Email กะ Tel ด้วยทำยังไง กรณี Email กะ Tel ซ้ำกะ ของคนอื่นๆ น่ะครับ โดยทำทีเดียวพร้อมกัน ทั้ง ชื่อ นามสกุล อีเมล เบอร์โทร ขอบคุณครับ
|
ประวัติการแก้ไข 2012-08-22 02:45:09
 |
 |
 |
 |
Date :
2012-08-22 02:40:19 |
By :
peteyothin |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จากโค้ดของคุณ Q
string strSQL = "SELECT COUNT(*) FROM Customer WHERE Cus_Email = '" + this.txtAddEmail.Text + "' "
ให้ทำการ AND เพิ่มค่ะ แต่จะเช็คพร้อมกันหมดทั้ง 3 อย่างที่คุณ pete ต้องการนะค่ะ
string strSQL = "SELECT COUNT(*) FROM Customer WHERE Cus_Email = '" + this.txtAddEmail.Text + "' AND Cus_Email = '" + this.txtAddEmail.Text + "' "
แต่ถ้าจะให้เช็ค ว่าซ้ำกับคนอื่น แนะนำให้เขียนเป็น Sub ไว้ แล้วเอามาเรียกใช้ทีละอันตอน บันทึก ค่ะ
แต่อาจจะมีวิธีที่ดีกว่านี้นะค่ะ ลองดูค่ะ
|
 |
 |
 |
 |
Date :
2012-08-22 09:18:32 |
By :
sinlukploy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ คุณ ~นู๋ซิน~ นะครับ ตอนนี้ได้แล้ว
Code (C#)
string strSQL = "SELECT COUNT(*) FROM Customer WHERE Cus_Email = '" + txtAddEmail.Text + "' OR Cus_Tel = '" + txtAddTel.Text +"' OR Cus_Name ='" + txtAddName.Text + "' AND Cus_Lastname='" + txtAddLastname.Text + "'";
|
 |
 |
 |
 |
Date :
2012-08-22 16:13:22 |
By :
peteyothin |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|