I want to load .csv file and then import to SQl database. But before I do that, I want to do something to delete first field (in header) and also all values of this field.
this below is the example of my .csv file :
> TableId|PERIODE|DATEBALANCEASOF|ACCCODE|CUSTNAME|CUSTGROUP|
> TB_001|201501|2015-01-01|11-0001|DYNAMIC EXPRESS|11|
> TB_001|201501|2015-01-01|11-0002|DYNAMIC EXPRESS|12|
> TB_001|201501|2015-01-01|11-0003|DYNAMIC EXPRESS|13|
> TB_001|201501|2015-01-01|11-0004|DYNAMIC EXPRESS|14|
before I import that .csv file, I need my .csv file willbe like this below :
PERIODE|DATEBALANCEASOF|ACCCODE|CUSTNAME|CUSTGROUP|
201501|2015-01-01|11-0001|DYNAMIC EXPRESS|11|
201501|2015-01-01|11-0002|DYNAMIC EXPRESS|12|
201501|2015-01-01|11-0003|DYNAMIC EXPRESS|13|
201501|2015-01-01|11-0004|DYNAMIC EXPRESS|14|
this below is my code :
public void readCSVManual(string pathLocalSuccess, string pathHistory, string modul)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"server=" + serverDB + "; database=" + DB + "; Trusted_Connection=" + trustedConnection + "; user=" + UserDB + "; password=" + PassDB + "";
string[] files = Directory.GetFiles(pathLocalSuccess);
if (files == null)
{
MessageBox.Show("Files not found");
}
foreach (string file in files)
{
FileInfo fileInf = new FileInfo(file);
string filename = fileInf.Name;
StreamReader reader = new StreamReader(file);
string line = reader.ReadLine();
string[] value = line.Split('|');
var list = new List<string>(value);
list.RemoveAt(0);
value = list.ToArray();
//string[] v = string(nValue.ToArray());
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while (!reader.EndOfStream)
{
value = reader.ReadLine().Split('|');
if (value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "ACC_004";
bc.BatchSize = dt.Rows.Count;
bc.WriteToServer(dt);
bc.Close();
con.Close();
reader.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Please help me to resolve this problem..