0

我要再问你一个愚蠢的问题。

我收到以下错误:

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();
        }
    }

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

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

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

4

1 回答 1

0

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

于 2013-05-23T10:45:06.707 回答