예제 #1
0
    public static bool FillDataTable_ViaCmd(ref DataTable ReturnTable, ref SqlCommand SqlCmd)
    {
        System.Data.SqlClient.SqlDataAdapter lo_Ada = new System.Data.SqlClient.SqlDataAdapter();
        DataTable     Return_DataTable = new DataTable();
        SqlConnection ActiveConn;

        if (!OpenConnection())
        {
            return(false);
        }
        else
        {
            ActiveConn = lo_Connection;
        }

        SqlCmd.Connection     = ActiveConn;
        SqlCmd.CommandTimeout = CommandTimeOutSeconds;

        int Retry = 2;

        while (Retry >= 0)
        {
            try
            {
                lo_Ada.SelectCommand = SqlCmd;
                lo_Ada.Fill(Return_DataTable);
                lo_Ada.Dispose();
                lo_Ada = null;
                ActiveConn.Close();
                ReturnTable = Return_DataTable;
                return(true);
            }
            catch (Exception ex)
            {
                if (Retry >= 1 && ex.Message.Contains("deadlock victim"))
                {
                    System.Threading.Thread.Sleep(3337);
                    Retry -= 1;
                }
                else if (Retry >= 1 && (ex.Message.Contains("INSERT EXEC failed ") || ex.Message.Contains("Schema changed ")))
                {
                    System.Threading.Thread.Sleep(3337);
                    Retry -= 1;
                }
                else
                {
                    ActiveConn.Close();
                    Retry = -1;
                }
            }
        }
        return(false);
    }
예제 #2
0
    public static Hashtable CreateHashTable_ViaCmd(ref SqlCommand SqlCmd)
    {
        // creates a hashtable for a single-row query
        // item key will be the db column name
        // null values are converted to empty strings
        System.Data.SqlClient.SqlDataReader lo_DatR;
        Hashtable     lo_HT;
        int           li_i;
        SqlConnection ActiveConn;

        if (!OpenConnection())
        {
            return(null);
        }
        else
        {
            ActiveConn = lo_Connection;
        }

        try
        {
            SqlCmd.Connection     = ActiveConn;
            SqlCmd.CommandTimeout = CommandTimeOutSeconds;

            lo_DatR = SqlCmd.ExecuteReader(CommandBehavior.SingleRow);
            if (!lo_DatR.HasRows)
            {
                lo_HT = null;
            }
            else if (!lo_DatR.Read())
            {
                lo_HT = null;
            }
            else
            {
                lo_HT = new Hashtable();
                for (li_i = 0; li_i <= lo_DatR.FieldCount - 1; li_i++)
                {
                    if (Convert.IsDBNull(lo_DatR[li_i]))
                    {
                        lo_HT.Add(lo_DatR.GetName(li_i), "");
                    }
                    else
                    {
                        lo_HT.Add(lo_DatR.GetName(li_i), lo_DatR[li_i]);
                    }
                }
                if (lo_HT.Count <= 0)
                {
                    lo_HT = null;
                }
            }
            lo_DatR.Close();

            return(lo_HT);
        }
        catch
        {
            return(null);
        }
        finally
        {
            ActiveConn.Close();
        }
    }