-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
136 lines (113 loc) · 4.79 KB
/
Program.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using FirebirdSql.Data.FirebirdClient;
using FirebirdSql.Data.Services;
namespace FB3EmbedDotNetExample
{
class Program
{
static void Main(string[] args)
{
var databasefilename = System.IO.Path.GetFullPath("demo.fdb");
Console.WriteLine("databasefilename: {0}", databasefilename);
var firebirdNativeClientLibraryFilename = System.IO.Path.GetFullPath(@"Firebird-3.0.4.33054-x64-embedded\fbclient.dll");
Console.WriteLine("firebirdNativeClientLibraryFilename: {0}", firebirdNativeClientLibraryFilename);
if (File.Exists(firebirdNativeClientLibraryFilename) == false)
{
throw new Exception("Firebird Embedded Not Found At Expected Location!");
}
var csb = new FbConnectionStringBuilder();
csb.ServerType = FbServerType.Embedded;
csb.ClientLibrary = firebirdNativeClientLibraryFilename;
csb.Database = databasefilename;
csb.UserID = "ThisCanBeAnything";
csb.Password = "AnyValueAtAll";
var connectionstring = csb.ToString();
if (File.Exists(databasefilename) == false)
{
Console.WriteLine("Creating database {0}", databasefilename);
FbConnection.CreateDatabase(connectionstring, false);
Console.WriteLine("Created database {0}", databasefilename);
}
Console.WriteLine("Connecting");
using (var connection = new FbConnection(csb.ToString()))
{
connection.StateChange += Connection_StateChange;
connection.InfoMessage += Connection_InfoMessage;
connection.Disposed += Connection_Disposed;
connection.Open();
using (var trn = connection.BeginTransaction())
{
using (var cmd = connection.CreateCommand())
{
cmd.Transaction = trn;
cmd.CommandText = @"
RECREATE TABLE ATABLE
(
PK INTEGER NOT NULL,
NAME VARCHAR( 128),
CONSTRAINT PK_ATABLE PRIMARY KEY (PK)
);";
cmd.ExecuteNonQuery();
}
trn.Commit();
}
using (var trn = connection.BeginTransaction())
{
using (var cmd = connection.CreateCommand())
{
cmd.Transaction = trn;
cmd.CommandText = "INSERT INTO ATABLE (PK,NAME) VALUES (@PK,@NAME)";
cmd.Parameters.Add("PK", 1);
cmd.Parameters.Add("NAME", "Adam");
cmd.ExecuteNonQuery();
}
using (var cmd = connection.CreateCommand())
{
cmd.Transaction = trn;
cmd.CommandText = "INSERT INTO ATABLE (PK,NAME) VALUES (@PK,@NAME)";
cmd.Parameters.Add("PK", 2);
cmd.Parameters.Add("NAME", "Eve");
cmd.ExecuteNonQuery();
}
using (var cmd = connection.CreateCommand())
{
cmd.Transaction = trn;
cmd.CommandText = "SELECT * from ATABLE";
using (var reader = cmd.ExecuteReader())
{
int RowIndex = 0;
while (reader.Read())
{
RowIndex += 1;
var PK = reader.GetFieldValue<int>(0);
var Name = reader.GetString(1);
Console.WriteLine("Row #{0} PK={1} Name={2}", RowIndex, PK, Name);
}
}
}
trn.Commit();
}
connection.Close();
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
private static void Connection_Disposed(object sender, EventArgs e)
{
Console.WriteLine("Connection Disposed");
}
private static void Connection_InfoMessage(object sender, FbInfoMessageEventArgs e)
{
Console.WriteLine("Connection InfoMessage:{0}", e.Message);
}
private static void Connection_StateChange(object sender, System.Data.StateChangeEventArgs e)
{
Console.WriteLine("Connection StateChange:{0}", e.CurrentState);
}
}
}