ExecuteScalar() public static method

Execute a single command against a MySQL database.
public static ExecuteScalar ( MySqlConnection connection, string commandText ) : object
connection MySqlConnection object to use
commandText string Command text to use for the command
return object
        // Проверяет, что поставщик может работать и работает с данным клиентом.
        private bool SupplierAvaliableForClient(ulong supplierId, ulong addressId)
        {
            var supplier = new MySqlParameter("?SupplierId", MySqlDbType.Int32);

            supplier.Value = supplierId;
            var address = new MySqlParameter("?AddressId", MySqlDbType.Int32);

            address.Value = addressId;

            return(With.Connection(c => Convert.ToInt32(MySqlHelper.ExecuteScalar(c, @"
SELECT count(i.Id)
FROM Customers.Addresses a
	JOIN Customers.Suppliers as s ON s.Id = ?SupplierId
	JOIN usersettings.pricesdata as prices ON prices.FirmCode = s.Id
	JOIN Customers.intersection as i ON i.ClientId = a.ClientId
		and i.LegalEntityId = a.LegalEntityId
		and i.PriceId = prices.PriceCode
		join Customers.AddressIntersection ai on ai.IntersectionId = i.Id and ai.AddressId = a.Id
WHERE a.Id = ?AddressId
	AND i.AgencyEnabled = 1
	AND i.AvailableForClient = 1
	AND prices.enabled = 1
	AND prices.AgencyEnabled = 1
",
                                                                                  supplier, address)) > 0));
        }
        /// <summary>
        /// Проверяет, существует ли клиент с указанным кодом.
        /// Также ищет указанный код среди адресов в таблице Customers.Addresses,
        /// поэтому можно сказать, что также проверяет адрес клиента на существование
        /// </summary>
        private bool ClientExists(uint checkClientCode)
        {
            var queryGetClientCode = String.Format(@"
SELECT Addr.Id
FROM Customers.Addresses Addr
WHERE Addr.Id = {0}",
                                                   checkClientCode);

            return(With.Connection(c => {
                var clientCode = MySqlHelper.ExecuteScalar(c, queryGetClientCode);
                return (clientCode != null);
            }));
        }
Exemplo n.º 3
0
    protected void btnApply_Click(object sender, EventArgs e)
    {
        CopyChangesToTable(dgvNonOptional, dtNonOptionalParams, PPropertyValue.ColumnName);
        CopyChangesToTable(dgvOptional, dtOptionalParams, OPPropertyValue.ColumnName);

        MyCn.Open();

        var drows = DS.Tables[dtOptionalParams.TableName].Rows.Cast <DataRow>().Where(dr => (dr.RowState == DataRowState.Added) && (dr[OPrtpID.ColumnName] is DBNull)).ToArray();

        for (var i = 0; i < drows.Count(); i++)
        {
            DS.Tables[dtOptionalParams.TableName].Rows.Remove(drows[i]);
        }

        foreach (DataRow dr in DS.Tables[dtOptionalParams.TableName].Rows)
        {
            if (dr.RowState == DataRowState.Added)
            {
                dr[OPPropertyValue.ColumnName] = MySqlHelper.ExecuteScalar(MyCn, "SELECT DefaultValue FROM reports.report_type_properties WHERE ID=" + dr[OPrtpID.ColumnName]);
            }
        }
        MyCn.Close();

        MyCn.Open();
        var trans = MyCn.BeginTransaction(IsolationLevel.ReadCommitted);

        try {
            var requiredParameters = DS.Tables[dtNonOptionalParams.TableName];

            var baseCostRows   = requiredParameters.Select("PPropertyName = 'ByBaseCosts'");
            var weightCostRows = requiredParameters.Select("PPropertyName = 'ByWeightCosts'");
            var clientRows     = requiredParameters.Select("PPropertyName = 'ClientCode'");
            if (baseCostRows.Length > 0 && weightCostRows.Length > 0 && clientRows.Length > 0)
            {
                if (baseCostRows[0]["PPropertyValue"].ToString() == "1" ||
                    weightCostRows[0]["PPropertyValue"].ToString() == "1")
                {
                    clientRows[0]["PPropertyValue"] = 1;
                }
            }

            ApplyNonOptimal(trans);
            ApplyOptimal(trans);

            trans.Commit();
            PostData();
        }
        catch {
            trans.Rollback();
            throw;
        }
        finally {
            MyCn.Close();
        }
        if (dgvNonOptional.Rows.Count > 0)
        {
            btnApply.Visible = true;
        }
        else
        {
            btnApply.Visible = false;
        }
    }