我正在尝试从我的 mysql 数据库中调用一个存储过程,下面是我的 C# 程序中的代码,但我一直遇到同样的错误
SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
在数据库中查看存储过程是否正常工作,所以有没有一种方法可以检查该过程何时完成 (第一次问关于堆栈溢出的问题我希望我提供了足够的信息,如果还有什么让我知道)
这是我的 C# 代码
public void BasicCveInfoFill()
{
MySqlTransaction transaction = null;
try
{
using(MySqlConnection conn = GetConnection())
{
conn.Open();
using(MySqlCommand cmd = new MySqlCommand())
{
cmd.Connection= conn;
cmd.Transaction = transaction;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"cveTableFill";
var rows = cmd.ExecuteNonQuery();
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
GetConnection方法(切换到随机数据库)
public MySqlConnection GetConnection()
{
string connString = @"server=localhost;user=root;database=test;password=";
MySqlConnection conn = new MySqlConnection(connString);
return conn;
}
这是存储过程的代码
DROP PROCEDURE IF EXISTS cveTableFill;
DELIMITER ;;
CREATE PROCEDURE cveTableFill()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM cpe INTO n;
SET i=0;
WHILE i<n DO
INSERT IGNORE INTO cve(cve_name, cpeMatchCount) SELECT c.cveMatch, COUNT(c.cveMatch) FROM cpe c GROUP BY c.cveMatch HAVING COUNT(c.cveMatch);
SET i = i + 1;
END WHILE;
End;;
DELIMITER ;
这是完整的异常
MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from the stream has failed. ---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.. ---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count) at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) at MySql.Data.MySqlClient.MySqlStream.LoadPacket() at MySql.Data.MySqlClient.MySqlStream.LoadPacket() at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at VulnerabilityDB.SighburDatabaseConnect.BasicCveInfoFill() in C:\Dev\pcs\PcsProjects\SbomArenaC\VulnerabilityDB\SighburDatabaseConnect.cs:line 65
最佳答案
尝试从你的命令中删除超时
cmd.CommandTimeout = 0;
该命令的默认超时时间为 30 秒
https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.commandtimeout
The time in seconds to wait for the command to execute. The default is 30 seconds.
关于c# - 连接尝试失败,因为连接方在一段时间后没有正确响应。从 C# 调用存储过程时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72733996/