10-04-23, 03:35 AM
(آخر تعديل لهذه المشاركة : 10-04-23, 03:42 AM {2} بواسطة عبدالله الدوسري.)
وعليكم السلام
هذة طريقة إذا كنت تستخدم SQL SERVER 2012 فأعلى ، بالتحكم في الإجراء المخزن بواسطة العبارة THROW
SQL :
VB.NET :
ملاحظات :
VB.NET
SQL :
ملاحظة :
في الإجراء المخزن : THROW وظيفتها ترمي الخطاء, فقط
والبرنامج يلتقط الخطاء ( بواسطة العبارة Catch SqlEx As SqlException )
هذة طريقة إذا كنت تستخدم SQL SERVER 2012 فأعلى ، بالتحكم في الإجراء المخزن بواسطة العبارة THROW
SQL :
VB.NET :
ملاحظات :
VB.NET
كود :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using conn As New SqlClient.SqlConnection(My.Settings.MyConStr)
Try
Using cmd As New SqlCommand("StoredProcedure2", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@itemCode", TxtGrossIncome.Text.Trim)
cmd.Parameters.AddWithValue("@barCode ", txtPhone.Text.Trim)
conn.Open()
cmd.ExecuteNonQuery()
MsgBox("تمت الإضافة بنجاح")
End Using
Catch SqlEx As SqlException
If SqlEx.Number = 50601 Then
Dim barCodeNo As String = SqlEx.Message
MsgBox($"الباركود موجود بالفعل، رقم الباركود هو : {barCodeNo}", MsgBoxStyle.Exclamation, $"Sql Server Error")
Else
MsgBox(SqlEx.Message, MsgBoxStyle.Critical, $"Sql Server Error : {SqlEx.Number}, Line:{SqlEx.LineNumber}")
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Application Error")
Finally
conn.Close()
End Try
End Using
End SubSQL :
كود :
ALTER PROCEDURE StoredProcedure2
@barCode nvarchar(50) = '',
@itemCode int = 0
AS
BEGIN
SET NOCOUNT ON;
/* التأكد من إدخال بيانات */
IF(@barCode IS NULL OR @barCode = '')
BEGIN
;THROW 50600, N'عفواً، تأكد من إدخال بيانات.', 1;
END
/* التأكد من موجود سجل مسبق */
IF EXISTS (SELECT [barCode] FROM [Uint_ItemsTbl] WHERE [barCode] = @barCode)
BEGIN
DECLARE @barCodeNo bigint = (SELECT barCode FROM Uint_ItemsTbl WHERE itemCode = @itemCode AND barCode = @barCode)
--محتاج ارجع بقيمة الاستعلام الى تكست بوكس فى الفورم
;THROW 50601, @barCodeNo, 1;
END
BEGIN TRY
/* إدخال البيانات */
INSERT INTO [Uint_ItemsTbl] ([barCode], [itemCode]) VALUES (@barCode, @itemCode)
END TRY
BEGIN CATCH
DECLARE @ErrorMsg AS NVARCHAR(500) = ERROR_MESSAGE()
;THROW 50602, @ErrorMsg, 1;
END CATCH
END
GOملاحظة :
في الإجراء المخزن : THROW وظيفتها ترمي الخطاء, فقط
والبرنامج يلتقط الخطاء ( بواسطة العبارة Catch SqlEx As SqlException )
