Hi There
I'm trying to insert a CSV file into SQL Database using C#. I can read the csv file and insert it into the table, however any fields with an embebbed comma are not being read correctly. How can I get around this.
Below is the code that I'm using:
I'm trying to insert a CSV file into SQL Database using C#. I can read the csv file and insert it into the table, however any fields with an embebbed comma are not being read correctly. How can I get around this.
Below is the code that I'm using:
Code:
protected void cmdUploadFile_Click(object sender, EventArgs e) { conStr = "workstation id=" + ServerName + ";packet size=4096;user id=sa;password=" + Pass + ";data source=" + ServerName + ";persist security info=False;initial catalog="; conStr = conStr + DB; string filepath = "D:\\Work\\Vukani\\CEMSSample10.csv"; StreamReader sr = new StreamReader(filepath); int NrLines = 0; string[,] mline; mline = new string[NrLines, 50]; int cntra = 0; int counter = 0; using (StreamReader cr = new StreamReader(filepath)) { while ((cr.ReadLine()) != null) { NrLines++; } cr.Close(); } mline = new string[NrLines, 25]; for (int lcounter = 1; (lcounter <= NrLines); lcounter++) { string[] sline = sr.ReadLine().Split(','); //strElem = strElem.Append(""); if (sline != null) { for (int c = 0; c < sline.Length; c++) mline[cntra, c] = sline[c]; cntra++; } } sr.Close(); for (counter = 1; counter < NrLines; counter++) { string Date = mline[counter, 0].ToString(); string SiteUD = mline[counter, 1].ToString(); string SiteName = mline[counter, 2].ToString(); string ModelNo = mline[counter, 3].ToString(); string MachID = mline[counter, 4].ToString(); string Manufacture = mline[counter, 5].ToString(); string TotalCashIn = mline[counter, 6].ToString(); string TotalCashOut = mline[counter, 7].ToString(); string NotesIN = mline[counter, 8].ToString(); string CoinsIn = mline[counter, 9].ToString(); string CoinsOut = mline[counter, 10].ToString(); string CoinstoDrop = mline[counter, 11].ToString(); string RemoteCashIn = mline[counter, 12].ToString(); string RemoteCashOut = mline[counter, 13].ToString(); string TotalWin = mline[counter, 14].ToString(); string TotalBet = mline[counter, 15].ToString(); string GGR = mline[counter, 16].ToString(); string GamesPlayed = mline[counter, 17].ToString(); string HandPays = mline[counter, 18].ToString(); string HopperRefill = mline[counter, 19].ToString(); SQL = "INSERT INTO ztrewVNLCemsImport " + "([Date], [SiteUD], [SiteName], [ModelNo.], [MachID], " + "[Manufacture], [TotalCashIn], [TotalCashOut], [NotesIN], [CoinsIn], " + "[CoinsOut], [CoinstoDrop], [RemoteCashIn], [RemoteCashOut], [TotalWin], " + "[TotalBet], [GGR], [GamesPlayed], [HandPays], [HopperRefill] ) " + "VALUES " + "('" + Date + "', '" + SiteUD + "', '" + SiteName + "', '" + ModelNo + "', '" + MachID + "', " + "'" + Manufacture + "', '" + TotalCashIn + "', '" + TotalCashOut + "', '" + NotesIN + "', '" + CoinsIn + "', " + "'" + CoinsOut + "', '" + CoinstoDrop + "', '" + RemoteCashIn + "', '" + RemoteCashOut + "', '" + TotalWin + "', " + "'" + TotalBet + "', '" + GGR + "', '" + GamesPlayed + "', '" + HandPays + "', '" + HopperRefill + "') "; SQL = SQL.Replace('\t', ' ');
Comment