Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I did run this code through StyleCop. Goal was to optimize the reading, parsing and inserting of a file on the web and write the rows to a database table using a stored procedure. Contrast this (roughly) to my most previous code review here: C# helper class - mailto C# helper class - mailto Different code but I feel like I'm making progress, for being still fairly new to C#. Thanks.

I did run this code through StyleCop. Goal was to optimize the reading, parsing and inserting of a file on the web and write the rows to a database table using a stored procedure. Contrast this (roughly) to my most previous code review here: C# helper class - mailto Different code but I feel like I'm making progress, for being still fairly new to C#. Thanks.

I did run this code through StyleCop. Goal was to optimize the reading, parsing and inserting of a file on the web and write the rows to a database table using a stored procedure. Contrast this (roughly) to my most previous code review here: C# helper class - mailto Different code but I feel like I'm making progress, for being still fairly new to C#. Thanks.

Source Link
Steve
  • 245
  • 2
  • 9

Parsing remote text file and inserting into database table

I did run this code through StyleCop. Goal was to optimize the reading, parsing and inserting of a file on the web and write the rows to a database table using a stored procedure. Contrast this (roughly) to my most previous code review here: C# helper class - mailto Different code but I feel like I'm making progress, for being still fairly new to C#. Thanks.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
namespace NewReleases
{
 public class Program
 {
 public static void Main(string[] args)
 {
 const string PremierPublishers = "PREMIER PUBLISHERS";
 const string NewReleasesFor = "New Releases For";
 ReleaseItem releaseItem = new ReleaseItem();
 List<string> premierPublishers = Release.PremierPublishers();
 DateTime releaseDate = new DateTime();
 foreach (var line in Release.GetRelease())
 {
 if (line.Contains(NewReleasesFor))
 {
 releaseDate = DateTime.Parse(line.Substring(line.Length - 10));
 }
 else
 {
 if (!line.Any(p => p.ToString().Contains("\t")) & !premierPublishers.Any(p => p == line))
 {
 releaseItem.Category = line.Trim();
 }
 else if (premierPublishers.Any(p => p == line))
 {
 releaseItem.Publisher = line.Trim();
 }
 else
 {
 string[] lineitem = line.Split('\t');
 if (lineitem.Count() == 3)
 {
 releaseItem.ItemCode = lineitem[0].Trim();
 releaseItem.Title = lineitem[1].Trim();
 releaseItem.Price = lineitem[2].Trim();
 if (releaseItem.Category != PremierPublishers & !premierPublishers.Any(p => p == line))
 {
 releaseItem.Publisher = null;
 }
 Release.WriteRelease(releaseDate, releaseItem);
 }
 }
 }
 }
 } 
 }
}

Release.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Dapper;
namespace NewReleases
{
 public static class Release
 {
 private const string RemoteReleaseFile = "http://www.previewsworld.com/shipping/newreleases.txt";
 public static IEnumerable<string> GetRelease()
 {
 using (WebClient webClient = new WebClient())
 {
 Stream stream = webClient.OpenRead(RemoteReleaseFile);
 List<string> lines = new List<string>();
 using (StreamReader streamReader = new StreamReader(stream))
 {
 while (!streamReader.EndOfStream)
 {
 lines.Add(streamReader.ReadLine());
 }
 }
 return lines.Where(f => !string.IsNullOrWhiteSpace(f));
 }
 }
 public static List<string> PremierPublishers()
 {
 using (IDbConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Previews"]))
 {
 return connection.Query<string>("Select PremierPublisher From PremierPublishers").ToList();
 }
 }
 
 public static void WriteRelease(DateTime releaseDate, ReleaseItem releaseItem)
 {
 using (IDbConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Previews"]))
 {
 decimal price = 0;
 string note = null;
 bool isDecimal = decimal.TryParse(releaseItem.Price.Replace("$", string.Empty), out price);
 if (!isDecimal)
 {
 note = releaseItem.Price;
 }
 else
 {
 price = Convert.ToDecimal(releaseItem.Price.Replace("$", string.Empty));
 }
 
 connection.Execute(
 "InsertReleaseItem",
 new
 {
 releaseDate,
 releaseItem.Category,
 releaseItem.Publisher,
 releaseItem.ItemCode,
 releaseItem.Title,
 price,
 note 
 },
 commandType: CommandType.StoredProcedure);
 }
 }
 }
}

ReleaseItem.cs

namespace NewReleases
{
 public class ReleaseItem
 {
 public string Category { get; set; }
 public string Publisher { get; set; }
 public string ItemCode { get; set; }
 public string Title { get; set; }
 public string Price { get; set; }
 }
}
lang-cs

AltStyle によって変換されたページ (->オリジナル) /