Project

General

Profile

Send NET C# » History » Version 1

Tomek Dziemidowicz, 2019-07-18 09:06 PM

1 1 Tomek Dziemidowicz
h1. Send NET C#
2
3
<pre><code class="csharp">
4
/// <summary>
5
/// Send changes (updates, insert, deletes) from local database to server
6
/// </summary>
7
/// <param name="subscriberId"></param>
8
private void SendChanges(string subscriberId)
9
{
10
    using (SQLiteConnection conn = new SQLiteConnection(this.connString))
11
    {
12
        using (SQLiteCommand cmd = new SQLiteCommand())
13
        {
14
            cmd.Connection = conn;
15
            conn.Open();
16
17
            SQLiteHelper sh = new SQLiteHelper(cmd);
18
19
            DataTable tables = sh.Select("select tbl_Name from sqlite_master where type='table' and sql like '%RowId%';");
20
21
            StringBuilder sqlitesync_SyncDataToSend = new StringBuilder();
22
            sqlitesync_SyncDataToSend.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?><SyncData xmlns=\"urn:sync-schema\">");
23
24
            foreach (DataRow table in tables.Rows)
25
            {
26
                string tableName = table["tbl_Name"].ToString();
27
                if (tableName.ToLower() != "MergeDelete".ToLower())
28
                {
29
                    try
30
                    {
31
                        sqlitesync_SyncDataToSend.Append("<tab n=\"" + tableName + "\">");
32
33
                        #region new records
34
                        DataTable newRecords = sh.Select("select * from " + tableName + " where RowId is null;");
35
                        sqlitesync_SyncDataToSend.Append("<ins>");
36
                        foreach (DataRow record in newRecords.Rows)
37
                        {
38
                            sqlitesync_SyncDataToSend.Append("<r>");
39
                            foreach (DataColumn column in newRecords.Columns)
40
                            {
41
                                if (column.ColumnName != "MergeUpdate")
42
                                {
43
                                    sqlitesync_SyncDataToSend.Append("<" + column.ColumnName + ">");
44
                                    sqlitesync_SyncDataToSend.Append("<![CDATA[" + record[column.ColumnName].ToString() + "]]>");
45
                                    sqlitesync_SyncDataToSend.Append("</" + column.ColumnName + ">");
46
                                }
47
                            }
48
                            sqlitesync_SyncDataToSend.Append("</r>");
49
                        }
50
                        sqlitesync_SyncDataToSend.Append("</ins>");
51
                        #endregion
52
53
                        #region updated records
54
                        DataTable updRecords = sh.Select("select * from " + tableName + " where MergeUpdate > 0 and RowId is not null;");
55
                        sqlitesync_SyncDataToSend.Append("<upd>");
56
                        foreach (DataRow record in updRecords.Rows)
57
                        {
58
                            sqlitesync_SyncDataToSend.Append("<r>");
59
                            foreach (DataColumn column in updRecords.Columns)
60
                            {
61
                                if (column.ColumnName != "MergeUpdate")
62
                                {
63
                                    sqlitesync_SyncDataToSend.Append("<" + column.ColumnName + ">");
64
                                    if (record[column.ColumnName].GetType().Name == "Byte[]")
65
                                        sqlitesync_SyncDataToSend.Append("<![CDATA[" + Convert.ToBase64String((byte[])record[column.ColumnName]) + "]]>");
66
                                    else
67
                                        sqlitesync_SyncDataToSend.Append("<![CDATA[" + record[column.ColumnName].ToString() + "]]>");
68
                                    sqlitesync_SyncDataToSend.Append("</" + column.ColumnName + ">");
69
                                }
70
                            }
71
                            sqlitesync_SyncDataToSend.Append("</r>");
72
                        }
73
                        sqlitesync_SyncDataToSend.Append("</upd>");
74
                        #endregion
75
76
                        sqlitesync_SyncDataToSend.Append("</tab>");
77
                    }
78
                    catch (Exception ex)
79
                    {
80
                        throw ex;
81
                    }
82
                }
83
            }
84
85
            #region deleted records
86
            DataTable delRecords = sh.Select("select * from MergeDelete;");
87
            sqlitesync_SyncDataToSend.Append("<delete>");
88
            foreach (DataRow record in delRecords.Rows)
89
            {
90
                sqlitesync_SyncDataToSend.Append("<r>");
91
                sqlitesync_SyncDataToSend.Append("<tb>" + record["TableId"].ToString() + "</tb>");
92
                sqlitesync_SyncDataToSend.Append("<id>" + record["RowId"].ToString() + "</id>");
93
                sqlitesync_SyncDataToSend.Append("</r>");
94
            }
95
            sqlitesync_SyncDataToSend.Append("</delete>");
96
            #endregion
97
98
            sqlitesync_SyncDataToSend.Append("</SyncData>");
99
100
            #region send changes to server
101
            JsonObject inputObject = new JsonObject();
102
            inputObject.Add("subscriber", subscriberId);
103
            inputObject.Add("content", sqlitesync_SyncDataToSend.ToString());
104
            inputObject.Add("version", "3");
105
106
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
107
            byte[] bytes = encoding.GetBytes(inputObject.ToString());
108
109
            var request = new RestRequest("Send", Method.POST);
110
            request.AddHeader("Content-Type", "application/json");
111
            request.AddHeader("Accept", "*/*");
112
            request.AddHeader("charset", "utf-8");
113
            request.AddHeader("Content-Length", bytes.Length.ToString());
114
115
            request.AddParameter("application/json; charset=utf-8", inputObject.ToString(), ParameterType.RequestBody);
116
            request.RequestFormat = DataFormat.Json;
117
118
            IRestResponse response = wsClient.Execute(request);
119
            #endregion
120
121
            #region clear update marker
122
            foreach (DataRow table in tables.Rows)
123
            {
124
                string tableName = table["tbl_Name"].ToString().ToLower();
125
                if (tableName != "MergeDelete".ToLower() && tableName != "MergeIdentity".ToLower())
126
                {
127
                    string updTriggerSQL = (string)sh.ExecuteScalar("select sql from sqlite_master where type='trigger' and name like 'trMergeUpdate_" + tableName + "'");
128
                    sh.Execute("drop trigger trMergeUpdate_" + tableName + ";");
129
                    sh.Execute("update " + tableName + " set MergeUpdate=0 where MergeUpdate > 0;");
130
                    sh.Execute(updTriggerSQL);
131
                }
132
133
                if (tableName == "MergeIdentity".ToLower())
134
                    sh.Execute("update MergeIdentity set MergeUpdate=0 where MergeUpdate > 0;");
135
            }
136
            #endregion
137
138
            #region clear delete marker
139
            sh.Execute("delete from MergeDelete");
140
            #endregion
141
142
            conn.Close();
143
        }
144
    }
145
}
146
147
</code></pre>
Go to top