Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is the csv file

PTNAME,REGNO/ID,BLOOD GRP,WARD NAME,DOC NAME,XRAY,PATHO,MEDICATION,BLOOD GIVEN
Mr.MISHRA SABHAPRASAD RAMNARESH,SH1312/00804,,SEMI DELUXE 05,SHELKE SAMEER,"X RAY LEFT HIP WITH THIGH AP/LAT --ACCEPTABLE WITH IMPLANT IN SITU WITH ACETABULAR CUP ARTHRITIC CHANGES 2 D ECHO-MILD CONC LVH GOOD LV DIASTOLIC FUNCTION ALTERED LV DIASTOLIC FUNCTION LVEF 55 % MRI BRAIN- FEW OLD ISCHEMIC CHNGES IN BILATERAL CEREBRAL WHITE MATTER MRI L-S SPINE WITH SCREENING OF WHOLE SPINE- PID-L3-4,L5-S1 PID C3- 4TO C6-7 ",HB- 11.4 BSL -206.4 SR CREAT-1.7 T3-0.74 T4-11.0 TSH-1.79 SR UREA-23 BLOOD GROUP- B RH POSITIVE PT INR-15/15/1 HIV AND HBSAG - NEGATIVE, IV DICLOGESIC RR DRIP 1-0-1 TAB TACIL 1-0-1 TAB ARCOPAN D 1-0-1 CAP GEMCAL PLUS 1 -0-1 TAB ANXIT 0.5 MG 0-0-1 ARCIZEN GEL LA 1-1-1-1 ,I POINT PCV GIVEN ON 6/4/2015 B RH POSITIVE


in this file from PTNAME to BLOOD GIVEN is headers and file is in proper format. i want to read this file and save in sql server database table. my simple import method doesnt work for this file i need to add some lines in my code bt im confused how to do that .

here is my simple import method if anyone can just edit my method and write code to read and save data in table.

C#
public void ImportAllFilesOfFolder()//function declares methods to import//
{
try
{
SqlConnection=new SqlConnection(Properties.Settings.Default.HospitalProjectConnectionString);
con.Open();
string sourceDir = txtsend.Text;
var IcsvFile = Directory.EnumerateFiles(sourceDir, "*.csv");

foreach (string currentFile in IcsvFile)
{
StreamReader sr = new StreamReader(currentFile);
string line = sr.ReadLine();
string[] value = line.Split(',');
DataTable dt = new DataTable();
DataRow row;

foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}

while (!sr.EndOfStream)
{
value = sr.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 = "New";
bc.BatchSize = dt.Rows.Count;
bc.WriteToServer(dt);
bc.Close();
}
}

catch
{

}
finally { con.Close(); }
}
Posted
Updated 20-Apr-15 22:16pm
v2
Comments
Rob Philpott 21-Apr-15 4:33am    
I hope that's not someone's real medical record...
Member 11543226 22-Apr-15 2:00am    
yes this is given to me by hospital databse.

Don't use Streamreader to read CSV, you can import it directly as described in Using OleDb to Import Text Files (tab, CSV, custom)[^].
 
Share this answer
 
Comments
Member 11543226 21-Apr-15 5:51am    
sir is it possible to use sql for this task?
Richard MacCutchan 21-Apr-15 8:39am    
Why not go and study the article, it is not too difficult to implement.
Member 11543226 22-Apr-15 1:17am    
sir give me link for the article i could not found it.
Richard MacCutchan 22-Apr-15 3:01am    
The link is in my answer above!
Member 11543226 22-Apr-15 8:18am    
I solved my issue by using varchar(max) datatype. thanks Richard Sir.
I solved my issue by using varchar(max) datatype. thanks Richard Sir.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900