本文介绍了如何在C#中读取没有标题列的REQ文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个c#控制台应用程序代码来读取文件夹中的REQ文件,它可以使用标题列,但是我的REQ文件没有标题列,例如我的意思是:
I have this c# console application code to read REQ files from folder and it work with header column ,but my REQ file is without header column this e.g. of what I mean :
123123*JAN*100001*[email protected]*CREDITADVICE*SDG*1000000*20170620*F0000999*10.105.1.20
此代码:
this the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
using System.Data;
namespace readREQ
{
class Program
{
static void Main(string[] args)
{
string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
string LogFolder = @"C:\Log\";
try
{
//Declare Variables and provide values
string SourceFolderPath = @"C:\Source\"; //Provide the Source Folder where files are present
string FileExtension = ".txt"; //Provide the extension of files you need to load, can be .txt or .csv
string FileDelimiter = ","; // provide the file delimiter such as comma or pipe
string ArchiveFolder = @"C:\Archive\"; //Provide the archive folder path where files will be moved
string TableName = "dbo.Customer"; //Provide the table name in which you would like to load the files.
//Create Connection to SQL Server in which you like to load files
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "Data Source = (local); Initial Catalog =TechBrothersIT; "
+ "Integrated Security=true;";
//Reading file names one by one
string[] fileEntries = Directory.GetFiles(SourceFolderPath, "*" + FileExtension);
foreach (string fileName in fileEntries)
{
//Writing Data of File Into Table
int counter = 0;
string line;
string ColumnList = "";
System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);
SQLConnection.Open();
while ((line = SourceFile.ReadLine()) != null)
{
if (counter == 0)
{
//By using Header Row, Build Column List
ColumnList = "[" + line.Replace(FileDelimiter, "],[") + "]";
}
else
{
//Build and Execute Insert Statement to insert record
string query = "Insert into " + TableName + " (" + ColumnList + ") ";
query += "VALUES('" + line.Replace(FileDelimiter, "','") + "')";
SqlCommand SQLCmd = new SqlCommand(query, SQLConnection);
SQLCmd.ExecuteNonQuery();
}
counter++;
}
SourceFile.Close();
SQLConnection.Close();
//move the file to archive folder after adding datetime to it
File.Move(fileName, ArchiveFolder + "\\" +
(fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "")
+ "_" + datetime + FileExtension);
}
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(LogFolder
+ "\\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());
}
}
}
}
}
我尝试过:
我试图删除这些行
What I have tried:
I have try to delete these line
if (counter == 0)
{
//By using Header Row, Build Column List
ColumnList = "[" + line.Replace(FileDelimiter, "],[") + "]";
}
但仍有错误
but still there is error
推荐答案
这篇关于如何在C#中读取没有标题列的REQ文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!