This repository was archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathStoredProcedureTutorial.cs
More file actions
108 lines (99 loc) · 4.07 KB
/
Copy pathStoredProcedureTutorial.cs
File metadata and controls
108 lines (99 loc) · 4.07 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GraphView;
namespace GraphViewTutorial
{
class StoredProcedureTutorial
{
// This is a tutorial for GraphView user about how to create SP and execute SP
// Using SP is much more faster than call GraphViewConnection.executeNonQuery()
// We highly recommend you create SP for the queries
// Please notice that following script will drop all the table in database when it finished
public static void run()
{
GraphViewConnection con = new GraphViewConnection(TutorialConfiguration.getConnectionString());
con.Open();
con.ClearData();
try
{
#region Create a schema for node
con.CreateNodeTable(@"CREATE TABLE [Node] (
[ColumnRole:""NodeId""] id INT,
[ColumnRole:""Edge"",Reference:""Node""] Edges VARBINARY(max) )");
System.Console.WriteLine("Create table successed!");
#endregion
#region Create nodes
con.ExecuteNonQuery("INSERT INTO [Node](id) VALUES(1)");
con.ExecuteNonQuery("INSERT INTO [Node](id) VALUES(2)");
con.ExecuteNonQuery("INSERT INTO [Node](id) VALUES(3)");
#endregion
#region Create SP
con.CreateProcedure(@"CREATE PROCEDURE AddEdge
@st INT,
@ed INT
AS
BEGIN
INSERT EDGE INTO Node.Edges
SELECT s,t FROM
Node s , Node t WHERE s.id = @st AND t.id= @ed ;
END");
con.CreateProcedure(@"CREATE PROCEDURE SelectNeighbors
@id INT
AS
BEGIN
SELECT y.* FROM Node x, Node y
MATCH x-[Edges]->y
WHERE x.id = @id
END");
#endregion
#region Using SP to insert edges
using (var command = con.CreateCommand())
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "AddEdge";
command.Parameters.AddWithValue("@st", 1);
command.Parameters.AddWithValue("@ed", 2);
command.ExecuteNonQuery();
command.Parameters.Clear();
command.Parameters.AddWithValue("@st", 1);
command.Parameters.AddWithValue("@ed", 3);
command.ExecuteNonQuery();
}
#endregion
#region Using SP to query
using (var command = con.CreateCommand())
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "SelectNeighbors";
command.Parameters.AddWithValue("@id", 1);
var res = command.ExecuteReader();
try
{
while (res.Read())
{
System.Console.WriteLine(res["id"].ToString());
}
}
finally
{
if (res != null)
res.Close();
}
}
#endregion
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
finally // close the connection and drop table
{
con.ClearGraphDatabase();
con.Close();
}
}
}
}