-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.cs
More file actions
70 lines (65 loc) · 2.41 KB
/
Copy pathData.cs
File metadata and controls
70 lines (65 loc) · 2.41 KB
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
using MySql.Data.MySqlClient;
using SqlSugar;
using System.Collections.Concurrent;
using System.Data;
using System.Data.SqlClient;
namespace ExportExcelByMultithreading
{
internal static class Data
{
private const string connectionStr = "user=root;password=123456;server=localhost;database=test";
private const string sql = "select id as ID,c_name as Name from user_info";
public static void GetData()
{
using MySqlConnection conn = new MySqlConnection(connectionStr);
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
using var dataReader = cmd.ExecuteReader();
//MySqlDataAdapter dataAdapter = new MySqlDataAdapter();
//dataAdapter.SelectCommand = cmd;
//DataTable dt = new DataTable();
//dataAdapter.Fill(dt);
//foreach(DataRow row in dt.Rows)
//{
// Console.WriteLine($"{row["id"]}, {row["c_name"]}");
//}
while (dataReader.Read())
{
Console.WriteLine($"{dataReader.GetInt32("id")}-{dataReader.GetString("c_name")}");
}
}
public static void GetDataBySqlSugar(BlockingCollection<User> users)
{
Task.Run(() =>
{
using SqlSugarClient client = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = connectionStr,
DbType = SqlSugar.DbType.MySql
});
var userReader = client.Ado.GetDataReader(sql);
while (userReader.Read())
{
////bool res = false;
////if (res == false)
////{
var user = new User
{
Id = userReader.GetInt32(0),
Name = userReader.GetString(1),
};
users.Add(user);
//if (users.Count > 10000)
//{
// Console.WriteLine("队列已满...");
// Thread.Sleep(1000);
//}
//}
}
users.CompleteAdding();
Console.WriteLine("读取数据结束。");
});
}
}
}