I have created a string array by parsing a long CSV string (read from a .csv file) into its elements (i.e. the bits that are actually seperated by commas). I now wish to add those elements to an SQL Server 2005 database table that I have already created for that purpose.
Can someone please point me at a simple example of how to loop thru the array and add the elements to the appropriate fields of a database.
I am using Visual Studio 2008, C#.NET.
Thanks.
Loading
Sam HobbsPosted Jul 15, 2010, 1:35 AM
The main portion that connects to the database and creates the insert command is:
And then my InsertPerson function is:
ahsan ashfaqPosted Jul 14, 2010, 1:52 AM
-------------------------------
string query
query="insert into table values('"
foreach(string x in stringarray[])
{
query=query+x+"',"
}
query=left(query,(query.length-2)) //Removing last extra comma, please check the indexes
query=query+")"
//here you have your query to run
Matej SkerjancPosted Jun 8, 2010, 9:03 AM
foreach(string x in stringarray[])
{
then you can create data row and fill x into it, and add row to datatable...
really depending on your database organisation
}
Steve GeardPosted May 13, 2010, 7:17 PM
Where I need the help is looping thru the string array and adding each element to a row in a previously created SQL Server database.
CrishPosted May 13, 2010, 2:01 AM
Get data from CSV file use below code.
StreamReader sr = new StreamReader("file.csv");
string inputLine = "";
String[] values = null;
while ((inputLine = input.ReadLine()) != null)
{
values = inputLine.Split(";");
}
input.Close();