我将再次问你一个愚蠢的问题。
我收到以下错误:
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();
}
}
当我尝试执行阅读器时出现错误。
请注意,错误不一致。有时它会出现,有时它不会
为什么会出现此错误?我在哪里使用套接字?如果有,在哪里?我怎样才能解决这个错误?
答案 0 :(得分:0)
您的数据库是位于远程服务器上还是位于计算机本地?