-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
108 lines (93 loc) · 2.76 KB
/
Form1.cs
File metadata and controls
108 lines (93 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Autor: Julio Bill Schvenger
Date 24/08/2020
Objetivo: Capturar o exception da trigger no C#
--criar a tabela
IF OBJECT_ID ('dbo.tabela_0001') IS NOT NULL
DROP TABLE dbo.tabela_0001
GO
CREATE TABLE dbo.tabela_0001
(
campo1 INT NULL,
campo2 INT NULL,
campo3 INT NULL
)
GO
--criar a trigger
ALTER TRIGGER tI_tabela_0001 ON tabela_0001
FOR INSERT
AS
BEGIN
DECLARE @ierrno INT,
@vcerrmsg VARCHAR(255)
IF EXISTS (
SELECT 1
FROM inserted
WHERE ltrim(campo1) IS NULL
)
BEGIN
SELECT @ierrno = 100001,
@vcerrmsg = 'Nome da pessoa inválida...............'
GOTO error
END
RETURN
error:;
throw @ierrno,
@vcerrmsg,
1
END
GO
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjetoMessageTrigger
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string msg_except = "";
string queryString = "insert into tabela_0001 (campo1) values (null)";
StringBuilder errorMessages = new StringBuilder();
using (SqlConnection connection1 = new SqlConnection("Data Source=localhost;Initial Catalog=master;Integrated Security=True"))
{
SqlCommand command1 = new SqlCommand(queryString, connection1);
try
{
command1.Connection.Open();
command1.ExecuteNonQuery();
}
catch (SqlException ex)
{
for (int i = 0; i < ex.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + ex.Errors[i].Message + "\n" +
"LineNumber: " + ex.Errors[i].LineNumber + "\n" +
"Source: " + ex.Errors[i].Source + "\n" +
"Procedure: " + ex.Errors[i].Procedure + "\n");
if (ex.Errors[i].Procedure == "tI_tabela_0001")
{
msg_except =
"Mensagem da trigger: " +
ex.Errors[i].Message;
}
}
MessageBox.Show(msg_except, "Erro", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
}
}
}
}
}