-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
369 lines (256 loc) · 10.5 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A0530_LINQ_SQL.Sample;
namespace A0530_LINQ_SQL
{
class Program
{
/// <summary>
/// SQL Server 的数据库连接字符串.
/// </summary>
private const String connString =
@"Data Source=localhost\SQLEXPRESS;Initial Catalog=TestLinq2Sql;Integrated Security=True";
static void Main(string[] args)
{
// 基本功能测试.
BaseTest();
// 事务处理的测试.
TransactionTest();
// 动态查询的测试.
IQueryableTest(null, null);
IQueryableTest(1, null);
IQueryableTest(null, "ONE");
IQueryableTest(1, "ONE");
// 手动执行SQL.
ExecuteQueryTest();
ExecuteCommandTest();
// 测试单表 identity 处理.
TestIdeneity();
// 测试调存储过程.
CallProc();
// 测试调函数
CallFunc();
Console.ReadLine();
}
/// <summary>
/// Linq2SQL 基本功能测试
///
/// 基本的 查询 / 插入 / 更新 / 删除 处理.
///
/// </summary>
private static void BaseTest()
{
Test context = new Test(connString);
// 单表查询
var query =
from testMain in context.TestMain
select testMain;
foreach (TestMain main in query)
{
Console.WriteLine("Main[{0}, {1}]", main.Id, main.Value);
}
// 关联查询
var query2 =
from testSub in context.TestSub
where testSub.TestMain.Value == "ONE"
select testSub;
foreach (TestSub sub in query2)
{
Console.WriteLine("Sub[{0}, {1}]", sub.Id, sub.Value);
}
// 插入.
TestMain main3 = new TestMain();
main3.Id = 3;
main3.Value = "Three";
context.TestMain.InsertOnSubmit(main3);
context.SubmitChanges();
Console.WriteLine("INSERT FINISH! --- Please Press Enter Ket");
Console.ReadLine();
// 更新.
var newTestMain =
(from testMain in context.TestMain
where testMain.Id == 3
select testMain).First();
newTestMain.Value = "Three3";
context.SubmitChanges();
Console.WriteLine("UPDATE FINISH! --- Please Press Enter Ket");
Console.ReadLine();
// 单表查询 TOP 2
var queryTop2 =
(from testMain in context.TestMain
orderby testMain.Id descending
select testMain).Take(2);
foreach (TestMain main in queryTop2)
{
Console.WriteLine("Main[{0}, {1}]", main.Id, main.Value);
}
// 删除.
context.TestMain.DeleteOnSubmit(newTestMain);
context.SubmitChanges();
Console.WriteLine("DELETE FINISH! --- Please Press Enter Ket");
Console.ReadLine();
}
/// <summary>
/// 事务处理的测试.
///
/// 隐式创建事务
///
/// </summary>
private static void TransactionTest()
{
// 在我们没有显式的开启事务时,
// DataContext.SubmitChanges在提交时会隐式创建事务
Test context = new Test(connString);
try
{
// 插入.
TestMain main3 = new TestMain();
main3.Id = 3;
main3.Value = "Three";
context.TestMain.InsertOnSubmit(main3);
TestMain main3x = new TestMain();
main3x.Id = 3;
main3x.Value = "Three Err";
context.TestMain.InsertOnSubmit(main3x);
context.SubmitChanges();
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
var query =
from testMain in context.TestMain
where testMain.Id == 3
select testMain;
Console.WriteLine("TestMain 表中, id=3 的记录有{0}条 ", query.Count());
}
/// <summary>
/// 用于 动态的查询处理.
/// </summary>
/// <param name="id"></param>
/// <param name="val"></param>
private static void IQueryableTest(int? id, string val)
{
// 如果 用户只输入了 id, 那么 只 where id = ?
// 如果 用户只输入了 val, 那么 只 where value = ?
// 如果 用户 id 和 val 都输入了, 那么 where id=? and value = ?
// 如果 用户 id 和 val 都没输入, 那么 where 也不需要了.
Console.WriteLine("传入的参数:id={0}, val={1}", id, val);
Test context = new Test(connString);
// 在控制台窗口中显示生成的 SQL.
// 当需要做 Debug 处理的时候, 可以这么设置.
context.Log = Console.Out;
IQueryable<TestMain> result = context.TestMain;
if (id != null)
{
result = result.Where(c => c.Id == id);
}
if (val != null)
{
result = result.Where(c => c.Value == val);
}
foreach (TestMain data in result)
{
Console.WriteLine("Main[{0}, {1}]", data.Id, data.Value);
}
}
/// <summary>
/// 测试直接执行 SQL 语句.
/// </summary>
private static void ExecuteQueryTest()
{
Test context = new Test(connString);
Console.WriteLine("手动执行 SQL : SELECT * FROM test_main WHERE id = 2 ");
var mainData = context.ExecuteQuery<TestMain>("SELECT * FROM TestMain WHERE id = 2");
foreach (TestMain data in mainData)
{
Console.WriteLine("Main[{0}, {1}]", data.Id, data.Value);
}
}
/// <summary>
/// 测试直接执行 SQL 语句.
/// </summary>
private static void ExecuteCommandTest()
{
Test context = new Test(connString);
Console.WriteLine("手动执行 SQL : INSERT INTO TestMain(id, value) VALUES (4, 'FOUR') ");
int r1 = context.ExecuteCommand("INSERT INTO TestMain(id, value) VALUES (4, 'FOUR') ");
Console.WriteLine("执行结果:{0}", r1);
Console.WriteLine("手动执行 SQL : SELECT * FROM TestMain WHERE id = 4 ");
var mainData = context.ExecuteQuery<TestMain>("SELECT * FROM TestMain WHERE id = 4");
foreach (TestMain data in mainData)
{
Console.WriteLine("Main[{0}, {1}]", data.Id, data.Value);
}
Console.WriteLine("手动执行 SQL : DELETE TestMain WHERE id = 4");
int r2 = context.ExecuteCommand("DELETE TestMain WHERE id = 4");
Console.WriteLine("执行结果:{0}", r2);
}
/// <summary>
/// 测试 Identity 的处理.
/// </summary>
private static void TestIdeneity()
{
Console.WriteLine("===== 测试父子关系表的 Identity 的处理.");
using (Test context = new Test(connString))
{
test_Identity_tab testMainData = new test_Identity_tab()
{
value = "测试主"
};
test_Identity_tab_Sub testSubData = new test_Identity_tab_Sub()
{
Value = "测试子"
};
testSubData.test_Identity_tab = testMainData;
// 插入.
context.test_Identity_tab.InsertOnSubmit(testMainData);
context.test_Identity_tab_Sub.InsertOnSubmit(testSubData);
Console.WriteLine("Before SubmitChanges testMainData.id = {0}", testMainData.id);
Console.WriteLine("Before SubmitChanges testSubData.id = {0}", testSubData.Id);
context.SubmitChanges();
Console.WriteLine("After SubmitChanges testMainData.id = {0}", testMainData.id);
Console.WriteLine("After SubmitChanges testSubData.id = {0}", testSubData.Id);
}
}
/// <summary>
/// 调存储过程.
/// </summary>
private static void CallProc()
{
Console.WriteLine("===== 测试执行存储过程的处理.");
using (Test context = new Test(connString))
{
string outVal = "", inoutVal=" Hello";
int result = context.HelloWorld2("Edward", ref outVal, ref inoutVal);
Console.WriteLine("执行存储过程 HelloWorld2, 返回值={0}, out参数1={1}, out参数2={2}",
result, outVal, inoutVal);
// 执行返回结果集的存储过程.
var query = context.testProc();
foreach (testProcResult data in query)
{
Console.WriteLine("执行返回结果集的存储过程,执行结果:A={0},B={1}", data.A, data.B);
}
}
}
/// <summary>
/// 调函数.
/// </summary>
private static void CallFunc()
{
Console.WriteLine("===== 测试执行函数的处理.");
using (Test context = new Test(connString))
{
string test = context.HelloWorldFunc();
Console.WriteLine("调用 HelloWorldFunc, 返回结果={0}", test);
var query = context.getHelloWorld();
foreach (getHelloWorldResult data in query)
{
Console.WriteLine("调用返回结果集的函数,返回结果 A={0}; B={1}", data.A, data.B);
}
}
}
}
}