我是C#development的新手。有人请给我带有文本文件的示例解决方案,用SQL读/写
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我是C#development的新手。有人请给我带有文本文件的示例解决方案,用SQL读/写相关的知识,希望对你有一定的参考价值。
我是C#development的新手。有人请给出一个带有文本文件的示例解决方案,用SQL读/写。
答案
FXCalculation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Globalization;
namespace EmployeeValidator
{
class FXCalculation
{
public void ProcessData( string sourcefolder,string sourcefilename,stringerrorfolder,string errorfilename,string archievefolder,string archievefile,stringsqlcon)
{
//call all the method inside this
// throw user defined exception “FXCalculationException if folder or files doesn’t exists
FXCalculation fxcalculation = new FXCalculation();
List<Trade> trades = new List<Trade>();
trades = fxcalculation.ReadAlltextfromfile(sourcefolder,sourcefilename);
List<Trade> validtrades = fxcalculation.PickValidTradeDetails(trades, errorfolder, errorfilename);
fxcalculation.SendValidTradeDetailsToDB(validtrades, sqlcon);
List<FxRate> fxrates= fxcalculation.CalculateFXRate(sqlcon);
SaveFxRatestoDB(fxrates, sqlcon);
}
public List<Trade> ReadAlltextfromfile(string sourcefolder,string sourcefilename)
{
List<Trade> trades = new List<Trade>();
//read all the text from file
//return it in a list
try
{
trades = File.ReadLines(sourcefolder + sourcefilename)
.Select(i => i.Split(','))
.Select(g => new Trade()
{
tradeId = g[0],
ISIN = g[1],
tradedate = g[2],
maturitydate = g[3],
currency = g[4],
amount = g[5],
tradetype = g[6],
schemename = g[7]
}).ToList();
return trades;
}
catch (FileNotFoundException ex)
{
throw;
}
}
public List<Trade> PickValidTradeDetails(List<Trade> list,stringerrorlogfolder,string errorlogfile)
{
List<Trade> validtrades = new List<Trade>();
List<Trade> invalidTrades = new List<Trade>();
bool isvalid = true;
//get the valid details based on below conditions and return it in list
//call method SendInvalidTradingdetailsToLog() for invalid details
//tradeId should starts with “TR” and not be null
//ISIN should starts with “INSI” and followed by 3 numeric digits
//trade date should be in format(mm/dd/yyyy)
//maturity date should be in format(mm/dd/yyyy) and maturity years should be greater than 5 from trade date
//schemename should not be numeric
//tradetype should not be null,
//Currency should be 3 digits
//Amount should be numeric and not null
Regex IsTradeId = new Regex(@"^[TR][0-9]+$");
Regex IsISIN = new Regex(@"^[INSI][0-9]{3}$");
Regex Isscheme = new Regex(@"^[a-zA-Z]+$");
Regex Iscur = new Regex(@"^[A-Z]{3}$");
Regex IsAmo = new Regex(@"^[0-9]+$");
foreach (var item in list)
{
DateTime oparsed, dparsed;
if ((item.tradeId.Substring(0,2) == "TR" && item.tradeId != string.Empty) &&
(item.ISIN.Substring(0,4) == "INSI" && Regex.Match(item.ISIN, @"d{3}$").Success) &&
(DateTime.TryParseExact(item.tradedate, "MM/dd/yyyy", CultureInfo.InvariantCulture,DateTimeStyles.None,out oparsed) &&
(DateTime.TryParseExact(item.maturitydate, "MM/dd/yyyy",CultureInfo.InvariantCulture,DateTimeStyles.None, out dparsed) &&
(Regex.Match(item.schemename,@"^[a-zA-Z]+$").Success))))
{
if((DateTime.ParseExact(item.tradedate,"MM/dd/yyyy", newCultureInfo("en-US")) -
}
if (!IsTradeId.IsMatch(item.tradeId) || !IsISIN.IsMatch(item.ISIN) || !Isscheme.IsMatch(item.schemename) || !Iscur.IsMatch(item.currency) || !IsAmo.IsMatch(item.amount))
{
isvalid = false;
}
if (item.tradetype == null || item.tradetype==string.Empty)
{
isvalid = false;
}
DateTime outdatetime,outdatetime1;
if (!DateTime.TryParse(item.tradedate, new CultureInfo("en-US"), DateTimeStyles.None,out outdatetime))
{
isvalid = false;
}
if (!DateTime.TryParse(item.maturitydate,new CultureInfo("en-US"),DateTimeStyles.None,out outdatetime1))
{
isvalid = false;
}
if (!isvalid)
{
invalidTrades.Add(item);
}
else
{
validtrades.Add(item);
}
}
if (invalidTrades.Count()>0)
{
SendInvalidTradingdetailsToLog(invalidTrades, errorlogfolder, errorlogfile);
}
return validtrades;
}
public bool SendInvalidTradingdetailsToLog(List<Trade> trades,stringerrorfolder,string errorfilename)
{
//Send invalid details to error log folder with filename extensions ErrorLog_mm/dd
//return true if the file is saved successfully
using (StreamWriter sw = File.AppendText(errorfolder + errorfilename + "ErrorLog_mm/dd"))
{
foreach (var item in trades)
{
sw.WriteLine(item.tradeId + ',' + item.ISIN + ',' + item.tradedate + ',' + item.maturitydate + ',' + item.currency + ',' + item.amount + ',' + item.tradetype + ',' + item.schemename);
}
}
return true;
}
public bool SendValidTradeDetailsToDB(List<Trade> trades,string slconn)
{
// save the values inside the validtrade list
// save the details to DB to the TradingDetails_Table
int i = -1;
SqlConnection con = new SqlConnection(slconn);
foreach (var item in trades)
{
using (SqlCommand cmd = new SqlCommand("Insert into TradingDetails_Table Values(@TradeId,@ISIN,@TradeDate,@MaturityDate,@Currency,@Amount,@Tradetype,@Schemename)", con))
{
cmd.Parameters.AddWithValue("@TradeId", item.tradeId);
cmd.Parameters.AddWithValue("@ISIN", item.ISIN);
cmd.Parameters.AddWithValue("@TradeDate", item.tradedate);
cmd.Parameters.AddWithValue("@MaturityDate", item.maturitydate);
cmd.Parameters.AddWithValue("@Currency", item.currency);
cmd.Parameters.AddWithValue("@Amount", item.amount);
cmd.Parameters.AddWithValue("@Tradetype", item.tradetype);
cmd.Parameters.AddWithValue("@Schemename", item.schemename);
con.Open();
i=cmd.ExecuteNonQuery();
con.Close();
}
}
//if its saved successfully
if (i > -1)
{
return true;
}
else
return false;
}
public List<FxRate> CalculateFXRate(string sqlconn)
{
List<FxRate> Fxrates = new List<FxRate>();
SqlConnection con = new SqlConnection(sqlconn);
// get the trade details from the DB where Tradetype = “FX”
// calculate FX rate based on the below conditions
// If Currency = “USD” then CalculatedFxrate = amount * 0.5
// If Currency = “BPS” then amount *0.6
// If Currency = “EUR” then amount *0.7
// If Currency = “GER” then amount *1
using (SqlCommand cmd= new SqlCommand("select TradeId,Currency,Amount From TradeDetails_Table Where Tradetype='FX'",con))
{
con.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
以上是关于我是C#development的新手。有人请给我带有文本文件的示例解决方案,用SQL读/写的主要内容,如果未能解决你的问题,请参考以下文章