Example #1
0
File: Vendor.cs Project: rmc00/gsf
        /// <summary>
        /// Saves <see cref="Vendor"/> information to database.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="vendor">Information about <see cref="Vendor"/>.</param>        
        /// <returns>String, for display use, indicating success.</returns>
        public static string Save(AdoDataConnection database, Vendor vendor)
        {
            bool createdConnection = false;
            string query;

            try
            {
                createdConnection = CreateConnection(ref database);

                if (vendor.ID == 0)
                {
                    query = database.ParameterizedQueryString("INSERT INTO Vendor (Acronym, Name, PhoneNumber, ContactEmail, URL, UpdatedBy, UpdatedOn, CreatedBy, CreatedOn) " +
                        "VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})", "acronym", "name", "phoneNumber", "contactEmail", "url", "updatedBy", "updatedOn", "createdBy",
                        "createdOn");
                    database.Connection.ExecuteNonQuery(query, DefaultTimeout, vendor.Acronym.Replace(" ", "").ToUpper(),
                        vendor.Name, vendor.PhoneNumber.ToNotNull(), vendor.ContactEmail.ToNotNull(), vendor.URL.ToNotNull(), CommonFunctions.CurrentUser, database.UtcNow,
                        CommonFunctions.CurrentUser, database.UtcNow);
                }
                else
                {
                    query = database.ParameterizedQueryString("UPDATE Vendor SET Acronym = {0}, Name = {1}, PhoneNumber = {2}, ContactEmail = {3}, " +
                        "URL = {4}, UpdatedBy = {5}, UpdatedOn = {6} WHERE ID = {7}", "acronym", "name", "phoneNumber", "contactEmail", "url", "updatedBy",
                        "updatedOn", "id");

                    database.Connection.ExecuteNonQuery(query, DefaultTimeout, vendor.Acronym.Replace(" ", "").ToUpper(), vendor.Name,
                        vendor.PhoneNumber.ToNotNull(), vendor.ContactEmail.ToNotNull(), vendor.URL.ToNotNull(), CommonFunctions.CurrentUser, database.UtcNow, vendor.ID);
                }

                return "Vendor information saved successfully";
            }
            finally
            {
                if (createdConnection && database != null)
                    database.Dispose();
            }
        }
Example #2
0
File: Vendor.cs Project: rmc00/gsf
        /// <summary>
        /// Loads <see cref="Vendor"/> information as an <see cref="ObservableCollection{T}"/> style list.
        /// </summary>
        /// <param name="database"><see cref="AdoDataConnection"/> to connection to database.</param>
        /// <param name="keys">Keys of the Measurement to be loaded from the database</param>
        /// <returns>Collection of <see cref="Vendor"/>.</returns>
        public static ObservableCollection<Vendor> Load(AdoDataConnection database, IList<int> keys)
        {
            bool createdConnection = false;

            try
            {
                createdConnection = CreateConnection(ref database);

                string query;
                string commaSeparatedKeys;

                Vendor[] vendorList = null;
                DataTable vendorTable;
                int id;

                if ((object)keys != null && keys.Count > 0)
                {
                    commaSeparatedKeys = keys.Select(key => key.ToString()).Aggregate((str1, str2) => str1 + "," + str2);
                    query = string.Format("SELECT ID, Acronym, Name, PhoneNumber, ContactEmail, URL FROM VendorDetail WHERE ID IN ({0})", commaSeparatedKeys);
                    vendorTable = database.Connection.RetrieveData(database.AdapterType, query);
                    vendorList = new Vendor[vendorTable.Rows.Count];

                    foreach (DataRow row in vendorTable.Rows)
                    {
                        id = row.ConvertField<int>("ID");

                        vendorList[keys.IndexOf(id)] = new Vendor()
                        {
                            ID = id,
                            Acronym = row.Field<string>("Acronym"),
                            Name = row.Field<string>("Name"),
                            PhoneNumber = row.Field<string>("PhoneNumber"),
                            ContactEmail = row.Field<string>("ContactEmail"),
                            URL = row.Field<string>("URL")
                        };
                    }
                }

                return new ObservableCollection<Vendor>(vendorList ?? new Vendor[0]);
            }
            finally
            {
                if (createdConnection && database != null)
                    database.Dispose();
            }
        }