Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
' إسم السيرفر
Dim srvr As New Server("localhost")
' تحديد قاعدة البيانات
Dim dbase As Database = srvr.Databases("tempdb")
' StoredProcedure تحديد اسم
Dim sprocName As String = "GetName"
' التأكد من وجود نفس الاسم مسبقاً
If dbase.StoredProcedures.Contains(sprocName ) Then
MsgBox("يوجد StoredProcedure بالاسم " & sprocName & " مسبقاً", _
MsgBoxStyle.MsgBoxRight + MsgBoxStyle.MsgBoxRtlReading)
Exit Sub
End If
' StoredProcedure تعريف
Dim sproc As New StoredProcedure(dbase, sprocName )
sproc.TextMode = False
' إضافة البارامترات
sproc.Parameters.Add(New StoredProcedureParameter(sproc, "@id", DataType.Int))
' جملة إسكيوإل
sproc.TextBody = " SELECT * FROM table1 WHERE id=@id "
' StoredProcedure إنشاء
sproc.Create()
MsgBox("تم الإنشاء بنجاح")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Dim conn As New SqlConnection("Data Source=localhost; Initial Catalog=tempdb; Integrated Security=True")
'StoredProcedure طريقة التعامل مع
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Using da As New SqlDataAdapter("GetName", conn)
da.SelectCommand.Parameters.AddWithValue("@id", 2)
' StoredProcedure تحديد نوع الأمر وهو هنا
da.SelectCommand.CommandType = CommandType.StoredProcedure
Dim dt As New DataTable
da.Fill(dt)
Me.DataGridView1.DataSource = dt
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
لحذف StoredProcedure
كود :
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
' إسم السيرفر
Dim srvr As New Server("localhost")
' تحديد قاعدة البيانات
Dim dbase As Database = srvr.Databases("tempdb")
' StoredProcedure تحديد اسم
Dim sprocName As String = "GetName"
' التأكد من وجوده مسبقاً
If dbase.StoredProcedures.Contains(sprocName) Then
If MsgBox("هل بالفعل تريد حذف StoredProcedure بالاسم " & sprocName, _
MsgBoxStyle.YesNo + MsgBoxStyle.MsgBoxRight + MsgBoxStyle.MsgBoxRtlReading) = MsgBoxResult.Yes Then
dbase.StoredProcedures.Item(sprocName).Drop()
MsgBox("تم الحذف بنجاح")
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub