-
Notifications
You must be signed in to change notification settings - Fork 7
/
FrmSync.cs
269 lines (223 loc) · 8.25 KB
/
FrmSync.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using DataMigration.Models;
using NewLife;
using NewLife.Log;
using System.ComponentModel;
using System.Data;
using XCode.DataAccessLayer;
namespace DataMigration
{
[Category("数据工具")]
[DisplayName("跨库数据同步")]
public partial class FrmSync : Form
{
DAL _source;
IList<IDataTable> _tables;
IList<TableModel> _models;
public FrmSync()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
richTextBox_log.UseWinFormControl();
}
private void FrmSync_Load(Object sender, EventArgs e)
{
cbConn.DataSource = DAL.ConnStrs.Keys.ToArray();
Task.Run(() => DbHelper.AutoDetectDatabase(() => this.Invoke(SetDatabaseList, DAL.ConnStrs.Keys.OrderBy(e => e).ToList())));
}
void SetDatabaseList(ICollection<String> list)
{
var str = cbConn.Text;
cbConn.DataSource = list;
//cbConn.DisplayMember = "value";
if (!String.IsNullOrEmpty(str)) cbConn.Text = str;
if (cbConn.SelectedIndex < 0 && cbConn.Items != null && cbConn.Items.Count > 0) cbConn.SelectedIndex = 0;
}
private void btnConnection_Click(Object sender, EventArgs e)
{
var btn = sender as Button;
if (btn == null) return;
var connName = cbConn.SelectedItem + "";
if (string.IsNullOrWhiteSpace(connName)) return;
_source = DAL.Create(connName);
if (btn.Text == "连接")
{
var conns = DAL.ConnStrs.Keys.Where(e => e != connName).OrderBy(e => e).ToArray();
cbTarget.DataSource = conns;
// 获取数据表
_tables = _source.Tables;
_models = _tables
//.Where(e => !e.IsView)
.Select(e => new TableModel
{
Name = e.TableName,
DisplayName = e.DisplayName,
EnableSync = true,
}).OrderBy(e => e.Name).ToList();
Task.Run(FetchRows);
dataGridView1.DataSource = _models;
gbSource.Enabled = true;
gbTarget.Enabled = true;
gbSetting.Enabled = false;
btn.Text = "断开";
btnConnect2.Text = "连接";
}
else
{
gbSource.Enabled = true;
gbTarget.Enabled = false;
gbSetting.Enabled = false;
btn.Text = "连接";
}
}
void FetchRows()
{
foreach (var item in _models)
{
var sb = new SelectBuilder { Table = item.Name };
item.Total = _source.SelectCount(sb);
}
Invoke(() => dataGridView1.Refresh());
}
private void btnConnect2_Click(Object sender, EventArgs e)
{
var btn = sender as Button;
if (btn == null) return;
if (btn.Text == "连接")
{
var connName = cbTarget.SelectedItem + "";
var dal = DAL.Create(connName);
Task.Run(() => FetchRows2(dal));
//gbTarget.Enabled = false;
gbSetting.Enabled = true;
btn.Text = "断开";
}
else
{
//gbTarget.Enabled = true;
gbSetting.Enabled = false;
btn.Text = "连接";
}
}
void FetchRows2(DAL dal)
{
var tables = dal.Tables;
if (tables == null) return;
foreach (var item in _models)
{
if (tables.Any(e => e.TableName.EqualIgnoreCase(item.Name)))
{
var sb = new SelectBuilder { Table = item.Name };
item.Total2 = dal.SelectCount(sb);
}
}
Invoke(() => dataGridView1.Refresh());
}
void DataGridViewUpdate(Object state)
{
// 定时任务
Invoke(() => dataGridView1.Refresh());
}
private async void btnSync_Click(Object sender, EventArgs e)
{
var connName = cbTarget.SelectedItem + "";
if (connName.IsNullOrEmpty()) return;
//var ts = _models.Select(e => e.Table).ToArray();
//if (ts.Length == 0) return;
gbSetting.Enabled = false;
var syncSchema = cbSyncSchema.Checked;
var ignoreError = cbIgnoreError.Checked;
//_source.SyncAll(ts, connName, cbSyncSchema.Checked, cbIgnoreError.Checked);
var dpk = new DbPackage
{
Dal = _source,
IgnoreError = ignoreError,
Log = XTrace.Log
};
//dpk.OnProgress = (p, dt) =>
//{
// var m = _models.FirstOrDefault(e => e.Name == dt);
//};
//Task.Run(() => dpk.SyncAll(ts, connName, syncSchema));
await Task.Run(() =>
{
var dal = DAL.Create(connName);
var tables = dal.Tables ?? new List<IDataTable>();
_source.Db.ShowSQL = false;
_source.Session.ShowSQL = false;
dal.Db.ShowSQL = false;
dal.Session.ShowSQL = false;
foreach (var item in _models)
{
if (!item.EnableSync) continue;
try
{
if (!syncSchema && !tables.Any(e => e.TableName.EqualIgnoreCase(item.Name)))
{
item.Description = "目标表不存在,跳过!";
}
else
{
if (item.Total2 != -1)
{
dal.Delete(item.Name, null);
}
var tb = _tables.FirstOrDefault(e => e.TableName == item.Name);
var rs = _source.Sync(tb, connName, syncSchema);
item.Finish = rs;
item.Total2 = rs;
item.Description = "成功!";
}
}
catch (Exception ex)
{
item.Description = ex.Message;
XTrace.WriteException(ex);
if (!ignoreError) throw;
}
Invoke(() => dataGridView1.Refresh());
}
_source.Session.ShowSQL = true;
_source.Db.ShowSQL = true;
dal.Session.ShowSQL = true;
dal.Db.ShowSQL = true;
});
gbSetting.Enabled = true;
}
private void btnSelectAll_Click(Object sender, EventArgs e)
{
if (_models == null) return;
foreach (var model in _models)
{
model.EnableSync = true;
}
dataGridView1.Refresh();
}
private void btnSelectOther_Click(Object sender, EventArgs e)
{
if (_models == null) return;
foreach (var item in _models)
{
item.EnableSync = !item.EnableSync;
}
dataGridView1.Refresh();
}
private void btnDifferent_Click(Object sender, EventArgs e)
{
if (_models == null) return;
foreach (var item in _models)
{
item.EnableSync = item.Total < item.Total2;
}
dataGridView1.Refresh();
}
private void 数据库配置ToolStripMenuItem_Click(object sender, EventArgs e)
{
AddDataBaseForm add = new AddDataBaseForm();
var reslut = add.ShowDialog();
if (reslut == DialogResult.OK || reslut == DialogResult.Cancel)
{
Task.Run(() => DbHelper.AutoDetectDatabase(() => this.Invoke(SetDatabaseList, DAL.ConnStrs.Keys.OrderBy(e => e).ToList())));
}
}
}
}