If (New SqlClient.SqlCommand("select case when count(*) > 0 then 'true' else 'false' end from xxxx", lcConn)).ExecuteScalar Then MsgBox("ไม่สามารถลบข้อมูลได้ ") Else 'ToDo : ลบข้อมูลตรงนี้ End If
//แบบที่ 1 : (ตัวอย่างเพื่อความเข้าใจ) cmdBuilder.AppendLine("DECLARE @DELETABLE_FLAG INT ;") ; cmdBuilder.AppendLine("SET @DELETABLE_FLAG = 0 ;") ; cmdBuilder.AppendLine() cmdBuilder.AppendLine("SELECT @DELETABLE_FLAG = COUNT([table02].[id]) ") ; cmdBuilder.AppendLine("FROM [table01]") ; cmdBuilder.AppendLine(" RIGHT JOIN [table02]") ; cmdBuilder.AppendLine(" ON [table01].[id]=[table02].[UIDJob] ;") ; cmdBuilder.AppendLine("WHERE [table01].[id]=@VAR_ID;") ; cmdBuilder.AppendLine() cmdBuilder.AppendLine("IF (@DELETABLE_FLAG=0)") ; cmdBuilder.AppendLine("BEGIN ") cmdBuilder.AppendLine(" DELETE * FROM [table01] WHERE [table01].[id]=@VAR_ID;") ; cmdBuilder.AppendLine(" SET@DELETABLE_FLAG = 1; ") cmdBuilder.AppendLine("END") cmdBuilder.AppendLine("ELSE") cmdBuilder.AppendLine(" SET@DELETABLE_FLAG = 0; ") cmdBuilder.AppendLine() cmdBuilder.AppendLine("SELECT @DELETABLE_FLAG;") ; //แบบสั้น cmdBuilder.AppendLine("DELETE FROM [table01]"); cmdBuilder.AppendLine("FROM [table01]"); cmdBuilder.AppendLine(" RIGHT JOIN [table02]"); cmdBuilder.AppendLine(" ON [table01].[id]=[table02].[UIDJob] ;"); cmdBuilder.AppendLine("WHERE ([table01].[id]=@VAR_ID)"); cmdBuilder.AppendLine(" AND ([table02].[id] IS NULL) ;");"); cmdBuilder.AppendLine("-- ([table02].[id] IS NULL) << NO RELATED ROW ACQUIRE");
System.Text.StringBuilder cmdBuilder = new System.Text.StringBuilder() ; // เอา code จากแบบที่ 1 หรือ 2 มาแปะตรงนี้ int totalEffectRec = 0 ; Using (SqlConnection myConn = new SqlConnection(uConnectionString)) { SqlCommand myCommand = new myCommand( cmdBuilder.ToString() ,myConn ); myCommand.Parameters.AddWithValue("@VAR_ID" ,uID) ; try { myConn.Open(); totalEffectRec = myCommand.ExecuteNonQuery() ; } catch (Exception Ex) { totalEffectRec = -1 ; // error trap here } finally { if(myConn!=null) myConn.Close(); } } switch (totalEffectRec) { case 0 : // ลบไม่ได้้ break ; case -1 " // Error break ; default : // ลบแล้ว break; }
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง