forked from NewLifeX/X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrmSchema.cs
126 lines (115 loc) · 3.51 KB
/
FrmSchema.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using System.Windows.Forms;
using NewLife.Log;
using XCode.DataAccessLayer;
namespace XCoder
{
public partial class FrmSchema : Form
{
#region 属性
private IDatabase _Db;
/// <summary>数据库</summary>
public IDatabase Db
{
get { return _Db; }
set { _Db = value; }
}
#endregion
#region 初始化界面
public FrmSchema()
{
InitializeComponent();
Icon = Source.GetIcon();
}
public static FrmSchema Create(IDatabase db)
{
var frm = new FrmSchema
{
Db = db ?? throw new ArgumentNullException("db")
};
return frm;
}
private void FrmSchema_Load(Object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
var tables = Db.CreateMetaData().GetTables();
this.Invoke(SetList, cbTables, tables);
}).LogException();
Task.Factory.StartNew(() =>
{
var list = Db.CreateMetaData().MetaDataCollections;
this.Invoke(SetList, cbSchemas, list);
}).LogException();
}
#endregion
#region 加载
void SetList(ComboBox cb, IEnumerable data)
{
if (cb == null || data == null) return;
try
{
if (!(data is IList))
{
var list = new List<Object>();
foreach (var item in data)
{
list.Add(item);
}
data = list;
}
cb.DataSource = data;
//cb.DisplayMember = "value";
cb.Update();
}
catch (Exception ex)
{
XTrace.WriteException(ex);
}
}
#endregion
private void cbTables_SelectedIndexChanged(Object sender, EventArgs e)
{
var cb = sender as ComboBox;
if (cb == null) return;
var obj = cb.SelectedItem;
if (obj == null) return;
try
{
var ss = Db.CreateSession();
if (obj is IDataTable)
{
var sql = "select * from " + (obj as IDataTable).TableName;
DataTable dt = null;
try
{
using (var cmd = ss.CreateCommand(sql))
using (var reader = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
{
dt = reader.GetSchemaTable();
}
}
finally
{
ss.AutoClose();
}
obj = dt;
}
else if (obj is String)
{
obj = ss.GetSchema((String)obj, null);
}
gv.DataSource = obj;
gv.Update();
}
catch (Exception ex)
{
XTrace.WriteException(ex);
}
}
}
}