获取,使用C#中的列表设置...需要帮助 [英] Get,Set using Lists in C# ...Help Needed

查看:114
本文介绍了获取,使用C#中的列表设置...需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey Guys,



我需要在C#中读取CSV文件并将数据放入列表中,然后将数据转储到SQL数据库数据库中。 。我感兴趣的课程是使用CsvHelper;



这是CSV文件中的两个示例数据:



<预LANG = XML> 1,2013-05-14,NISSAN,059 7013,1995,VAN,Deisel,NISSAN,0000,NISSAN,红,MARADANA,OP1,描述p1.jpg + p2.jpg + p3.jpg,R1 + R2 + R3

2,2013-05-14,NISSAN2,059-7013,1995,VAN,Deisel,NISSAN,0000,NISSAN ,Red,MARADANA,op1,description,p1.jpg + p2.jpg + p3.jpg,r1 + r2 + r3



这是CSV文件的类< br $> b $ b

  class 车辆
{
public int ID { get ; set ; }
public string ClosingDate { get ; set ; }
public string 模型{ get ; set ; }
public string Reg { get ; set ; }
public string YearOfManufacture { get ; set ; }
public string VehicleType { get ; set ; }
public string 燃料{ get ; set ; }
public string 品牌{获取; set ; }
public string 里程{ get ; set ; }
public string 使{获取; set ; }
public string 颜色{ get ; set ; }
public string VehicleYard { get ; set ; }
public string BasicOptions { get ; set ; }
public string 描述{ get ; set ; }
public List< string>照片{获取; set ; }
public List< string>区域{获取; set ; }
}





我试过并使用以下代码阅读CSV文件:



 命名空间 ConsoleApplication2 
{
class 计划
{
静态 void Main( string [] args)
{
CsvHelperRead();

Console.Read();
}

private static void CsvHelperRead()
{

CsvConfiguration config = new CsvConfiguration();
config.HasHeaderRecord = false ;

使用 var reader = new CsvReader( new StreamReader( @ C:\Users\afshandc\Desktop\0729\vTest.csv ),配置))
{
<跨度类= 代码关键字>而
(reader.Read())
{
Console.WriteLine(reader.GetField( 1 ));
}
}
}
}
}





我需要阅读CSV文件并将其放到此列表中,任何HElp帮助都会受到欢迎!谢谢!



  CSV 
{
public 列表< Vehicle> ReadVechileCSV()
{







}
}

解决方案

据我所知,你已经达到了阅读文件并在信息框中显示内容的地步但是填充一个新对象?如果是这种情况你只需要向第三部分添加som代码:





列表<车辆GT; lv =  new 列表< vehicle>(); 

while (reader.Read())
{

Vehicle v = new Vehicle();

// Console.WriteLine(reader.GetField(1));
v.ID = reader.GetField( 1 );
v.ClosingDate = Convert.ToDateTime(reader.GetField( 2 ));
....

lv。 add (v);

} < / vehicle > < / vehicle > ;







但我可能没有理解你的问题....


Hey Guys,

I need to read a CSV file in C# and drop the data into a list and then dump the data to a SQL Database database... The class im interested using is using CsvHelper;

This is two sample data in the CSV File:

1,2013-05-14,NISSAN,059 7013,1995,VAN,Deisel,NISSAN,0000,NISSAN,Red,MARADANA,op1,description,p1.jpg+p2.jpg+p3.jpg,r1+r2+r3

2,2013-05-14,NISSAN2,059-7013,1995,VAN,Deisel,NISSAN,0000,NISSAN,Red,MARADANA,op1,description,p1.jpg+p2.jpg+p3.jpg,r1+r2+r3


This is the class for the CSV file

class Vehicle
    {
        public int ID { get; set; }
        public string ClosingDate { get; set; }
        public string Model { get; set; }
        public string Reg { get; set; }
        public string YearOfManufacture { get; set; }
        public string VehicleType { get; set; }
        public string Fuel { get; set; }
        public string Brand { get; set; }
        public string Mileage { get; set; }
        public string Make { get; set; }
        public string Colour { get; set; }
        public string VehicleYard { get; set; }
        public string BasicOptions { get; set; }
        public string Description { get; set; }
        public List<string> Photos { get; set; }
        public List<string> Regions { get; set; }
    }



I tried and Read the CSV file using this code:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            CsvHelperRead();

            Console.Read();
        }

        private static void CsvHelperRead()
        {

            CsvConfiguration config = new CsvConfiguration();
            config.HasHeaderRecord = false;

            using (var reader = new CsvReader(new StreamReader(@"C:\Users\afshandc\Desktop\0729\vTest.csv"), config))
            {
                while (reader.Read())
                {
                    Console.WriteLine(reader.GetField(1));
                }
            }
        }
    }
}



I Need to Read the CSV File and Drop it to this list, Any HElp Help Will be appriciated!Thanks!

class CSV
   {
       public List<Vehicle> ReadVechileCSV()
       {







       }
   }

解决方案

As far as I understand it you got to the point where you read the file and showed the content in a messagbox but don't populate a new object? If thats the case you just needs to add som code to you third section:


List<vehicle> lv = new List<vehicle>();

while (reader.Read())
 {

    Vehicle v = new Vehicle();

 //Console.WriteLine(reader.GetField(1));
     v.ID = reader.GetField(1);
     v.ClosingDate  = Convert.ToDateTime(reader.GetField(2));
     ....

lv.add(v);

 }</vehicle></vehicle>




But I might not have understod your question....


这篇关于获取,使用C#中的列表设置...需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆