C# 读取csv文件并访问列以插入guid

C# 读取csv文件并访问列以插入guid,c#,asp.net,csv,C#,Asp.net,Csv,我试图在DataTable的一列上插入GUID以缩短代码: Guid linkid = Guid.NewGuid(); string link = linkid.ToString(); string savepath; HttpPostedFile postedFile = context.Request.Files["Filedata"]; savepath = context.Server.MapPath("files")

我试图在DataTable的一列上插入GUID以缩短代码:

Guid linkid = Guid.NewGuid();
string link = linkid.ToString();

string savepath;  
                HttpPostedFile postedFile = context.Request.Files["Filedata"];
                savepath = context.Server.MapPath("files");
                string filename = postedFile.FileName;
                todelete = savepath + @"\" + filename;
                string forex = savepath + @"\" + filename;
                postedFile.SaveAs(savepath + @"\" + filename);
                DataTable tblcsv = new DataTable();
                tblcsv.Columns.AddRange(new DataColumn[7] { new DataColumn("latitude", typeof(string)), new DataColumn("longitude", typeof(string)), new DataColumn("mps", typeof(int)), new DataColumn("activity_type", typeof(int))
                ,new DataColumn("date_occured" , typeof(DateTime)) , new DataColumn("details" , typeof(string)) , new DataColumn("linkid" , typeof(string))});
                string ReadCSV = File.ReadAllText(forex);

                foreach (string csvRow in ReadCSV.Split('\n'))
                {
                    if (!string.IsNullOrEmpty(csvRow))
                    {
                        //Adding each row into datatable  
                        tblcsv.Rows.Add();
                        int count = 0;
                        foreach (string FileRec in csvRow.Split(','))
                        {
                            if (count > 5) {
                                tblcsv.Rows[tblcsv.Rows.Count - 1][count] = link;
                                break;
                            }

                                tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;
                                count++;



                        }



                    }


                }


                    string consString = ConfigurationManager.ConnectionStrings["diposlConnectionString"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(consString))
                    {
                        using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
                        {
                            //Set the database table name
                            sqlBulkCopy.DestinationTableName = "dbo.tbl_activities";

                            //[OPTIONAL]: Map the Excel columns with that of the database table
                            sqlBulkCopy.ColumnMappings.Add("latitude", "latitude");
                            sqlBulkCopy.ColumnMappings.Add("longitude", "longitude");
                            sqlBulkCopy.ColumnMappings.Add("mps", "mps");
                            sqlBulkCopy.ColumnMappings.Add("activity_type", "activity_type");
                            sqlBulkCopy.ColumnMappings.Add("date_occured", "date_occured");
                            sqlBulkCopy.ColumnMappings.Add("details", "details");
                            sqlBulkCopy.ColumnMappings.Add("linkid", "linkid");
                            con.Open();
                            sqlBulkCopy.WriteToServer(tblcsv);
                            con.Close();
                        }
                    }
我正在生成一个GUID,以将上载的数据与其他数据区分开来。我尝试上载了一些数据,但该GUID未包含在我的SQL Server上载数据列表中。我甚至将其转换为字符串,因为该列为varchar格式

我的代码有什么不足吗

tblcsv.Rows.Add();
                        int count = 0;
                        foreach (string FileRec in csvRow.Split(','))
                        {


                                tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;


                                if (count == 5)
                                { 
                                    tblcsv.Rows[tblcsv.Rows.Count - 1][6] = link; 
                                }
                               count++;

                        }
我试图在递增计数之前先截取计数,因此成功插入了GUID


谢谢

稍微干净一点的方法是:

var fileRecs = csvRow.Split(',').Concat(new [] { link }).ToArray();
for (var count = 0; count < fileRecs.Length; count++)
{
    tblcsv.Rows[tblcsv.Rows.Count - 1][count] = fileRecs[count];
}
var fileRecs=csvRow.Split(',).Concat(new[]{link}).ToArray();
对于(var count=0;count