Advertisement

06.30.2008 at 12:18PM PDT, ID: 23528006
[x]
Attachment Details

Conditional import of a csv file

Asked by kikimayhay in Microsoft Visual C#.Net, Databases Miscellaneous

Tags: C#, http://www.codeproject.com/KB/database/FinalCSVReader.aspx

I am attempting to modify the code found on: http://www.codeproject.com/KB/database/FinalCSVReader.aspx
to me it looks like it should be possible to add a where clause to the select statement (sql_select = "select * from ["+ filetable +"]";) but so far I have been unsuccessful.  Is it possible to add a where clause to make the csv file upload only the data I want or will I have to load it to a temp table and then remove extraneous data?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
public DataSet ConnectCSV(string filetable)
		{
			DataSet ds = new DataSet();
			try
			{		
				// You can get connected to driver either by using DSN or connection string
 
				// Create a connection string as below, if you want to use DSN less connection. The DBQ attribute sets the path of directory which contains CSV files
				string strConnString="Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Extensions=asc,csv,tab,txt;Persist Security Info=False";
				string sql_select;								
				System.Data.Odbc.OdbcConnection conn;		
		
				//Create connection to CSV file
				conn	=	new System.Data.Odbc.OdbcConnection(strConnString.Trim());
 
				// For creating a connection using DSN, use following line
				//conn	=	new System.Data.Odbc.OdbcConnection(DSN="MyDSN");
				
				//Open the connection 
				conn.Open();
				//Fetch records from CSV
				sql_select		=	"select * from ["+ filetable +"]";
				
				obj_oledb_da	=	new System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
				//Fill dataset with the records from CSV file
				obj_oledb_da.Fill(ds,"Stocks");
				
				//Set the datagrid properties
				
				dGridCSVdata.DataSource=ds;
				dGridCSVdata.DataMember="Stocks";
				//Close Connection to CSV file
				conn.Close();				
			}
			catch(Exception e) //Error
			{
				MessageBox.Show(e.Message);
			}
			return ds;
		}
 
		# endregion
 
 
Loading Advertisement...
 
[+][-]06.30.2008 at 01:32PM PDT, ID: 21902788

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, Databases Miscellaneous
Sign Up Now!
Solution Provided By: DarkoLord
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628