منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب
لدي كود للإضافة أريد كود للتعديل وكود للحذف - نسخة قابلة للطباعة

+- منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب (http://vb4arb.com/vb)
+-- قسم : قسم لغة الفيجوال بيسك VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=182)
+--- قسم : قسم اسئلة VB.NET (http://vb4arb.com/vb/forumdisplay.php?fid=183)
+--- الموضوع : لدي كود للإضافة أريد كود للتعديل وكود للحذف (/showthread.php?tid=50578)



لدي كود للإضافة أريد كود للتعديل وكود للحذف - F.H.M - 05-11-24

الأخوة الأعزاء

السلام عليكم ورحمة الله وبركاته ،،،

لدي هذا الكود وهو خاص بالإضافة ( Insert )

أريد كود التعديل وكود للحذف بحيث يتوافق مع هذا الكود وكود الحذف أريده يحذف المجلد الجديد الذي بداخله الصورة ولا يحذف المجلد الرئيسي وهو images

كود :
Dim folderPath As String = Application.StartupPath & "\Images\"
If Not Directory.Exists(folderPath & "\" & ID.ToString) Then
    Directory.CreateDirectory(folderPath & "\" & ID.ToString)
End If
Dim mname As String = ID.ToString()

Dim imagePath As String = folderPath & ID.ToString() & "\" & mname & ".jpg"

 Dim ms As New MemoryStream
 Car_Doc.Image.Save(ms, Car_Doc.Image.RawFormat)
 IO.File.WriteAllBytes(imagePath, ms.ToArray())

.Parameters.AddWithValue("@Car_Doc", OleDbType.VarChar).Value = "images\" & ID.ToString() & "\" & mname & ".jpg"

وشكراً جزيلاً للجميع


RE: لدي كود للإضافة أريد كود للتعديل وكود للحذف - Zuhare - 07-11-24

كود الحذف المجلد بما فيه ولا يحذف المجلد الرئيسي
PHP كود :
IO.Directory.Delete(folderPath ID.ToString(), True



RE: لدي كود للإضافة أريد كود للتعديل وكود للحذف - F.H.M - 07-11-24

(07-11-24, 08:03 PM)Zuhare كتب : كود الحذف المجلد بما فيه ولا يحذف المجلد الرئيسي
PHP كود :
IO.Directory.Delete(folderPath ID.ToString(), True

الله يعطيك العافية أخ زهير

المطلوب ليس فقط حذف الصورة والمجلد بينما أيضاً حذف البيانات من قاعدة البيانات


RE: لدي كود للإضافة أريد كود للتعديل وكود للحذف - Zuhare - 09-11-24

حذف البيانات من قاعدة البيانات بيتعمل عادي عن طريق Delete مع Where لرقم ID - لو الكود كامل او فيه مشروع صغير افضل كان ممكن احاول اعدله


RE: لدي كود للإضافة أريد كود للتعديل وكود للحذف - F.H.M - 09-11-24

الله يعطيك العافية أخ زهير

هذا كود الإضافة
كود :
Public Sub Insert_Car_Arshef(ByVal ID As Long, ByVal Car_Number As String, ByVal Enwan_Ketab As String, ByVal Date_Ketab As String, Car_Doc As PictureBox)
   Dim folderPath As String = Application.StartupPath & "\Images\"
   If Not Directory.Exists(folderPath & "\" & ID.ToString) Then
       Directory.CreateDirectory(folderPath & "\" & ID.ToString)
   End If
   Dim mname As String = ID.ToString

   Dim imagePath As String = folderPath & ID.ToString() & "\" & mname & ".jpg"



   Dim ms As New MemoryStream
   Car_Doc.Image.Save(ms, Car_Doc.Image.RawFormat)
   IO.File.WriteAllBytes(imagePath, ms.ToArray())

   Dim Cmd As New OleDbCommand
   With Cmd
       .Connection = con
       .CommandType = CommandType.Text
       .CommandText = "Insert Into Car_Arshef ( ID,Car_Number,Enwan_Ketab,Date_Ketab,Car_Doc)values(@ID,@Car_Number,@Enwan_Ketab,@Date_Ketab,@Car_Doc)"
       .Parameters.Clear()
       .Parameters.AddWithValue("@ID", OleDbType.Integer).Value = ID
       .Parameters.AddWithValue("@Car_Number", OleDbType.VarChar).Value = Car_Number
       .Parameters.AddWithValue("@Enwan_Ketab", OleDbType.VarChar).Value = Enwan_Ketab
       .Parameters.AddWithValue("@Date_Ketab", OleDbType.VarChar).Value = Date_Ketab
       .Parameters.AddWithValue("@Car_Doc", OleDbType.VarChar).Value = "images\" & ID.ToString() & "\" & mname & ".jpg"

   End With

   If con.State = ConnectionState.Open Then con.Close()
   con.Open()
   Cmd.ExecuteNonQuery()
   con.Close()
   MsgBox("تم إضافة السجل بنجاح", MsgBoxStyle.Information, "حفظ")
   Cmd = Nothing
End Sub


هذا كود التعديل
كود :
Public Sub Update_Car_Arshef(ByVal Car_Number As String, ByVal Enwan_Ketab As String, ByVal Date_Ketab As String, ByVal Car_Doc As PictureBox, ByVal IDW As Long)

   Try

       Dim folderPath As String = Path.Combine(Application.StartupPath, "Images")

       If Not Directory.Exists(folderPath) Then
           Directory.CreateDirectory(folderPath)
       End If

       Dim imagePath As String = Path.Combine(folderPath, IDW.ToString() & ".jpg")

       If File.Exists(imagePath) Then
           File.Delete(imagePath)
       End If

       Using bmpCopy As New Bitmap(Car_Doc.Image)
           bmpCopy.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg)
       End Using


       Dim Cmd As New OleDbCommand
       With Cmd

           .Connection = con
           .CommandType = CommandType.Text
           .CommandText = "Update Car_Arshef Set Car_Number = @Car_Number,Enwan_Ketab = @Enwan_Ketab,Date_Ketab = @Date_Ketab,Car_Doc = @Car_Doc Where ID = @ID"
           .Parameters.Clear()
           .Parameters.AddWithValue("@Car_Number", OleDbType.VarWChar).Value = Car_Number
           .Parameters.AddWithValue("@Enwan_Ketab", OleDbType.VarWChar).Value = Enwan_Ketab
           .Parameters.AddWithValue("@Date_Ketab", OleDbType.VarWChar).Value = Date_Ketab
           .Parameters.AddWithValue("@Car_Doc", "images\" & IDW.ToString() & ".jpg")
           .Parameters.AddWithValue("@ID", IDW)
           
       End With
       If con.State = ConnectionState.Open Then con.Close()
       con.Open()
       Cmd.ExecuteNonQuery()
       con.Close()

       MsgBox("تم تعديل السجل بنجاح", MsgBoxStyle.Information, "تعديل")
   Catch ex As Exception
       MsgBox("حدث خطأ أثناء محاولة تعديل السجل" & ex.Message, MsgBoxStyle.Critical)
 
   End Try
End Sub

وهذا كود الحذف
كود :
Public Sub Delete_Car_Arshef(ByVal dgv_Car_Arshef As DataGridView)
   Dim Position As Integer = dgv_Car_Arshef.CurrentRow.Index
   Dim ID_Position As Integer = dgv_Car_Arshef.Rows(Position).Cells("ID").Value
   Dim Cmd As New OleDbCommand
   With Cmd
       .Connection = con
       .CommandType = CommandType.Text
       .CommandText = "Delete  From Car_Arshef Where ID = @ID"
       .Parameters.Clear()
       .Parameters.AddWithValue("@ID", OleDbType.Integer).Value = ID_Position
   End With
   If con.State = 1 Then con.Close()
   con.Open()
   Cmd.ExecuteNonQuery()
   con.Close()
   MsgBox("تم حذف السجل.", MsgBoxStyle.Information, "حذف")
   Cmd = Nothing
End Sub