Friday, February 17, 2012

csv file to sql table

Hi...

I have .csv file(with headers) that i want to send its data to temporary table in sql.

Do i need to get the data as dataTable and than send it to sql table?

any way, i need help to do so...

thank you...

If you are using SQL 2005, you can use SSIS to load the data. If not see the example in http://www.codeproject.com/aspnet/ImportExportCSV.asp

|||

Hi may215,

may215:

I have .csv file(with headers) that i want to send its data to temporary table in sql.

Do i need to get the data as dataTable and than send it to sql table?

in the past i had a similar problem and solved it at this way you asked. Anyway. If you interested on it, here is a method in C# to read a structured textfile in a DataTable.

1DataTable table =null;2// set the delimeter3char _delim =';';45private void createTable()6{7// check if file exits8if (!File.Exists(_fileName))9{10// you have to change this to youre requirement11this._errorText =string.Format(CultureInfo.InvariantCulture,"Datei {0} ist nicht vorhanden, oder der Zugriff wurde Verweigert.",12this._fileName);13this._hasError =true;14throw new FileNotFoundException(_errorText);15}1617// create new DataTable18table =new DataTable();19table.Locale = CultureInfo.InvariantCulture;2021// String and StringArray initialize22string kontent =null;23string[] kontentArray =null;2425// create a FileStream26FileStream fs = File.OpenRead(_fileName);27// create a StreamReader and read the FileStream28using (StreamReader sr =new StreamReader(fs))29{30// read one line from the textfile , to create the columns in the DataTable31kontent = sr.ReadLine();32kontentArray = kontent.Split(_delim);33int i = 0;34// create the columns in the DataTable,35// the same number like the columns in the file36// beginn with 037foreach (string sin kontentArray)38{39table.Columns.Add(i.ToString(CultureInfo.InvariantCulture),typeof(string));40i++;41}4243// delete the old data from the buffer of the StreamReader44sr.DiscardBufferedData();45// set FileStream position at the line 2 from beginning46sr.BaseStream.Seek(1, SeekOrigin.Begin);4748// read the whole file in the DataTable.49while ((kontent = sr.ReadLine()) !=null)50{51kontentArray = kontent.Split(_delim);52table.Rows.Add(kontentArray);53}54}55// if FileSream not closed, close now56if (fs !=null)57{58fs.Close();59}60}

Hope could help a little.

|||

Hi...

thank you for your answer...

I have an odd problem, my csv file has headers, 10 in Hebrew, and 10 in English , the headers in hebrew are translate to "" sign...the headers in English are ok.

Why is that?

|||

may215:

Why is that?

Different cultures need different codepages and character sets.

may215:

I have an odd problem, my csv file has headers, 10 in Hebrew, and 10 in English , the headers in hebrew are translate to "" sign...the headers in English are ok.

I did not really understand the problem. As far as i understand your first post, you simple want to insert the content of the csv-file in a temporary table. So whats the matter with the headers? You can name the columns in your table however you want. So the header culture from the csv file is irrelevant. In the method i've postet prior, i simple read the header line into an array and count the contents from the array to generate the same count of columns. So when the hebrew content of a column is translated to " ", its also a content of an array. An empty one but a content, and so it is counted and a table column is generated.

If i understand your problem wrong, so please post back.

No comments:

Post a Comment