gpt4 book ai didi

c# - 连接尝试失败,因为连接方在一段时间后没有正确响应。从 C# 调用存储过程时

转载 作者:行者123 更新时间:2023-12-05 04:27:55 27 4
gpt4 key购买 nike

我正在尝试从我的 mysql 数据库中调用一个存储过程,下面是我的 C# 程序中的代码,但我一直遇到同样的错误

SocketException: A connection attempt failed because the connectedparty did not properly respond after a period of time, or establishedconnection 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 errorencountered attempting to read the resultset. --->MySql.Data.MySqlClient.MySqlException (0x80004005): Reading from thestream has failed. ---> System.IO.IOException: Unable to read datafrom the transport connection: A connection attempt failed because theconnected party did not properly respond after a period of time, orestablished connection failed because connected host has failed torespond.. ---> System.Net.Sockets.SocketException (10060): Aconnection attempt failed because the connected party did not properlyrespond after a period of time, or established connection failedbecause connected host has failed to respond. atSystem.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,Int32 count) --- End of inner exception stack trace --- atSystem.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset,Int32 count) at MySql.Data.MySqlClient.TimedStream.Read(Byte[]buffer, Int32 offset, Int32 count) atMySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[]buffer, Int32 offset, Int32 count) atMySql.Data.MySqlClient.MySqlStream.LoadPacket() atMySql.Data.MySqlClient.MySqlStream.LoadPacket() atMySql.Data.MySqlClient.MySqlStream.ReadPacket() atMySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow,Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32statementId, Int32& affectedRows, Int64& insertedId) atMySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Booleanforce) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() atMySql.Data.MySqlClient.MySqlDataReader.NextResult() atMySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehaviorbehavior) atMySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehaviorbehavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() atVulnerabilityDB.SighburDatabaseConnect.BasicCveInfoFill() inC:\Dev\pcs\PcsProjects\SbomArenaC\VulnerabilityDB\SighburDatabaseConnect.cs:line65

最佳答案

尝试从你的命令中删除超时

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com