以下查询在 mysql 中有效。
DROP TEMPORARY TABLE IF EXISTS TempTbl;
create temporary table TempTbl like ACTIVITY;
LOAD DATA local INFILE 'C:\\test\\000000135_20190109133919.flat'
INTO TABLE TempTbl COLUMNS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
但是当我尝试在 .net core 中运行时,出现以下错误。我该如何解决?
var db = new RheEntities(Utils.GetAppSettingsByKey("ConnectionStrings:RheConnection"));
var query = System.IO.File.ReadAllText(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data/BulkInsertTemplate.sql"));
db.Database.ExecuteSqlCommand(query);
异常信息:
Error during LOAD DATA LOCAL INFILE
堆栈跟踪:
at MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet(ResultSet resultSet) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 80
at MySql.Data.MySqlClient.MySqlDataReader.ReadFirstResultSetAsync(IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 302
at MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(MySqlCommand command, CommandBehavior behavior, ResultSetProtocol resultSetProtocol, IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlDataReader.cs:line 287
at MySqlConnector.Core.TextCommandExecutor.ExecuteReaderAsync(String commandText, MySqlParameterCollection parameterCollection, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 37
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 261
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlCommand.cs:line 62
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommand(DatabaseFacade databaseFacade, RawSqlString sql, IEnumerable`1 parameters)
at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.ExecuteSqlCommand(DatabaseFacade databaseFacade, RawSqlString sql, Object[] parameters)
内部异常:
Use SourceStream or SslMode >= VerifyCA for LOAD DATA LOCAL INFILE
详细信息:
.Net core 2.2
Microsoft.EntityFrameworkCore 2.2.1
Pomelo.EntityFrameworkCore.MySql 2.1.4
MySqlConnector 0.47.1
最佳答案
这是 MySqlConnector 中的一项安全功能;见issue 334 .由于您未连接到经过验证的主机,MySqlConnector 可防止信息泄露漏洞,该漏洞可能会让恶意服务器/代理读取您系统上的任何本地文件。
来自 Security Issues with LOAD DATA LOCAL :
There are two potential security issues with the LOCAL version of LOAD DATA:
- The transfer of the file from the client host to the server host is initiated by the MySQL server. In theory, a patched server could be built that would tell the client program to transfer a file of the server's choosing rather than the file named by the client in the LOAD DATA statement. Such a server could access any file on the client host to which the client user has read access.
您可以通过使用 MySqlBulkLoader
类而不是自己构建 SQL 来解决这个问题:
var loader = new MySqlBulkLoader(db.Database)
{
FileName = "C:\\test\\000000135_20190109133919.flat",
TableName = "TempTbl",
FieldTerminator = ",",
FieldQuotationCharacter = '"',
NumberOfLinesToSkip = 1,
};
loader.Load();
关于c# - 加载数据本地 INFILE 无法在 .net 核心上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54566987/