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
| DataTable dtReturn = new DataTable(); var connection = new SqlConnection((_connectionstring.IndexOf( "MultipleActiveResultsets" ) == -1) ? _connectionstring + "MultipleActiveResultsets=True;" : _connectionstring); try { connection.Open(); Task<DataTable>[] tasks = new Task<DataTable>[source.Rows.Count]; for ( int i = 0; i < source.Rows.Count; i++) { int s = Convert.ToInt32(source.Rows[i][0]); Task<DataTable> t = Task.Factory.StartNew(() => { var command = connection.CreateCommand(); command.CommandText = commandText; command.CommandType = CommandType.StoredProcedure; command.CommandTimeout = timeout; foreach (SqlParameter p in parms) { if (p.ParameterName == "@Sources" ) command.Parameters.Add( new SqlParameter() { SqlDbType = System.Data.SqlDbType.Structured, ParameterName = p.ParameterName, Value = s.ToString().ToIDList() }); else command.Parameters.Add( new SqlParameter() { ParameterName = p.ParameterName, Value = p.Value }); } var dr = command.ExecuteReader(); command.Parameters.Clear(); DataTable dt = new DataTable(); if (dr.HasRows) { dt.Load(dr); dr.Close(); } return dt; }); tasks[i] = t; } Task.WaitAll(tasks); if (tasks.Length > 0) { bool isSchemaCloned = false ; foreach ( var t in tasks) { if (!isSchemaCloned && t.Result.Columns.Count > 0 ) { dtReturn = t.Result.Clone(); isSchemaCloned = true ; } foreach (DataRow r in t.Result.Rows) { dtReturn.Rows.Add(r.ItemArray); } } } } catch (Exception ex) { throw ex; } finally { if (connection.State != ConnectionState.Closed) connection.Close(); } return dtReturn; |
Chủ Nhật, 7 tháng 8, 2016
ADO.NET using TPL
Đăng ký:
Đăng Nhận xét (Atom)
Không có nhận xét nào:
Đăng nhận xét