赞
踩
apache doris 兼容mysql协议;所以我们在.netcore项目中,可以使用Mysql的驱动
dotnet add package MySqlConnector
测试代码:
- [HttpGet]
- public async Task<string> Get2()
- {
- //打开连接
- await using var connection = new MySqlConnection("Server=192.168.122.136;Port=9030;User ID=root;Password=admin123;Database=demo");
- await connection.OpenAsync();
-
-
- //添加数据
- using (MySqlCommand cmd = new MySqlCommand())
- {
- cmd.Connection = connection;
- cmd.CommandText = "insert into example_tbl VALUES(10006,'2021-4-10', '北京',36,0,'2023-3-10',200,40,6)";
- // cmd.Parameters.AddWithValue("p", "Hello world");
- int ret = await cmd.ExecuteNonQueryAsync();
- Console.WriteLine($"ret={ret}");
- }
-
- //查询
- using var command = new MySqlCommand("SELECT * FROM example_tbl;", connection);
- using var reader =await command.ExecuteReaderAsync();
- while ( await reader.ReadAsync())
- {
- Console.WriteLine($"user_id={reader.GetString(0)},city={reader.GetString(2)}");
-
- }
-
- return "ok";
- }
官方的地址:https://mysqlconnector.net/tutorials/basic-api/
连接字符串的说明:https://mysqlconnector.net/connection-options/
也可以整合Dapper,请看官网:Tutorial: Connect to MySQL with Dapper using C# - MySqlConnector
EFCore测试了下,没成功。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。