【发布时间】:2014-05-05 06:49:17
【问题描述】:
我正在尝试使用 C# 将 CSV 文件插入 SQL 数据库。我可以读取 csv 文件并将其插入表中,但是任何带有嵌入逗号的字段都没有被正确读取。我该如何解决这个问题。 以下是我正在使用的代码:
protected void UploadFile_Click(object sender, EventArgs e)
{
conStr = "workstation id=" + ServerName + ";packet size=4096;user id=xx;password=" yyyyy ";data source=" blahblah ";persist security info=False;initial catalog=";
conStr = conStr + DB;
string filepath = "D:\\Work\\Sample01.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', ' ');
【问题讨论】:
-
您是否考虑过使用
Microsoft.VisualBasic.FileIO.TextFieldParser来解析CSV 文件?它消除了与 CSV 文件相关的所有复杂性。 -
所以 , 符号是每列的分隔符。嵌入的逗号是否以某种方式标记为文本?喜欢介于 2 ' 或 2 " 之间?如果不是,那么即使是第三人称 csv 解析器也会在那里遇到问题。但如果是,你可以使用第三人称 csv 解析器,或者在正则表达式的帮助下调整你的代码,这样那些,不算数。