gpt4 book ai didi

c# - 不使用 System.Net.Sockets 时出现套接字错误

转载 作者:行者123 更新时间:2023-11-30 01:37:16 27 4
gpt4 key购买 nike

我又要问你一个愚蠢的问题了。

我收到以下错误:

A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

我在通过 C# 代码从 MySql 数据库获取一些数据时收到此错误。

下面是我用来连接 MySql 数据库的方法。

public int dl_open_connection(MySqlCommand a_mySqlCommandObj)
{
int l_ret_val = 1;
try
{
int l_db_open_counter = 0;
int numberOfMilliseconds = 0;
string l_msg = "";

//while (true)
for (int i = 0; i < 5; i++)
{
l_db_open_counter = i + 1;
numberOfMilliseconds = Convert.ToInt32(ConfigurationManager.AppSettings["db_open_sleep_timer"].ToString());

if (l_ret_val != 0)
{
//l_msg = string.Format("Connection Attempt-{0} Reconnecting...", l_db_open_counter);
//Console.WriteLine(l_msg);
l_ret_val = init(a_mySqlCommandObj);
if (l_ret_val != 0)
{
l_db_open_counter++;
l_msg = string.Format("Connection Failed. Attempt-{0} Reconnecting...", l_db_open_counter);
//Console.WriteLine(l_msg);
System.Threading.Thread.Sleep(numberOfMilliseconds);
continue;
}
else
{
l_msg = string.Format("Connection Successfully Done. Attempt-{0}", l_db_open_counter);
//Console.WriteLine(l_msg);
break;
}

}
else
{
l_msg = string.Format("Connection Successfully Done. Attempt-{0}", l_db_open_counter);
new BL().writeInRed(l_msg);
break;
}
}

}
catch
{
l_ret_val = 1;
throw;
}
return l_ret_val;

}
public int init(MySqlCommand a_mySqlCommandObj)
{
int l_ret_val = 0;
try
{
if (a_mySqlCommandObj.Connection.State != ConnectionState.Open)
{
a_mySqlCommandObj.Connection.Open();
}
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
new BL().writeInRed("Unable to open the database connection. Re-trying...");
l_ret_val = 1;

}
finally
{

}
return l_ret_val;
}

这就是我所说的地方。

public List<deviceDetails> getAllVehicles(string accountID)
{
List<deviceDetails> resultdevice = new List<deviceDetails>();
string connectionString = ConfigurationManager.AppSettings["connectionString"].ToString();
MySqlConnection connectionObj = new MySqlConnection(connectionString);
try
{
MySqlCommand commandObj = new MySqlCommand("REPORTSERVER_GET_ALL_DEVICES", connectionObj);
commandObj.CommandType = CommandType.StoredProcedure;
dl_open_connection(commandObj);
if (commandObj.Connection.State != ConnectionState.Open) //being on safe side
{
commandObj.Connection.Open();
}
commandObj.Parameters.Add(new MySqlParameter("acc", accountID));
MySqlDataReader readerObj = commandObj.ExecuteReader();
while (readerObj.Read())
{
deviceDetails tempDevice = new deviceDetails();
tempDevice.accountID = readerObj["accountID"].ToString();
tempDevice.deviceID = readerObj["deviceID"].ToString();
tempDevice.displayName = readerObj["displayName"].ToString();
resultdevice.Add(tempDevice);
}
readerObj.Close();
commandObj.Dispose();
return resultdevice;
}
catch (Exception ex)
{
new BL().writeInRed("Problem occured in fetching the vehicle List " + Environment.NewLine + ex.Message.ToString(),true);
throw;
}
finally
{
connectionObj.Close();
connectionObj.Dispose();
}
}

当我尝试执行读取器时出现错误。

请注意,错误不一致。有时出现有时不出现

为什么会出现这个错误?我在任何地方使用套接字吗?如果有,在哪里?我该如何解决这个错误?

最佳答案

您的数据库位于远程服务器还是您计算机的本地?

关于c# - 不使用 System.Net.Sockets 时出现套接字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707483/

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