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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
 
namespace Example {
 
    /*
     * Simple example - Load sample earth quake data into table. Perform simple query
     **/
    public class Run {
 
        private string[] mRawEarthQuakeData = new string[] {
            "2680586,-40.74401,174.21178,2612341,6050864,2007,1,10,0,18,30.98472,2.658,55.7826,",
            "2680587,-37.83085,176.79094,2843831,6367800,2007,1,10,0,23,37.06635,2.174,5,",
            "2680596,-40.7615,174.53143,2639299,6048499,2007,1,10,1,1,27.73818,2.483,64.2591,",
            "2680608,-38.61806,176.03662,2774500,6283017,2007,1,10,1,32,30.70737,1.579,8.5689,",
            "2680609,-41.32589,172.75418,2489421,5986927,2007,1,10,1,35,39.96362,2.696,105.5983,",
            "2680612,-42.98694,172.54916,2473251,5802358,2007,1,10,1,41,21.80939,2.722,5,",
            "2680615,-45.53393,167.29909,2064956,5504088,2007,1,10,1,54,26.31741,2.426,5,"
        };
 
        public void LoadData() {
 
            Tables.EarthQuake.Table eqTable = Tables.EarthQuake.Table.INSTANCE;
 
            using(Sql.Transaction transaction = new Sql.Transaction(DB.INSTANCE)) {
 
                for (int index = 0; index < mRawEarthQuakeData.Length; index++) {
 
                    Earthquake earthquake = new Earthquake(mRawEarthQuakeData[index].Split(','));
 
                    Sql.Query.Insert(eqTable)
                        .Set(eqTable.CUSP_ID, earthquake.CUSP_ID)
                        .Set(eqTable.LAT, earthquake.LAT)
                        .Set(eqTable.LONG, earthquake.LONG)
                        .Set(eqTable.NZMGE, earthquake.NZMGE)
                        .Set(eqTable.NZMGN, earthquake.NZMGN)
                        .Set(eqTable.DateTime, earthquake.DATE_TIME)
                        .Set(eqTable.MAG, earthquake.MAG)
                        .Set(eqTable.DEPTH, earthquake.DEPTH)
                        .Execute(transaction);
                }
                transaction.Commit();
            }
 
            //Simple query
            Sql.IResult result = Sql.Query.Select(eqTable)
                .From(eqTable)
                .Where(eqTable.MAG > 7 & eqTable.DEPTH < 10)
                .OrderBy(eqTable.MAG.DESC)
                .ExecuteUncommitted();
 
            for (int index = 0; index < result.Count; index++) {
 
                Tables.EarthQuake.Row eqRow = eqTable[index, result];
 
                decimal mag = eqRow.MAG;    //Access data on row
                decimal depth = eqRow.DEPTH;
            }
        }
 
        /*
         * Load using Row object instead of insert query
         **/
        public void LoadData2() {
 
            Tables.EarthQuake.Table eqTable = Tables.EarthQuake.Table.INSTANCE;
 
            using (Sql.Transaction transaction = new Sql.Transaction(DB.INSTANCE)) {
 
                for (int index = 0; index < mRawEarthQuakeData.Length; index++) {
 
                    Earthquake earthquake = new Earthquake(mRawEarthQuakeData[index].Split(','));
 
                    Tables.EarthQuake.Row eqRow = new Tables.EarthQuake.Row();
 
                    eqRow.CUSP_ID = earthquake.CUSP_ID;
                    eqRow.LAT = earthquake.LAT;
                    eqRow.LONG = earthquake.LONG;
                    eqRow.NZMGE = earthquake.NZMGE;
                    eqRow.NZMGN = earthquake.NZMGN;
                    eqRow.DateTime = earthquake.DATE_TIME;
                    eqRow.MAG = earthquake.MAG;
                    eqRow.DEPTH = earthquake.DEPTH;
 
                    eqRow.Update(transaction);
                }
                transaction.Commit();
            }
        }
    }
 
    public class Earthquake {
 
        public int CUSP_ID { get; private set; }
        public decimal LAT { get; private set; }
        public decimal LONG { get; private set; }
 
        public int NZMGE { get; private set; }
        public int NZMGN { get; private set; }
 
        public DateTime DATE_TIME { get; private set; }
 
        public decimal MAG { get; private set; }
        public decimal DEPTH { get; private set; }
 
        public Earthquake(string[] fields) {
 
            CUSP_ID = int.Parse(fields[0]);
            LAT = decimal.Parse(fields[1]);
            LONG = decimal.Parse(fields[2]);
 
            NZMGE = !string.IsNullOrEmpty(fields[3]) ? int.Parse(fields[3]) : -1;
            NZMGN = !string.IsNullOrEmpty(fields[4]) ? int.Parse(fields[4]) : -1;
 
            int year = int.Parse(fields[5]);
            int month = int.Parse(fields[6]);
            int day = int.Parse(fields[7]);
            int hour = int.Parse(fields[8]);
            int minute = int.Parse(fields[9]);
            int seconds = (int)decimal.Parse(fields[10]);
 
            DATE_TIME = new DateTime(year, month, day, hour, minute, seconds);
 
            MAG = !string.IsNullOrEmpty(fields[11]) ? decimal.Parse(fields[11]) : -1;
            DEPTH = !string.IsNullOrEmpty(fields[11]) ? decimal.Parse(fields[12]) : -1;
        }
    }
}
 
namespace Tables.EarthQuake {
 
    /*
     * Generated using Table generator tool
     **/
    public sealed class Table : Sql.ATable {
 
        public static readonly Table INSTANCE = new Table();
 
        public readonly Sql.Column.IntegerColumn CUSP_ID;
        public readonly Sql.Column.DecimalColumn LAT;
        public readonly Sql.Column.DecimalColumn LONG;
        public readonly Sql.Column.IntegerColumn NZMGE;
        public readonly Sql.Column.IntegerColumn NZMGN;
        public readonly Sql.Column.DateTimeColumn DateTime;
        public readonly Sql.Column.DecimalColumn MAG;
        public readonly Sql.Column.DecimalColumn DEPTH;
 
        public Table() : base(DB.INSTANCE, "Earthquake", typeof(Row)) {
 
            CUSP_ID = new Sql.Column.IntegerColumn(this, "earCUSP_ID", true);
            LAT = new Sql.Column.DecimalColumn(this, "earLAT", false);
            LONG = new Sql.Column.DecimalColumn(this, "earLONG", false);
            NZMGE = new Sql.Column.IntegerColumn(this, "earNZMGE", false);
            NZMGN = new Sql.Column.IntegerColumn(this, "earNZMGN", false);
            DateTime = new Sql.Column.DateTimeColumn(this, "earDateTime", false);
            MAG = new Sql.Column.DecimalColumn(this, "earMAG", false);
            DEPTH = new Sql.Column.DecimalColumn(this, "earDEPTH", false);
 
            AddColumns(CUSP_ID, LAT, LONG, NZMGE, NZMGN, DateTime, MAG, DEPTH);
        }
 
        public Row this[int pIndex, Sql.IResult pQueryResult] {
            get { return (Row)pQueryResult.GetRow(this, pIndex); }
        }
    }
 
    /*
     * Generated using Table generator tool
     **/
    public sealed class Row : Sql.ARow {
 
        private new Table Tbl {
            get { return (Table)base.Tbl; }
        }
 
        public Row() : base(Table.INSTANCE) {
        }
 
        public int CUSP_ID {
            get { return Tbl.CUSP_ID.ValueOf(this); }
            set { Tbl.CUSP_ID.SetValue(this, value); }
        }
        public decimal LAT {
            get { return Tbl.LAT.ValueOf(this); }
            set { Tbl.LAT.SetValue(this, value); }
        }
        public decimal LONG {
            get { return Tbl.LONG.ValueOf(this); }
            set { Tbl.LONG.SetValue(this, value); }
        }
        public int NZMGE {
            get { return Tbl.NZMGE.ValueOf(this); }
            set { Tbl.NZMGE.SetValue(this, value); }
        }
        public int NZMGN {
            get { return Tbl.NZMGN.ValueOf(this); }
            set { Tbl.NZMGN.SetValue(this, value); }
        }
        public DateTime DateTime {
            get { return Tbl.DateTime.ValueOf(this); }
            set { Tbl.DateTime.SetValue(this, value); }
        }
        public decimal MAG {
            get { return Tbl.MAG.ValueOf(this); }
            set { Tbl.MAG.SetValue(this, value); }
        }
        public decimal DEPTH {
            get { return Tbl.DEPTH.ValueOf(this); }
            set { Tbl.DEPTH.SetValue(this, value); }
        }
    }
 
    /*
     *  Single database class
     **/
    public class DB : Sql.ADatabase {
 
        public readonly static Sql.ADatabase INSTANCE = new DB();
 
        private DB() : base("DB", Sql.DatabaseType.Mssql) {
        }
 
        protected override string ConnectionString {
            get { return "user id=user_login;password=pwd;server=localhost\\SQLEXPRESS;Trusted_Connection=no;database=DB;connection timeout=30"; }
        }
        public override System.Data.Common.DbConnection GetConnection() {
            SqlConnection connection = new SqlConnection(ConnectionString);
            connection.Open();
            return connection;
        }
    }
}